@naturalcycles/backend-lib 9.47.0 → 9.48.0
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.
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PerformanceObserver } from 'node:perf_hooks';
|
|
2
|
+
import type { NonNegativeInteger, NumberOfMilliseconds, NumberOfPercent } from '@naturalcycles/js-lib/types';
|
|
2
3
|
/**
|
|
4
|
+
* Monitors EventLoopDelay.
|
|
5
|
+
* Also, monitors GC performance.
|
|
6
|
+
* Once per `measureInterval` sends a callback with stats.
|
|
7
|
+
*
|
|
3
8
|
* @experimental
|
|
4
9
|
*/
|
|
5
10
|
export declare class EventLoopMonitor {
|
|
@@ -11,6 +16,9 @@ export declare class EventLoopMonitor {
|
|
|
11
16
|
* Undefined until the first interval has completed.
|
|
12
17
|
*/
|
|
13
18
|
lastStats?: EventLoopStats;
|
|
19
|
+
po: PerformanceObserver;
|
|
20
|
+
gcCount: number;
|
|
21
|
+
gcTotalTime: number;
|
|
14
22
|
stop(): void;
|
|
15
23
|
}
|
|
16
24
|
export interface EventLoopMonitorCfg {
|
|
@@ -43,4 +51,16 @@ export interface EventLoopStats {
|
|
|
43
51
|
* utilization: active / (idle + active)
|
|
44
52
|
*/
|
|
45
53
|
elu: NumberOfPercent;
|
|
54
|
+
/**
|
|
55
|
+
* Number of times gc ran during the last `measureInterval`
|
|
56
|
+
*/
|
|
57
|
+
gcCount: NonNegativeInteger;
|
|
58
|
+
/**
|
|
59
|
+
* Total time spent on gc during the last `measureInterval`
|
|
60
|
+
*/
|
|
61
|
+
gcTotalTime: NumberOfMilliseconds;
|
|
62
|
+
/**
|
|
63
|
+
* % of CPU time spent on GC in the last `measureInterval`
|
|
64
|
+
*/
|
|
65
|
+
gcCPU: NumberOfPercent;
|
|
46
66
|
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { monitorEventLoopDelay, performance } from 'node:perf_hooks';
|
|
1
|
+
import { monitorEventLoopDelay, performance, PerformanceObserver } from 'node:perf_hooks';
|
|
2
2
|
/**
|
|
3
|
+
* Monitors EventLoopDelay.
|
|
4
|
+
* Also, monitors GC performance.
|
|
5
|
+
* Once per `measureInterval` sends a callback with stats.
|
|
6
|
+
*
|
|
3
7
|
* @experimental
|
|
4
8
|
*/
|
|
5
9
|
export class EventLoopMonitor {
|
|
@@ -8,9 +12,16 @@ export class EventLoopMonitor {
|
|
|
8
12
|
this.eld = monitorEventLoopDelay({ resolution });
|
|
9
13
|
this.eld.enable();
|
|
10
14
|
this.lastElu = performance.eventLoopUtilization();
|
|
15
|
+
this.po = new PerformanceObserver(list => {
|
|
16
|
+
for (const entry of list.getEntries()) {
|
|
17
|
+
this.gcCount++;
|
|
18
|
+
this.gcTotalTime += entry.duration;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
this.po.observe({ entryTypes: ['gc'] });
|
|
11
22
|
this.interval = setInterval(() => {
|
|
12
23
|
// Delay stats are reported in **nanoseconds**
|
|
13
|
-
const { eld } = this;
|
|
24
|
+
const { eld, gcCount, gcTotalTime } = this;
|
|
14
25
|
const p50 = Math.round(eld.percentile(50) / 1e6);
|
|
15
26
|
const p90 = Math.round(eld.percentile(90) / 1e6);
|
|
16
27
|
const p99 = Math.round(eld.percentile(99) / 1e6);
|
|
@@ -20,6 +31,7 @@ export class EventLoopMonitor {
|
|
|
20
31
|
const deltaElu = performance.eventLoopUtilization(this.lastElu, currentElu);
|
|
21
32
|
this.lastElu = currentElu;
|
|
22
33
|
const elu = Math.round(deltaElu.utilization * 100);
|
|
34
|
+
const gcCPU = Math.round((gcTotalTime / measureInterval) * 100);
|
|
23
35
|
this.lastStats = {
|
|
24
36
|
p50,
|
|
25
37
|
p90,
|
|
@@ -27,9 +39,14 @@ export class EventLoopMonitor {
|
|
|
27
39
|
max,
|
|
28
40
|
mean,
|
|
29
41
|
elu,
|
|
42
|
+
gcCount,
|
|
43
|
+
gcTotalTime,
|
|
44
|
+
gcCPU,
|
|
30
45
|
};
|
|
31
46
|
cfg.onStats?.(this.lastStats);
|
|
32
47
|
eld.reset();
|
|
48
|
+
this.gcCount = 0;
|
|
49
|
+
this.gcTotalTime = 0;
|
|
33
50
|
}, measureInterval);
|
|
34
51
|
}
|
|
35
52
|
interval;
|
|
@@ -39,8 +56,12 @@ export class EventLoopMonitor {
|
|
|
39
56
|
* Undefined until the first interval has completed.
|
|
40
57
|
*/
|
|
41
58
|
lastStats;
|
|
59
|
+
po;
|
|
60
|
+
gcCount = 0;
|
|
61
|
+
gcTotalTime = 0;
|
|
42
62
|
stop() {
|
|
43
|
-
this.interval
|
|
63
|
+
clearInterval(this.interval);
|
|
44
64
|
this.eld.disable();
|
|
65
|
+
this.po.disconnect();
|
|
45
66
|
}
|
|
46
67
|
}
|
|
@@ -2,7 +2,7 @@ import { AjvSchema, getCoercingAjv, } from '@naturalcycles/nodejs-lib/ajv';
|
|
|
2
2
|
import { handleValidationError } from '../validateRequest.util.js';
|
|
3
3
|
class AjvValidateRequest {
|
|
4
4
|
body(req, schema, opt = {}) {
|
|
5
|
-
return this.validate(req, 'body', schema, req.rawBody ? () => JSON.parse(req.rawBody.toString()) : undefined, opt);
|
|
5
|
+
return this.validate(req, 'body', schema, req.rawBody?.byteLength ? () => JSON.parse(req.rawBody.toString()) : undefined, opt);
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Query validation uses type coercion (unlike body validation),
|
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import type { EventLoopUtilization, IntervalHistogram } from 'node:perf_hooks'
|
|
2
|
-
import { monitorEventLoopDelay, performance } from 'node:perf_hooks'
|
|
3
|
-
import type {
|
|
2
|
+
import { monitorEventLoopDelay, performance, PerformanceObserver } from 'node:perf_hooks'
|
|
3
|
+
import type {
|
|
4
|
+
NonNegativeInteger,
|
|
5
|
+
NumberOfMilliseconds,
|
|
6
|
+
NumberOfPercent,
|
|
7
|
+
} from '@naturalcycles/js-lib/types'
|
|
4
8
|
|
|
5
9
|
/**
|
|
10
|
+
* Monitors EventLoopDelay.
|
|
11
|
+
* Also, monitors GC performance.
|
|
12
|
+
* Once per `measureInterval` sends a callback with stats.
|
|
13
|
+
*
|
|
6
14
|
* @experimental
|
|
7
15
|
*/
|
|
8
16
|
export class EventLoopMonitor {
|
|
@@ -14,9 +22,18 @@ export class EventLoopMonitor {
|
|
|
14
22
|
|
|
15
23
|
this.lastElu = performance.eventLoopUtilization()
|
|
16
24
|
|
|
25
|
+
this.po = new PerformanceObserver(list => {
|
|
26
|
+
for (const entry of list.getEntries()) {
|
|
27
|
+
this.gcCount++
|
|
28
|
+
this.gcTotalTime += entry.duration
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
this.po.observe({ entryTypes: ['gc'] })
|
|
33
|
+
|
|
17
34
|
this.interval = setInterval(() => {
|
|
18
35
|
// Delay stats are reported in **nanoseconds**
|
|
19
|
-
const { eld } = this
|
|
36
|
+
const { eld, gcCount, gcTotalTime } = this
|
|
20
37
|
const p50 = Math.round(eld.percentile(50) / 1e6)
|
|
21
38
|
const p90 = Math.round(eld.percentile(90) / 1e6)
|
|
22
39
|
const p99 = Math.round(eld.percentile(99) / 1e6)
|
|
@@ -28,6 +45,7 @@ export class EventLoopMonitor {
|
|
|
28
45
|
this.lastElu = currentElu
|
|
29
46
|
|
|
30
47
|
const elu = Math.round(deltaElu.utilization * 100)
|
|
48
|
+
const gcCPU = Math.round((gcTotalTime / measureInterval) * 100)
|
|
31
49
|
|
|
32
50
|
this.lastStats = {
|
|
33
51
|
p50,
|
|
@@ -36,11 +54,16 @@ export class EventLoopMonitor {
|
|
|
36
54
|
max,
|
|
37
55
|
mean,
|
|
38
56
|
elu,
|
|
57
|
+
gcCount,
|
|
58
|
+
gcTotalTime,
|
|
59
|
+
gcCPU,
|
|
39
60
|
}
|
|
40
61
|
|
|
41
62
|
cfg.onStats?.(this.lastStats)
|
|
42
63
|
|
|
43
64
|
eld.reset()
|
|
65
|
+
this.gcCount = 0
|
|
66
|
+
this.gcTotalTime = 0
|
|
44
67
|
}, measureInterval)
|
|
45
68
|
}
|
|
46
69
|
|
|
@@ -51,10 +74,14 @@ export class EventLoopMonitor {
|
|
|
51
74
|
* Undefined until the first interval has completed.
|
|
52
75
|
*/
|
|
53
76
|
lastStats?: EventLoopStats
|
|
77
|
+
po: PerformanceObserver
|
|
78
|
+
gcCount = 0
|
|
79
|
+
gcTotalTime = 0
|
|
54
80
|
|
|
55
81
|
stop(): void {
|
|
56
|
-
this.interval
|
|
82
|
+
clearInterval(this.interval)
|
|
57
83
|
this.eld.disable()
|
|
84
|
+
this.po.disconnect()
|
|
58
85
|
}
|
|
59
86
|
|
|
60
87
|
// cfg: Required<EventLoopMonitorCfg>
|
|
@@ -92,4 +119,16 @@ export interface EventLoopStats {
|
|
|
92
119
|
* utilization: active / (idle + active)
|
|
93
120
|
*/
|
|
94
121
|
elu: NumberOfPercent
|
|
122
|
+
/**
|
|
123
|
+
* Number of times gc ran during the last `measureInterval`
|
|
124
|
+
*/
|
|
125
|
+
gcCount: NonNegativeInteger
|
|
126
|
+
/**
|
|
127
|
+
* Total time spent on gc during the last `measureInterval`
|
|
128
|
+
*/
|
|
129
|
+
gcTotalTime: NumberOfMilliseconds
|
|
130
|
+
/**
|
|
131
|
+
* % of CPU time spent on GC in the last `measureInterval`
|
|
132
|
+
*/
|
|
133
|
+
gcCPU: NumberOfPercent
|
|
95
134
|
}
|