Implement metric interval control

This commit is contained in:
2023-11-30 13:21:10 +01:00
parent fc66f568ea
commit 60af808ea0
5 changed files with 44 additions and 15 deletions

View File

@@ -25,6 +25,22 @@ class CircularBuffer {
}
return result;
}
toArrayRecent(n = 10) {
const result = [];
const threshold = Date.now() - n * 1000;
let current = (this.head - 1 + this.size) % this.size;
while (current !== this.tail) {
if (this.buffer[current] !== undefined && this.buffer[current].timestamp > threshold) {
result.push(this.buffer[current]);
} else {
break;
}
current = (current - 1 + this.size) % this.size;
}
return result;
}
}
module.exports = { CircularBuffer };

View File

@@ -15,13 +15,13 @@ class MetricManager {
},
cliProgress.Presets.shades_grey
);
setInterval(() => this.multibar.update(), 100);
setInterval(() => this.multibar.update(), 200);
}
}
AddMetrics(name, refresh = true) {
if (this.options.bars) {
const metric = new Metric(name, this.multibar, this.metricBufferSize, refresh);
const metric = new Metric(name, this.multibar, this.metricBufferSize, this.options, refresh);
return metric;
}
}

View File

@@ -1,13 +1,14 @@
const { CircularBuffer } = require("./circularBuffer");
class Metric {
constructor(barName, multibar, bufferSize, refresh = true) {
constructor(barName, multibar, bufferSize, options, refresh = true) {
this.options = options;
this.multibar = multibar;
this.bar = multibar.create(0, 0);
this.bar.update(0, { name: barName });
this.bar.total = 1000;
this.maxRate = 1000;
this.bar.total = this.maxRate;
this.buffer = new CircularBuffer(bufferSize);
this.maxRate = 0;
if (refresh) {
setInterval(this.UpdateBar.bind(this), 100);
}
@@ -19,21 +20,18 @@ class Metric {
}
GetRate() {
const entries = this.buffer.toArray();
const currentTime = Date.now();
const interval = entries.length > 1 ? currentTime - entries[0].timestamp : 1;
const entries = this.buffer.toArrayRecent(this.options.metricsinterval);
const totalRX = entries.reduce((sum, entry) => sum + entry.count, 0);
return Math.round((totalRX / interval) * 100000) / 100;
return Math.round((totalRX / this.options.metricsinterval) * 100) / 100;
}
UpdateBar() {
const eps = this.GetRate();
// if (eps > this.maxRate) {
// this.bar.total = eps;
// this.maxRate = eps;
// }
if (eps > this.maxRate) {
this.bar.total = eps;
this.maxRate = eps;
}
this.bar.update(eps);
}
}