@naturalcycles/backend-lib 9.47.1 → 9.48.1

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 type { NumberOfMilliseconds, NumberOfPercent } from '@naturalcycles/js-lib/types';
1
+ import { PerformanceObserver } from 'node:perf_hooks';
2
+ import type { Integer, 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,21 @@ 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
+ * CPU time spent on GC in the last `measureInterval`.
64
+ * In "basis points", 100 basis points == 1%.
65
+ * E.g 125 means 1.25% of CPU spent on GC.
66
+ * It's reported in BPS to be able to do more accurate aggregate reporting
67
+ * (aggregate this number across many instances, where rounding to 1% would be
68
+ * too "lossy")
69
+ */
70
+ gcCPU: Integer;
46
71
  }
@@ -1,5 +1,26 @@
1
- import { monitorEventLoopDelay, performance } from 'node:perf_hooks';
1
+ import { monitorEventLoopDelay, performance, PerformanceObserver } from 'node:perf_hooks';
2
+ /*
3
+ Healthy gcCPU Ranges
4
+ ┌────────┬──────────────────────────────────────────────┐
5
+ │ gcCPU │ Assessment │
6
+ ├────────┼──────────────────────────────────────────────┤
7
+ │ < 2% │ Excellent - well-optimized service │
8
+ ├────────┼──────────────────────────────────────────────┤
9
+ │ 2-5% │ Good - typical for most healthy services │
10
+ ├────────┼──────────────────────────────────────────────┤
11
+ │ 5-10% │ Concerning - worth investigating │
12
+ ├────────┼──────────────────────────────────────────────┤
13
+ │ 10-20% │ Problematic - noticeable latency impact │
14
+ ├────────┼──────────────────────────────────────────────┤
15
+ │ > 20% │ Critical - GC thrashing, service degradation │
16
+ └────────┴──────────────────────────────────────────────┘
17
+
18
+ */
2
19
  /**
20
+ * Monitors EventLoopDelay.
21
+ * Also, monitors GC performance.
22
+ * Once per `measureInterval` sends a callback with stats.
23
+ *
3
24
  * @experimental
4
25
  */
