@esmj/monitor
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -297,2 +297,26 @@ 'use strict'; | ||
function memo(func) { | ||
return (function (func) { | ||
let cache = {}; | ||
const keyGenerator = (...rest) => rest.join('-'); | ||
const clear = () => { | ||
cache = {}; | ||
}; | ||
const memoized = (...rest) => { | ||
const key = keyGenerator(...rest); | ||
if (!cache[key]) { | ||
cache[key] = func(...rest); | ||
} | ||
return cache[key]; | ||
}; | ||
memoized.clear = clear; | ||
return memoized; | ||
})(func); | ||
} | ||
class MetricsHistory extends observable.Observer { | ||
@@ -305,2 +329,5 @@ #options = { limit: 60 }; | ||
this.#options = { ...this.#options, ...options }; | ||
this.percentileMemo = memo((...rest) => this.percentile(...rest)); | ||
this.trendMemo = memo((...rest) => this.trend(...rest)); | ||
} | ||
@@ -312,2 +339,6 @@ | ||
get current() { | ||
return this.#history[this.#history.length - 1]; | ||
} | ||
complete() { | ||
@@ -323,2 +354,5 @@ this.#history = []; | ||
} | ||
this.percentileMemo.clear(); | ||
this.trendMemo.clear(); | ||
} | ||
@@ -325,0 +359,0 @@ |
{ | ||
"name": "@esmj/monitor", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Node.js performance measurement metrics (cpu, memory, event loop, gc)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -25,4 +25,4 @@ # Monitor | ||
const { unsubscribe } = monitor.subscribe((metrics) => { | ||
console.log(metrics); | ||
const { unsubscribe } = monitor.subscribe((metric) => { | ||
console.log(metric); | ||
// { | ||
@@ -71,5 +71,8 @@ // cpuUsage: { user: 1692, system: 925, percent: 0.26 }, | ||
console.log(metricsHistory.percentile('memoryUsage.rss', 80)); // 61 | ||
console.log(metricsHistory.trend('memoryUsage.rss').predict()); // 65 | ||
}, 5000); | ||
setTimeout(() => { | ||
console.log(metricsHistory.size) // 15; | ||
console.log(metricsHistory.current) // return last captured metric structure | ||
unsubscribe(); | ||
@@ -159,2 +162,5 @@ monitor.stop(); | ||
#### percentileMemo(key, number) | ||
Memoized version of percentile function. Cache is cleared after capture new metric. | ||
#### trend(key, limit) | ||
@@ -173,2 +179,5 @@ Returns linear regression variables `slope`, `yIntercept` and `predict` function for measured metric. | ||
Defined how much records use for calculating linear regression. Default is use all records from FIFO array. | ||
Defined how much records use for calculating linear regression. Default is use all records from FIFO array. | ||
#### trendMemo(key, number) | ||
Memoized version of trend function. Cache is cleared after capture new metric. |
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
39857
1174
180