Skip to content

Instantly share code, notes, and snippets.

@othiym23 othiym23/drift.js
Created Aug 7, 2013

Embed
What would you like to do?
When I run this using 0.10.15 on "Darwin bauchelain.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64", instead of getting a roughly stable value, I get one that steadily (but not consistently) increases. Are my units wrong? Is V8's timer that out to lunch?
function hrToMillis(hr) {
return (hr[0] * 1e3) + (hr[1] / 1e6);
}
function wtf() {
return Date.now() - hrToMillis(process.hrtime());
}
var basis = wtf();
// output will grow, but non-monotonically
setInterval(function () { console.log(wtf() - basis ); }, 500);
14.513427734375
15.303466796875
15.666259765625
15.363037109375
15.42578125
16.18408203125
16.325439453125
16.84228515625
16.871337890625
16.511474609375
17.25048828125
16.783203125
16.892333984375
17.9775390625
17.783935546875
17.49072265625
18.184814453125
18.623779296875
18.278564453125
18.9873046875
18.65185546875
19.53076171875
19.301025390625
19.9423828125
19.769775390625
19.615234375
20.3681640625
20.093994140625
19.85791015625
20.780517578125
21.1123046875
20.765869140625
20.822265625
21.179443359375
21.926513671875
21.62939453125
21.555908203125
22.371826171875
22.1591796875
22.8134765625
22.279541015625
23.013427734375
22.6318359375
23.264404296875
22.946533203125
23.712646484375
23.68310546875
23.964599609375
23.740478515625
24.3515625
24.486083984375
25.10302734375
24.79150390625
24.99267578125
25.06689453125
25.333740234375
25.377197265625
26.304443359375
26.385986328125
26.128662109375
26.801513671875
26.632080078125
27.421630859375
27.210205078125
27.032958984375
27.806884765625
27.51318359375
28.21337890625
27.91650390625
28.62841796875
28.350341796875
29.078857421875
28.86279296875
29.354736328125
29.1328125
29.945068359375
29.7119140625
30.42529296875
29.915771484375
30.744140625
30.44921875
31.18994140625
30.95947265625
31.706298828125
31.493408203125
31.24658203125
31.98876953125
31.755126953125
31.95947265625
32.6865234375
32.594970703125
32.40576171875
32.5048828125
33.581298828125
32.96875
33.92529296875
33.683837890625
34.42919921875
34.14501953125
34.854736328125
34.551025390625
35.192138671875
34.811767578125
34.9716796875
34.7265625
35.305908203125
35.927734375
35.712158203125
36.287841796875
36.1240234375
35.9296875
36.701904296875
36.29052734375
37.363037109375
37.247802734375
37.133544921875
37.89501953125
37.641845703125
38.2734375
38.139892578125
38.79638671875
39.13134765625
38.941162109375
38.675048828125
39.067138671875
39.506591796875
39.82666015625
39.39208984375
40.2412109375
39.9482421875
40.684326171875
40.502685546875
41.196044921875
41.363525390625
41.58935546875
41.351806640625
41.111083984375
41.8701171875
41.5673828125
42.22607421875
42.630859375
42.36181640625
43.1201171875
42.62255859375
43.2470703125
42.879638671875
43.6796875
43.406982421875
43.635009765625
44.596923828125
44.02197265625
44.637451171875
44.392822265625
44.9931640625
44.8056640625
45.507080078125
45.210205078125
46.004150390625
45.78271484375
45.87353515625
46.787841796875
46.185546875
47.02294921875
46.789794921875
47.5830078125
47.342529296875
47.60205078125
47.4267578125
48.267333984375
47.9951171875
48.66748046875
@trevnorris

This comment has been minimized.

Copy link

commented Aug 7, 2013

Here's the output on my linux box:

0.382568359375
0.39794921875
0.191162109375
0.225830078125
0.2373046875
0.3310546875
0.396484375
0.527099609375
0.645751953125
0.755126953125
0.765869140625
0.860107421875
0.928466796875
0.0537109375
0.129638671875
0.21044921875
0.281005859375
0.36962890625
0.49462890625
0.60302734375
0.723388671875
0.822021484375
-0.01513671875
0.081298828125
0.203125
0.30078125
0.46630859375
0.59033203125
0.72509765625
0.5517578125
0.60595703125
0.736083984375
0.838134765625
0.667724609375
0.6845703125
0.806640625
0.910888671875
-0.0205078125
0.079345703125
0.197509765625
0.417724609375
0.280029296875
0.31298828125
0.207763671875
0.883544921875
-0.02978515625
0.062744140625
0.1845703125
0.287109375
0.12890625
0.171630859375
0.272705078125
@mattsta

This comment has been minimized.

