From bd09f7f3698deacb6e50f745d054f02500c42200 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 20 Feb 2019 01:40:40 +0800 Subject: [PATCH 1/3] process: fix calculation in process.uptime() In https://github.com/nodejs/node/pull/26016 the result returned by process.uptime() was mistakenly set to be based in the wrong unit. This patch fixes the calculation and makes sure the returned value is in seconds. Refs: https://github.com/nodejs/node/pull/26016 --- src/node_process_methods.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index dfcc6641a1c5f8..80027c26ebcf15 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -57,10 +57,8 @@ Mutex umask_mutex; // Microseconds in a second, as a float, used in CPUUsage() below #define MICROS_PER_SEC 1e6 -// used in Hrtime() below +// used in Hrtime() and Uptime() below #define NANOS_PER_SEC 1000000000 -// Used in Uptime() -#define NANOS_PER_MICROS 1e3 #ifdef _WIN32 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */ @@ -246,7 +244,7 @@ static void Uptime(const FunctionCallbackInfo& args) { uv_update_time(env->event_loop()); double uptime = static_cast(uv_hrtime() - per_process::node_start_time); - Local result = Number::New(env->isolate(), uptime / NANOS_PER_MICROS); + Local result = Number::New(env->isolate(), uptime / NANOS_PER_SEC); args.GetReturnValue().Set(result); } From a6ea194f008451603628801d76d0d82cf95226cd Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 20 Feb 2019 01:43:23 +0800 Subject: [PATCH 2/3] process: move test-process-uptime to parallel In addition, do not make too many assumptions about the startup time and timer latency in test-process-uptime. Instead only test that the value is likely in the correct unit (seconds) and it should be increasing in subsequent calls. --- test/{pummel => parallel}/test-process-uptime.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) rename test/{pummel => parallel}/test-process-uptime.js (83%) diff --git a/test/pummel/test-process-uptime.js b/test/parallel/test-process-uptime.js similarity index 83% rename from test/pummel/test-process-uptime.js rename to test/parallel/test-process-uptime.js index 781066371eaa31..d52b97f663ba96 100644 --- a/test/pummel/test-process-uptime.js +++ b/test/parallel/test-process-uptime.js @@ -20,18 +20,18 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); console.error(process.uptime()); -assert.ok(process.uptime() <= 2); +// Add some wiggle room for different platforms. +// Verify that the returned value is in seconds - +// 15 seconds should be a good estimate. +assert.ok(process.uptime() <= 15); const original = process.uptime(); setTimeout(function() { const uptime = process.uptime(); - // some wiggle room to account for timer - // granularity, processor speed, and scheduling - assert.ok(uptime >= original + 2); - assert.ok(uptime <= original + 3); -}, 2000); + assert.ok(original < uptime); +}, 10); From fd7520ee83467b64cb114f3a3a5aca4f1ee15b8d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 19 Feb 2019 11:09:19 -0800 Subject: [PATCH 3/3] fixup! process: move test-process-uptime to parallel --- test/parallel/test-process-uptime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-process-uptime.js b/test/parallel/test-process-uptime.js index d52b97f663ba96..eabb6cf2661c87 100644 --- a/test/parallel/test-process-uptime.js +++ b/test/parallel/test-process-uptime.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); console.error(process.uptime());