@esmj/monitor
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -329,5 +329,22 @@ 'use strict'; | ||
return this.#calculate(array, number); | ||
return this.#calculatePercentile(array, number); | ||
} | ||
trend(key, limit) { | ||
let array = this.#getValues(key); | ||
array = array.slice( | ||
!limit || limit > array.length ? 0 : array.length - limit, | ||
array.length | ||
); | ||
const { slope, yIntercept } = this.#getLinearRegression(array); | ||
return { | ||
slope, | ||
yIntercept, | ||
predict: (x = array.length + 1) => slope * x + yIntercept, | ||
}; | ||
} | ||
#getValues(key) { | ||
@@ -343,3 +360,35 @@ const keys = key?.split('.') ?? []; | ||
#calculate(array, number) { | ||
#getLinearRegression(array) { | ||
const { sumY, sumX, sumX2, sumXY } = array.reduce( | ||
(result, value, index) => { | ||
const x = value?.x ?? index + 1; | ||
const y = value?.y ?? value; | ||
result.sumX += x; | ||
result.sumY += y; | ||
result.sumX2 += x * x; | ||
result.sumXY += x * y; | ||
return result; | ||
}, | ||
{ | ||
sumY: 0, | ||
sumX: 0, | ||
sumX2: 0, | ||
sumXY: 0, | ||
} | ||
); | ||
const divisor = array.length * sumX2 - sumX * sumX; | ||
if (divisor === 0) { | ||
return { slope: 0, yIntercept: 0 }; | ||
} | ||
const yIntercept = (sumY * sumX2 - sumX * sumXY) / divisor; | ||
const slope = (array.length * sumXY - sumX * sumY) / divisor; | ||
return { slope, yIntercept }; | ||
} | ||
#calculatePercentile(array, number) { | ||
if (!array.length) { | ||
@@ -346,0 +395,0 @@ return undefined; |
{ | ||
"name": "@esmj/monitor", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Node.js performance measurement metrics (cpu, memory, event loop, gc)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -114,3 +114,3 @@ # Monitor | ||
#### add(metric) | ||
Monitoring start measure node metric. | ||
Monitoring add measure new metric. | ||
@@ -141,6 +141,6 @@ ##### metric | ||
FIFO size of array for calculating percentile. | ||
FIFO size of array for calculating percentile and linear regressions. | ||
#### percentile(key, value) | ||
Monitoring start measure node metric. | ||
#### percentile(key, number) | ||
Returns defined percentile for measured metric | ||
@@ -153,6 +153,21 @@ ##### key | ||
##### value | ||
##### number | ||
Type: `Number` | ||
Percentile number for FIFO array | ||
Percentile number for FIFO array | ||
#### trend(key, limit) | ||
Returns linear regression variables `slope`, `yIntercept` and `predict` function for measured metric. | ||
##### key | ||
Type: `String` | ||
Path in measured metric structure. | ||
##### limit | ||
Type: `Number` | ||
Defined how much records use for calculating linear regression. Default is use all records from FIFO array. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37335
1099
170