5
26
  export class EventLoopMonitor {
@@ -8,9 +29,16 @@ export class EventLoopMonitor {
8
29
  this.eld = monitorEventLoopDelay({ resolution });
9
30
  this.eld.enable();
10
31
  this.lastElu = performance.eventLoopUtilization();
32
+ this.po = new PerformanceObserver(list => {
33
+ for (const entry of list.getEntries()) {
34
+ this.gcCount++;
35
+ this.gcTotalTime += entry.duration;
36
+ }
37
+ });
38
+ this.po.observe({ entryTypes: ['gc'] });
11
39
  this.interval = setInterval(() => {
12
40
  // Delay stats are reported in **nanoseconds**
13
- const { eld } = this;
41
+ const { eld, gcCount, gcTotalTime } = this;
14
42
  const p50 = Math.round(eld.percentile(50) / 1e6);
15
43
  const p90 = Math.round(eld.percentile(90) / 1e6);
16
44
  const p99 = Math.round(eld.percentile(99) / 1e6);
@@ -20,6 +48,7 @@ export class EventLoopMonitor {
20
48
  const deltaElu = performance.eventLoopUtilization(this.lastElu, currentElu);
21
49
  this.lastElu = currentElu;
22
50
  const elu = Math.round(deltaElu.utilization * 100);
51
+ const gcCPU = Math.round(gcTotalTime / measureInterval);
23
52
  this.lastStats = {
24
53
  p50,
25
54
  p90,
@@ -27,9 +56,14 @@ export class EventLoopMonitor {
27
56
  max,
28
57
  mean,
29
58
  elu,
59
+ gcCount,
60
+ gcTotalTime,
61
+ gcCPU,
30
62
  };
31
63
  cfg.onStats?.(this.lastStats);
32
64
  eld.reset();
65
+ this.gcCount = 0;
66
+ this.gcTotalTime = 0;
33
67
  }, measureInterval);
34
68
  }
35
69
  interval;
@@ -39,8 +73,12 @@ export class EventLoopMonitor {
39
73
  * Undefined until the first interval has completed.
40
74
  */
41
75
  lastStats;
76
+ po;
77
+ gcCount = 0;
78
+ gcTotalTime = 0;
42
79
  stop() {
43
- this.interval.close();
80
+ clearInterval(this.interval);
44
81
  this.eld.disable();
82
+ this.po.disconnect();
45
83
  }
46
84
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.47.1",
4
+ "version": "9.48.1",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -36,7 +36,7 @@
36
36
  "@sentry/node": "^10",
37
37
  "@types/ejs": "^3",
38
38
  "fastify": "^5",
39
- "@naturalcycles/dev-lib": "20.23.0"
39
+ "@naturalcycles/dev-lib": "18.4.2"
40
40
  },
41
41
  "exports": {
42
42
  ".": "./dist/index.js",
@@ -1,8 +1,35 @@
1
1
  import type { EventLoopUtilization, IntervalHistogram } from 'node:perf_hooks'
2
- import { monitorEventLoopDelay, performance } from 'node:perf_hooks'
3
- import type { NumberOfMilliseconds, NumberOfPercent } from '@naturalcycles/js-lib/types'
2
+ import { monitorEventLoopDelay, performance, PerformanceObserver } from 'node:perf_hooks'
3
+ import type {
4
+ Integer,
5
+ NonNegativeInteger,
6
+ NumberOfMilliseconds,
7
+ NumberOfPercent,
8
+ } from '@naturalcycles/js-lib/types'
9
+
10
+ /*
11
+ Healthy gcCPU Ranges
12
+ ┌────────┬──────────────────────────────────────────────┐
13
+ │ gcCPU │ Assessment │
14
+ ├────────┼──────────────────────────────────────────────┤
15
+ │ < 2% │ Excellent - well-optimized service │
16
+ ├────────┼──────────────────────────────────────────────┤
17
+ │ 2-5% │ Good - typical for most healthy services │
18
+ ├────────┼──────────────────────────────────────────────┤
19
+ │ 5-10% │ Concerning - worth investigating │
20
+ ├────────┼──────────────────────────────────────────────┤
21
+ │ 10-20% │ Problematic - noticeable latency impact │
22
+ ├────────┼──────────────────────────────────────────────┤
23
+ │ > 20% │ Critical - GC thrashing, service degradation │
24
+ └────────┴──────────────────────────────────────────────┘
25
+
26
+ */
4
27
 
5
28
  /**
29
+ * Monitors EventLoopDelay.
30
+ * Also, monitors GC performance.
31
+ * Once per `measureInterval` sends a callback with stats.
32
+ *
6
33
  * @experimental
7
34
  */
8
35
  export class EventLoopMonitor {
@@ -14,9 +41,18 @@ export class EventLoopMonitor {
14
41
 
15
42
  this.lastElu = performance.eventLoopUtilization()
16
43
 
44
+ this.po = new PerformanceObserver(list => {
45
+ for (const entry of list.getEntries()) {
46
+ this.gcCount++
47
+ this.gcTotalTime += entry.duration
48
+ }
49
+ })
50
+
51
+ this.po.observe({ entryTypes: ['gc'] })
52
+
17
53
  this.interval = setInterval(() => {
18
54
  // Delay stats are reported in **nanoseconds**
19
- const { eld } = this
55
+ const { eld, gcCount, gcTotalTime } = this
20
56
  const p50 = Math.round(eld.percentile(50) / 1e6)
21
57
  const p90 = Math.round(eld.percentile(90) / 1e6)
22
58
  const p99 = Math.round(eld.percentile(99) / 1e6)
@@ -28,6 +64,7 @@ export class EventLoopMonitor {
28
64
  this.lastElu = currentElu
29
65
 
30
66
  const elu = Math.round(deltaElu.utilization * 100)
67
+ const gcCPU = Math.round(gcTotalTime / measureInterval)
31
68
 
32
69
  this.lastStats = {
33
70
  p50,
@@ -36,11 +73,16 @@ export class EventLoopMonitor {
36
73
  max,
37
74
  mean,
38
75
  elu,
76
+ gcCount,
77
+ gcTotalTime,
78
+ gcCPU,
39
79
  }
40
80
 
41
81
  cfg.onStats?.(this.lastStats)
42
82
 
43
83
  eld.reset()
84
+ this.gcCount = 0
85
+ this.gcTotalTime = 0
44
86
  }, measureInterval)
45
87
  }
46
88
 
@@ -51,10 +93,14 @@ export class EventLoopMonitor {
51
93
  * Undefined until the first interval has completed.
52
94
  */
53
95
  lastStats?: EventLoopStats
96
+ po: PerformanceObserver
97
+ gcCount = 0
98
+ gcTotalTime = 0
54
99
 
55
100
  stop(): void {
56
- this.interval.close()
101
+ clearInterval(this.interval)
57
102
  this.eld.disable()
103
+ this.po.disconnect()
58
104
  }
59
105
 
60
106
  // cfg: Required<EventLoopMonitorCfg>
@@ -92,4 +138,21 @@ export interface EventLoopStats {
92
138
  * utilization: active / (idle + active)
93
139
  */
94
140
  elu: NumberOfPercent
141
+ /**
142
+ * Number of times gc ran during the last `measureInterval`
143
+ */
144
+ gcCount: NonNegativeInteger
145
+ /**
146
+ * Total time spent on gc during the last `measureInterval`
147
+ */
148
+ gcTotalTime: NumberOfMilliseconds
149
+ /**
150
+ * CPU time spent on GC in the last `measureInterval`.
151
+ * In "basis points", 100 basis points == 1%.
152
+ * E.g 125 means 1.25% of CPU spent on GC.
153
+ * It's reported in BPS to be able to do more accurate aggregate reporting
154
+ * (aggregate this number across many instances, where rounding to 1% would be
155
+ * too "lossy")
156
+ */
157
+ gcCPU: Integer
95
158
  }