-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
97 lines (86 loc) · 2.29 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
export const MAX_KEY_BYTE_LENGTH = 100
export const MAX_VALUE_BYTE_LENGTH = 5
export const MAX_LINE_BYTE_LENGTH = MAX_KEY_BYTE_LENGTH + 1 + MAX_VALUE_BYTE_LENGTH + 1
export const MINUS_CHAR_CODE = '-'.charCodeAt(0)
export const SEMI_CHAR_CODE = ';'.charCodeAt(0)
export const LINE_FEED_CHAR_CODE = '\n'.charCodeAt(0)
//
export const delayMs = ms => new Promise(resolve => setTimeout(resolve, ms))
export function* range(start, end, step = 1) {
for(let i = start; i < end; i += step) {
yield i
}
}
export function humanByteLength(byteLength) {
let scratch = byteLength
for(const suffix of ['bytes', 'KiB', 'MiB', 'GiB']) {
if(scratch < 1024.0) return `${scratch} ${suffix}`
scratch /= 1024.0
}
}
export function parseValueBuffer(buffer8, offset = 0) {
const byteLength = buffer8.byteLength - offset
// console.log(buffer8, offset, byteLength)
if(buffer8[offset + 0] === MINUS_CHAR_CODE) {
if(byteLength === 5) {
// -xx.x
// console.log('-xx.x')
return -(
100 * (buffer8[offset + 1] - 0x30) +
10 * (buffer8[offset + 2] - 0x30) +
(buffer8[offset + 4] - 0x30)
)
}
else {
// -x.x
// console.log('-x.x')
return -(
10 * (buffer8[offset + 1] - 0x30) +
(buffer8[offset + 3] - 0x30)
)
}
}
else {
if(byteLength === 4) {
// xx.x
// console.log('xx.x')
return (
100 * (buffer8[offset + 0] - 0x30) +
10 * (buffer8[offset + 1] - 0x30) +
(buffer8[offset + 3] - 0x30)
)
}
else {
// x.x
// console.log('x.x')
return (
10 * (buffer8[offset + 0] - 0x30) +
(buffer8[offset + 2] - 0x30)
)
}
}
}
export function processLine(results, key, value) {
const existing = results.get(key)
if (existing === undefined) {
results.set(key, { min: value, max: value, count: 1, sum: value })
postMessage({ type: 'new', key, value, min: value, max: value })
}
else {
existing.count += 1
existing.sum += value
if (value < existing.min) {
existing.min = value
postMessage({ type: 'update', key, ...existing })
}
if (value > existing.max) {
existing.max = value
postMessage({ type: 'update', key, ...existing })
}
const now = Date.now()
if((existing.lastReportTime === undefined) || (now - existing.lastReportTime > 1000)) {
postMessage({ type: 'update', key, ...existing })
existing.lastReportTime = now
}
}
}