Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
process: add process.hrtime()
Browse files Browse the repository at this point in the history
This commit adds a high-resolution timer function.
  • Loading branch information
TooTallNate authored and bnoordhuis committed Mar 6, 2012
1 parent 544e5ee commit 07c886f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/api/process.markdown
Expand Up @@ -359,3 +359,25 @@ given, otherwise returns the current mask.
## process.uptime()

Number of seconds Node has been running.


## process.hrtime()

Returns the current high-resolution real time in a `[seconds, nanoseconds]`
tuple Array. It is relative to an arbitrary time in the past. It is not
related to the time of day and therefore not subject to clock drift. The
primary use is for measuring performance between intervals.

You may pass in the result of a previous call to `process.hrtime()` to get
a diff reading, useful for benchmarks and measuring intervals:

var t = process.hrtime();
// [ 1800216, 927643717 ]

setTimeout(function () {
t = process.hrtime(t);
// [ 1, 6962306 ]

console.log('benchmark took %d seconds and %d nanoseconds', t[0], t[1]);
// benchmark took 1 seconds and 6962306 nanoseconds
}, 1000);
30 changes: 30 additions & 0 deletions src/node.cc
Expand Up @@ -1571,6 +1571,34 @@ Handle<Value> Kill(const Arguments& args) {
return Undefined();
}

// used in Hrtime() below
#define NANOS_PER_SEC 1000000000

// Hrtime exposes libuv's uv_hrtime() high-resolution timer.
// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
// so this function instead returns an Array with 2 entries representing seconds
// and nanoseconds, to avoid any integer overflow possibility.
// Pass in an Array from a previous hrtime() call to instead get a time diff.
Handle<Value> Hrtime(const v8::Arguments& args) {
HandleScope scope;

uint64_t t = uv_hrtime();

if (args.Length() > 0) {
// return a time diff tuple
Local<Array> inArray = Local<Array>::Cast(args[0]);
uint64_t seconds = inArray->Get(0)->Uint32Value();
uint64_t nanos = inArray->Get(1)->Uint32Value();
t -= (seconds * NANOS_PER_SEC) + nanos;
}

Local<Array> tuple = Array::New(2);
tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC));
tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC));

return scope.Close(tuple);
}


typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);

Expand Down Expand Up @@ -2150,6 +2178,8 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
NODE_SET_METHOD(process, "_debugPause", DebugPause);
NODE_SET_METHOD(process, "_debugEnd", DebugEnd);

NODE_SET_METHOD(process, "hrtime", Hrtime);

NODE_SET_METHOD(process, "dlopen", DLOpen);

NODE_SET_METHOD(process, "uptime", Uptime);
Expand Down
40 changes: 40 additions & 0 deletions test/pummel/test-process-hrtime.js
@@ -0,0 +1,40 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');

var start = process.hrtime();

// process.hrtime() should return an Array
assert(Array.isArray(start));

// busy-loop for 2 seconds
var now = Date.now();
while (Date.now() - now < 2000);

// get a diff reading
var diff = process.hrtime(start);

// should be at least 1 second, at most 2 seconds later
// (the while loop will usually exit a few nanoseconds before 2)
assert(diff[0] >= 1);
assert(diff[0] <= 2);

0 comments on commit 07c886f

Please sign in to comment.