|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Line | |
|---|
| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const { hrtime } = require("./utils");
|
|---|
| 4 |
|
|---|
| 5 | // The HR-TIME spec calls for 5-μs accuracy. Check that we have that in both hrtime() and Date.now().
|
|---|
| 6 |
|
|---|
| 7 | function testClockAccuracy() {
|
|---|
| 8 | // Test hrtime() first. The check is simpler and more stable, and we use hrtime() to measure Date.now()'s performance.
|
|---|
| 9 | const roundTrip = hrtime(hrtime());
|
|---|
| 10 | if (roundTrip[0] > 1 || roundTrip[1] > 5e3 * 2) {
|
|---|
| 11 | return false;
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | // Test Date.now() twice: first with a looser bound (10 μs) but with a smaller run time to filter out very bad
|
|---|
| 15 | // Date.now() performance, and then with a tighter bound (5 μs) to check we have the accuracy we need.
|
|---|
| 16 | let times;
|
|---|
| 17 | // eslint-disable-next-line no-unused-vars
|
|---|
| 18 | let cur;
|
|---|
| 19 | let start;
|
|---|
| 20 | let end;
|
|---|
| 21 |
|
|---|
| 22 | times = 100;
|
|---|
| 23 | start = hrtime();
|
|---|
| 24 | while (times-- > 0) {
|
|---|
| 25 | cur = Date.now();
|
|---|
| 26 | }
|
|---|
| 27 | end = hrtime(start);
|
|---|
| 28 | if ((end[0] * 1e9 + end[1]) > 1000000) {
|
|---|
| 29 | return false;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | times = 10000;
|
|---|
| 33 | start = hrtime();
|
|---|
| 34 | while (times-- > 0) {
|
|---|
| 35 | cur = Date.now();
|
|---|
| 36 | }
|
|---|
| 37 | end = hrtime(start);
|
|---|
| 38 | if ((end[0] * 1e9 + end[1]) > 50000000) {
|
|---|
| 39 | return false;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | return true;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | // Warm up the function.
|
|---|
| 46 | testClockAccuracy();
|
|---|
| 47 | testClockAccuracy();
|
|---|
| 48 | testClockAccuracy();
|
|---|
| 49 |
|
|---|
| 50 | const TIMES = 5;
|
|---|
| 51 | const THRESHOLD = 0.6 * TIMES;
|
|---|
| 52 | let accurates = 0;
|
|---|
| 53 | for (let i = 0; i < TIMES; i++) {
|
|---|
| 54 | if (testClockAccuracy()) {
|
|---|
| 55 | accurates++;
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | const isAccurate = accurates >= THRESHOLD;
|
|---|
| 60 |
|
|---|
| 61 | module.exports = isAccurate;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.