Copy link

commented Aug 7, 2013

 matt@nibonium:/tmp% node drift.js 
 0.559326171875
 0.88720703125
 0.026123046875
 0.9013671875
 0.730712890625
 0.44189453125
 0.1142578125
 0.946044921875
 0.712890625
 0.276123046875
 1.011962890625
 0.644287109375
 0.43994140625
 0.23828125
 1.068359375
 0.6875
 0.845947265625
 0.892333984375
 0.286865234375
 0.91015625
 0.359130859375
 0.9873046875
 1.179931640625
 0.809326171875
 1.15234375

 matt@nibonium:/tmp% uname -a
 Darwin nibonium.local 13.0.0 Darwin Kernel Version 13.0.0: Wed Jul 17 01:22:21 PDT 2013; root:xnu-2422.1.43~2/RELEASE_X86_64 x86_64

 matt@nibonium:/tmp% node -v
 v0.10.13
@brycebaril

This comment has been minimized.

Copy link

commented Aug 7, 2013

From Ubuntu:

0.212158203125
0.078857421875
0.244140625
-0.5576171875
-0.343994140625
-0.0966796875
0.127685546875
0.331787109375
-0.4541015625
-0.236328125
0.055908203125
0.30908203125
-0.481689453125
-0.266357421875
0.203857421875
-0.5166015625
-0.32421875
-0.10693359375
0.10498046875
0.313232421875
-0.47265625
-0.2607421875
-0.047607421875
0.232666015625
-0.5712890625
-0.30908203125
-0.092041015625
0.086669921875
0.299560546875
-0.31787109375
-0.052001953125
0.15234375
0.363525390625
-0.42919921875
-0.187744140625
0.00927734375
0.282958984375
-0.466796875
-0.2265625
-0.017333984375
0.26806640625
-0.456787109375
-0.123046875
-0.218505859375

@stdarg

This comment has been minimized.

Copy link

commented Aug 7, 2013

I do not see the same behavior on node 0.10.15 on OS X. I see an approximation of trevnorris sees.

@isaacs

This comment has been minimized.

Copy link

commented Aug 7, 2013

On my mac:

$ ./node j.js
0.051025390625
0.165771484375
-0.150634765625
0.521728515625
0.28076171875
-0.03271484375
-0.360595703125
0.32421875
-0.0087890625
0.498779296875
0.099853515625
-0.318115234375
0.344482421875
0.02880859375
-0.070068359375
-0.463134765625
0.32763671875
0.075927734375
-0.247314453125
-0.580322265625
0.11376953125
0.217529296875
-0.02490234375
@othiym23

This comment has been minimized.

Copy link
Owner Author

commented Aug 8, 2013

Yay! My Mac is doomed! Thanks, everybody!

@davidcelis

This comment has been minimized.

Copy link

commented Aug 8, 2013

% node j.js
-0.07861328125
0.147705078125
-0.35107421875
-0.3759765625
-0.66357421875
-0.8837890625
-0.108642578125
-0.339111328125
-0.758056640625
-0.935791015625
-1.2705078125
-0.568359375
-0.8173828125
-1.099609375
-1.3330078125
-1.659423828125
-0.9658203125
-1.063232421875
-1.240234375
-1.494384765625
-1.594970703125
-1.834716796875
-2.162353515625
-2.4443359375
-1.693603515625
-1.9423828125
-2.109619140625
-2.32958984375
-2.090087890625
-2.24462890625
-2.421142578125
-2.63671875
-2.488037109375
-2.460205078125
-2.720947265625
-2.976318359375
-3.231689453125
-3.42919921875
^C
@protodave

This comment has been minimized.

Copy link

commented Aug 8, 2013

Mine drifts some too...

$ node drift.js

13.57958984375
14.47607421875
13.72509765625
14.081787109375
14.22705078125
...
< ~5 minutes later... >
...
18.938232421875
18.806396484375
18.867431640625
18.851806640625
18.8828125
^C

$ node -v; uname -a

v0.10.15
CYGWIN_NT-6.1-WOW64 - 1.7.22(0.268/5/3) 2013-07-22 17:06 i686 Cygwin

Since process.hrtime() "is not related to the time of day and therefore not subject to clock drift", I think you've built yourself a handy little tool to measure a machine's hardware clock quality, and you are seeing your CLOCK_REALTIME slewing relative to your CLOCK_MONOTONIC, +/- clock jitter (btw, darwin is using mach_absolute_time under the hood, compared to other un*xes using clock_gettime(CLOCK_MONOTONIC). Win uses QueryPerformanceCounter).

Your script run results do seem to drift pretty quickly. Does the magnitude of clock frequency error measurement in your ntp driftfile correlate with your script results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.