12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- "use strict";
- const { hrtime } = require("./utils");
- function testClockAccuracy() {
-
- const roundTrip = hrtime(hrtime());
- if (roundTrip[0] > 1 || roundTrip[1] > 5e3 * 2) {
- return false;
- }
-
-
- let times;
-
- let cur;
- let start;
- let end;
- times = 100;
- start = hrtime();
- while (times-- > 0) {
- cur = Date.now();
- }
- end = hrtime(start);
- if ((end[0] * 1e9 + end[1]) > 1000000) {
- return false;
- }
- times = 10000;
- start = hrtime();
- while (times-- > 0) {
- cur = Date.now();
- }
- end = hrtime(start);
- if ((end[0] * 1e9 + end[1]) > 50000000) {
- return false;
- }
- return true;
- }
- testClockAccuracy();
- testClockAccuracy();
- testClockAccuracy();
- const TIMES = 5;
- const THRESHOLD = 0.6 * TIMES;
- let accurates = 0;
- for (let i = 0; i < TIMES; i++) {
- if (testClockAccuracy()) {
- accurates++;
- }
- }
- const isAccurate = accurates >= THRESHOLD;
- module.exports = isAccurate;
|