@esmj/monitor
Advanced tools
Comparing version 0.1.1 to 0.2.0
'use strict'; | ||
var observable = require('@esmj/observable'); | ||
var node_process = require('node:process'); | ||
var node_perf_hooks = require('node:perf_hooks'); | ||
var node_os = require('node:os'); | ||
var node_v8 = require('node:v8'); | ||
class ObserverInterface { | ||
pipe() {} | ||
next() {} | ||
error() {} | ||
complete() {} | ||
} | ||
class Observable extends ObserverInterface { | ||
#observers = []; | ||
pipe(...operations) { | ||
return Array.from(operations).reduce( | ||
(observable, operation) => operation(observable), | ||
this | ||
); | ||
} | ||
next(...rest) { | ||
this.#observers.forEach((observer) => { | ||
typeof observer === 'function' | ||
? observer(...rest) | ||
: observer.next(...rest); | ||
}); | ||
} | ||
error(...rest) { | ||
this.#observers.forEach((observer) => { | ||
observer?.error?.(...rest); | ||
this.unsubscribe(observer); | ||
}); | ||
} | ||
complete(...rest) { | ||
this.#observers.forEach((observer) => { | ||
observer?.complete?.(...rest); | ||
this.unsubscribe(observer); | ||
}); | ||
} | ||
subscribe(observer) { | ||
this.#observers.push(observer); | ||
return () => { | ||
this.unsubscribe(observer); | ||
}; | ||
} | ||
unsubscribe(observer) { | ||
const index = this.#observers.indexOf(observer); | ||
this.#observers.splice(index, 1); | ||
} | ||
} | ||
class Monitor extends Observable { | ||
class Monitor extends observable.Observable { | ||
#options = { interval: 1000 }; | ||
@@ -131,2 +79,6 @@ #intervalId = null; | ||
function roundToTwoDecimal(value) { | ||
return Math.round(value * 100) / 100; | ||
} | ||
class CPUUsageMetric extends Metric { | ||
@@ -146,4 +98,5 @@ #cpuUsage = null; | ||
system: cpuUsageData.system, | ||
percent: | ||
(100 * (cpuUsageData.user + cpuUsageData.system)) / (interval * 1000), | ||
percent: roundToTwoDecimal( | ||
(100 * (cpuUsageData.user + cpuUsageData.system)) / (interval * 1000) | ||
), | ||
}, | ||
@@ -177,7 +130,9 @@ }; | ||
eventLoopDelay: { | ||
min: this.#histogram.min / (interval * 1000), | ||
max: this.#histogram.max / (interval * 1000), | ||
mean: this.#histogram.mean / (interval * 1000), | ||
stddev: this.#histogram.stddev / (interval * 1000), | ||
percentile80: this.#histogram.percentile(80) / (interval * 1000), | ||
min: roundToTwoDecimal(this.#histogram.min / (interval * 1000)), | ||
max: roundToTwoDecimal(this.#histogram.max / (interval * 1000)), | ||
mean: roundToTwoDecimal(this.#histogram.mean / (interval * 1000)), | ||
stddev: roundToTwoDecimal(this.#histogram.stddev / (interval * 1000)), | ||
percentile80: roundToTwoDecimal( | ||
this.#histogram.percentile(80) / (interval * 1000) | ||
), | ||
}, | ||
@@ -214,7 +169,13 @@ }; | ||
measure() { | ||
const eventLoopUtilizationData = eventLoopUtilization( | ||
this.#eventLoopUtilizationDataEnd, | ||
this.#eventLoopUtilizationDataStart | ||
); | ||
return { | ||
eventLoopUtilization: eventLoopUtilization( | ||
this.#eventLoopUtilizationDataEnd, | ||
this.#eventLoopUtilizationDataStart | ||
), | ||
eventLoopUtilization: { | ||
idle: roundToTwoDecimal(eventLoopUtilizationData.idle), | ||
active: roundToTwoDecimal(eventLoopUtilizationData.active), | ||
utilization: roundToTwoDecimal(eventLoopUtilizationData.utilization), | ||
}, | ||
}; | ||
@@ -239,5 +200,5 @@ } | ||
loadAverage: { | ||
minute1, | ||
minute5, | ||
minute15, | ||
minute1: roundToTwoDecimal(minute1), | ||
minute5: roundToTwoDecimal(minute5), | ||
minute15: roundToTwoDecimal(minute15), | ||
}, | ||
@@ -249,2 +210,8 @@ }; | ||
class MemoryUsageMetric extends Metric { | ||
#heapStatistics = null; | ||
start() { | ||
this.#heapStatistics = node_v8.getHeapStatistics(); | ||
} | ||
measure() { | ||
@@ -255,2 +222,6 @@ const memoryUsageData = node_process.memoryUsage(); | ||
memoryUsage: { | ||
percent: roundToTwoDecimal( | ||
(memoryUsageData.rss / this.#heapStatistics.total_available_size) * | ||
100 | ||
), | ||
rss: this.#toMB(memoryUsageData.rss), | ||
@@ -265,4 +236,8 @@ heapTotal: this.#toMB(memoryUsageData.heapTotal), | ||
stop() { | ||
this.#heapStatistics = null; | ||
} | ||
#toMB(value) { | ||
return Math.round((value / 1024 / 1024) * 100) / 100; | ||
return roundToTwoDecimal(value / 1024 / 1024); | ||
} | ||
@@ -330,3 +305,3 @@ } | ||
class MetricsHistory extends ObserverInterface { | ||
class MetricsHistory extends observable.Observer { | ||
#options = { limit: 10 }; | ||
@@ -333,0 +308,0 @@ #history = []; |
{ | ||
"name": "@esmj/monitor", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Node.js performance measurement metrics (cpu, memory, event loop, gc)", | ||
@@ -82,3 +82,6 @@ "keywords": [ | ||
} | ||
}, | ||
"dependencies": { | ||
"@esmj/observable": "^0.0.5" | ||
} | ||
} |
@@ -28,21 +28,22 @@ # Monitor | ||
// { | ||
// cpuUsage: { user: 1692, system: 925, percent: 0.2617 }, | ||
// cpuUsage: { user: 1692, system: 925, percent: 0.26 }, | ||
// eventLoopDelay: { | ||
// min: 20.0704, | ||
// max: 21.151743, | ||
// mean: 20.785241043478262, | ||
// stddev: 0.3817646061991465, | ||
// percentile80: 21.086207 | ||
// min: 20.07, | ||
// max: 21.15, | ||
// mean: 20.78, | ||
// stddev: 0.38, | ||
// percentile80: 21.08 | ||
// }, | ||
// eventLoopUtilization: { | ||
// idle: 992.720965999999, | ||
// active: 7.85202197369108, | ||
// utilization: 0.00784752543599303 | ||
// idle: 992.72, | ||
// active: 7.85, | ||
// utilization: 0.01 | ||
// }, | ||
// loadAverage: { | ||
// minute1: 3.38623046875, | ||
// minute5: 8.2880859375, | ||
// minute15: 9.15283203125 | ||
// minute1: 3.38, | ||
// minute5: 8.28, | ||
// minute15: 9.15 | ||
// }, | ||
// memoryUsage: { | ||
// percent: 5.23, | ||
// rss: 54.2, | ||
@@ -49,0 +50,0 @@ // heapTotal: 20.2, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155
33430
1
973
+ Added@esmj/observable@^0.0.5
+ Added@esmj/observable@0.0.5(transitive)