@icgio/icg-utils 1.9.69 → 1.9.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils.js +19 -0
- package/package.json +2 -2
- package/test/hrtime-epoch.test.js +32 -0
package/lib/utils.js
CHANGED
|
@@ -103,6 +103,24 @@ function get_hhmmss() {
|
|
|
103
103
|
return new Date().toTimeString().split(' ')[0]
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
// hrtime as wall-clock ms with the fraction Date.now() drops; anchored once on a Date.now() tick, which lands within a microsecond of a whole millisecond
|
|
107
|
+
let hrtime_epoch_offset_ms = null
|
|
108
|
+
function hrtime_epoch_ms(hr) {
|
|
109
|
+
if (!Array.isArray(hr)) {
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
if (hrtime_epoch_offset_ms === null) {
|
|
113
|
+
const start = Date.now()
|
|
114
|
+
let tick = start
|
|
115
|
+
while (tick === start) {
|
|
116
|
+
tick = Date.now()
|
|
117
|
+
}
|
|
118
|
+
const now = process.hrtime()
|
|
119
|
+
hrtime_epoch_offset_ms = tick - (now[0] * 1e3 + now[1] / 1e6)
|
|
120
|
+
}
|
|
121
|
+
return hr[0] * 1e3 + hr[1] / 1e6 + hrtime_epoch_offset_ms
|
|
122
|
+
}
|
|
123
|
+
|
|
106
124
|
function round(number, precision) {
|
|
107
125
|
if (typeof number === 'string') number = parseFloat(number)
|
|
108
126
|
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)
|
|
@@ -900,6 +918,7 @@ module.exports = {
|
|
|
900
918
|
ordered_dict,
|
|
901
919
|
random_id,
|
|
902
920
|
get_hhmmss,
|
|
921
|
+
hrtime_epoch_ms,
|
|
903
922
|
round,
|
|
904
923
|
cut_orderbook,
|
|
905
924
|
level_position,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.70",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js && node test/merge-trades.test.js"
|
|
10
|
+
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js && node test/merge-trades.test.js && node test/hrtime-epoch.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { hrtime_epoch_ms } = require('../lib/utils.js')
|
|
3
|
+
|
|
4
|
+
let pass = 0,
|
|
5
|
+
fail = 0
|
|
6
|
+
function assert(condition, msg) {
|
|
7
|
+
if (condition) {
|
|
8
|
+
pass++
|
|
9
|
+
} else {
|
|
10
|
+
fail++
|
|
11
|
+
console.log(' FAIL:', msg)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
assert(hrtime_epoch_ms(null) === null, 'null for a missing hrtime')
|
|
16
|
+
assert(hrtime_epoch_ms(undefined) === null, 'null for undefined')
|
|
17
|
+
|
|
18
|
+
const epoch = hrtime_epoch_ms(process.hrtime())
|
|
19
|
+
const wall = Date.now()
|
|
20
|
+
assert(Math.abs(epoch - wall) < 2, `within 2ms of Date.now() (got ${(epoch - wall).toFixed(3)})`)
|
|
21
|
+
|
|
22
|
+
const hr1 = process.hrtime()
|
|
23
|
+
const spin_until = Date.now() + 3
|
|
24
|
+
while (Date.now() < spin_until) {}
|
|
25
|
+
const hr2 = process.hrtime()
|
|
26
|
+
const elapsed_hr = (hr2[0] - hr1[0]) * 1e3 + (hr2[1] - hr1[1]) / 1e6
|
|
27
|
+
const elapsed_epoch = hrtime_epoch_ms(hr2) - hrtime_epoch_ms(hr1)
|
|
28
|
+
assert(Math.abs(elapsed_epoch - elapsed_hr) < 1e-6, 'differences equal hrtime differences')
|
|
29
|
+
assert(elapsed_epoch >= 3 && elapsed_epoch < 10, `elapsed ${elapsed_epoch.toFixed(3)}ms after a 3ms spin`)
|
|
30
|
+
|
|
31
|
+
console.log(`hrtime-epoch tests: ${pass} passed, ${fail} failed`)
|
|
32
|
+
process.exit(fail ? 1 : 0)
|