From 9fccd87eebd2193b825a18cbcb88cc8120b59790 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 5 May 2020 12:05:30 +0200
Subject: [PATCH] refactor: use Date.now() instead of process.hrtime()

---
 lib/datasource/rubygems/get-rubygems-org.ts    |  8 +++-----
 lib/platform/git/storage.ts                    | 18 ++++++------------
 lib/util/exec/index.ts                         |  8 +++-----
 .../repository/process/extract-update.ts       |  8 +++-----
 lib/workers/repository/process/fetch.ts        |  8 +++-----
 package.json                                   |  1 -
 yarn.lock                                      |  5 -----
 7 files changed, 18 insertions(+), 38 deletions(-)

diff --git a/lib/datasource/rubygems/get-rubygems-org.ts b/lib/datasource/rubygems/get-rubygems-org.ts
index bda6288b34..489a47f89b 100644
--- a/lib/datasource/rubygems/get-rubygems-org.ts
+++ b/lib/datasource/rubygems/get-rubygems-org.ts
@@ -1,4 +1,3 @@
-import { hrtime } from 'process';
 import { logger } from '../../logger';
 import { Http } from '../../util/http';
 import { DatasourceError, ReleaseResult } from '../common';
@@ -24,11 +23,10 @@ async function updateRubyGemsVersions(): Promise<void> {
   let newLines: string;
   try {
     logger.debug('Rubygems: Fetching rubygems.org versions');
-    const startTime = hrtime();
+    const startTime = Date.now();
     newLines = (await http.get(url, options)).body;
-    const duration = hrtime(startTime);
-    const seconds = Math.round(duration[0] + duration[1] / 1e9);
-    logger.debug({ seconds }, 'Rubygems: Fetched rubygems.org versions');
+    const durationMs = Math.round(Date.now() - startTime);
+    logger.debug({ durationMs }, 'Rubygems: Fetched rubygems.org versions');
   } catch (err) /* istanbul ignore next */ {
     if (err.statusCode !== 416) {
       contentLength = 0;
diff --git a/lib/platform/git/storage.ts b/lib/platform/git/storage.ts
index 04b1a22608..2aca6841e7 100644
--- a/lib/platform/git/storage.ts
+++ b/lib/platform/git/storage.ts
@@ -1,6 +1,5 @@
 import { join } from 'path';
 import URL from 'url';
-import convertHrtime from 'convert-hrtime';
 import fs from 'fs-extra';
 import Git from 'simple-git/promise';
 import {
@@ -145,17 +144,14 @@ export class Storage {
       try {
         this._git = Git(cwd).silent(true);
         await this._git.raw(['remote', 'set-url', 'origin', config.url]);
-        const fetchStart = process.hrtime();
+        const fetchStart = Date.now();
         await this._git.fetch(['--depth=10']);
         await setBaseBranchToDefault(this._git);
         await this._resetToBranch(config.baseBranch);
         await this._cleanLocalBranches();
         await this._git.raw(['remote', 'prune', 'origin']);
-        const fetchSeconds =
-          Math.round(
-            1 + 10 * convertHrtime(process.hrtime(fetchStart)).seconds
-          ) / 10;
-        logger.debug({ fetchSeconds }, 'git fetch completed');
+        const durationMs = Math.round(Date.now() - fetchStart);
+        logger.debug({ durationMs }, 'git fetch completed');
         clone = false;
       } catch (err) /* istanbul ignore next */ {
         logger.error({ err }, 'git fetch error');
@@ -164,7 +160,7 @@ export class Storage {
     if (clone) {
       await fs.emptyDir(cwd);
       this._git = Git(cwd).silent(true);
-      const cloneStart = process.hrtime();
+      const cloneStart = Date.now();
       try {
         // clone only the default branch
         let opts = ['--depth=2'];
@@ -178,10 +174,8 @@ export class Storage {
         logger.debug({ err }, 'git clone error');
         throw new Error(PLATFORM_FAILURE);
       }
-      const seconds =
-        Math.round(1 + 10 * convertHrtime(process.hrtime(cloneStart)).seconds) /
-        10;
-      logger.debug({ seconds }, 'git clone completed');
+      const durationMs = Math.round(Date.now() - cloneStart);
+      logger.debug({ durationMs }, 'git clone completed');
     }
     const submodules = await this.getSubmodules();
     for (const submodule of submodules) {
diff --git a/lib/util/exec/index.ts b/lib/util/exec/index.ts
index f2618a4b87..27f60115a6 100644
--- a/lib/util/exec/index.ts
+++ b/lib/util/exec/index.ts
@@ -1,6 +1,5 @@
 import { ExecOptions as ChildProcessExecOptions } from 'child_process';
 import { dirname, join } from 'path';
-import { hrtime } from 'process';
 import { RenovateConfig } from '../../config/common';
 import { logger } from '../../logger';
 import {
@@ -140,7 +139,7 @@ export async function exec(
 
   let res: ExecResult | null = null;
   for (const rawExecCommand of commands) {
-    const startTime = hrtime();
+    const startTime = Date.now();
     let timer;
     const { timeout } = rawExecOptions;
     if (useDocker) {
@@ -168,13 +167,12 @@ export async function exec(
       throw err;
     }
     clearTimeout(timer);
-    const duration = hrtime(startTime);
-    const seconds = Math.round(duration[0] + duration[1] / 1e9);
+    const durationMs = Math.round(Date.now() - startTime);
     if (res) {
       logger.debug(
         {
           cmd: rawExecCommand,
-          seconds,
+          durationMs,
           stdout: res.stdout,
           stderr: res.stderr,
         },
diff --git a/lib/workers/repository/process/extract-update.ts b/lib/workers/repository/process/extract-update.ts
index cdacbe3ada..b446cfc52e 100644
--- a/lib/workers/repository/process/extract-update.ts
+++ b/lib/workers/repository/process/extract-update.ts
@@ -1,4 +1,3 @@
-import { hrtime } from 'process';
 import { RenovateConfig } from '../../../config';
 import { logger } from '../../../logger';
 import { PackageFile } from '../../../manager/common';
@@ -46,12 +45,11 @@ function extractStats(packageFiles: Record<string, PackageFile[]>): any {
 
 export async function extract(config: RenovateConfig): Promise<ExtractResult> {
   logger.debug('extractAndUpdate()');
-  const startTime = hrtime();
+  const startTime = Date.now();
   const packageFiles = await extractAllDependencies(config);
-  const duration = hrtime(startTime);
-  const seconds = Math.round(duration[0] + duration[1] / 1e9);
+  const durationMs = Math.round(Date.now() - startTime);
   const stats = extractStats(packageFiles);
-  logger.info({ stats, seconds }, `Dependency extraction complete`);
+  logger.info({ stats, durationMs }, `Dependency extraction complete`);
   logger.trace({ config: packageFiles }, 'packageFiles');
   await fetchUpdates(config, packageFiles);
   logger.debug({ config: packageFiles }, 'packageFiles with updates');
diff --git a/lib/workers/repository/process/fetch.ts b/lib/workers/repository/process/fetch.ts
index 19a5289cf1..1b3daef5cc 100644
--- a/lib/workers/repository/process/fetch.ts
+++ b/lib/workers/repository/process/fetch.ts
@@ -1,4 +1,3 @@
-import { hrtime } from 'process';
 import pAll from 'p-all';
 import {
   ManagerConfig,
@@ -115,12 +114,11 @@ export async function fetchUpdates(
   packageFiles: Record<string, PackageFile[]>
 ): Promise<void> {
   const managers = Object.keys(packageFiles);
-  const startTime = hrtime();
+  const startTime = Date.now();
   const allManagerJobs = managers.map((manager) =>
     fetchManagerUpdates(config, packageFiles, manager)
   );
   await Promise.all(allManagerJobs);
-  const duration = hrtime(startTime);
-  const seconds = Math.round(duration[0] + duration[1] / 1e9);
-  logger.info({ seconds }, 'Package releases lookups complete');
+  const durationMs = Math.round(Date.now() - startTime);
+  logger.info({ durationMs }, 'Package releases lookups complete');
 }
diff --git a/package.json b/package.json
index 7077031193..b40be85a83 100644
--- a/package.json
+++ b/package.json
@@ -121,7 +121,6 @@
     "clean-git-ref": "2.0.1",
     "commander": "5.1.0",
     "conventional-commits-detector": "1.0.2",
-    "convert-hrtime": "3.0.0",
     "deepmerge": "4.2.2",
     "delay": "4.3.0",
     "detect-indent": "6.0.0",
diff --git a/yarn.lock b/yarn.lock
index 3e1616e257..4bd80e9734 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2903,11 +2903,6 @@ conventional-commits-parser@^3.0.0, conventional-commits-parser@^3.0.7:
     through2 "^3.0.0"
     trim-off-newlines "^1.0.0"
 
-convert-hrtime@3.0.0:
-  version "3.0.0"
-  resolved "https://registry.yarnpkg.com/convert-hrtime/-/convert-hrtime-3.0.0.tgz#62c7593f5809ca10be8da858a6d2f702bcda00aa"
-  integrity sha512-7V+KqSvMiHp8yWDuwfww06XleMWVVB9b9tURBx+G7UTADuo5hYPuowKloz4OzOqbPezxgo+fdQ1522WzPG4OeA==
-
 convert-source-map@^1.1.0, convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
   version "1.7.0"
   resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
-- 
GitLab