From 60af808ea0da5eb7e522fb95140f6fded7273995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Thu, 30 Nov 2023 13:21:10 +0100 Subject: [PATCH] Implement metric interval control --- center.js | 5 ++++- cliOptions.js | 12 ++++++++++++ metrics/circularBuffer.js | 16 ++++++++++++++++ metrics/metricManager.js | 4 ++-- metrics/metrics.js | 22 ++++++++++------------ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/center.js b/center.js index 9e346d1..f4ae752 100644 --- a/center.js +++ b/center.js @@ -43,6 +43,7 @@ const sendTimer = new NanoTimer(); // TODO: Fix issue where a client disconnecting does not stop this timer // TODO: Fix issue where only one session is being utilized because they all share the same timer +// TODO: Maybe add only receiver and only transmitter modes instead of transciever // Instead just use the same timer but make a pool of connections; That way both problems will be solved function startInterval(session, sessionLogger, rxMetrics) { if (!options.messagecount > 0) { @@ -123,7 +124,9 @@ const server = smpp.createServer( session.on("submit_sm", async function (pdu) { if (!options.dr) { sessionLogger.info("Replying to incoming submit_sm"); - rxMetrics.AddEvent(); + if (options.bars) { + rxMetrics.AddEvent(); + } // setTimeout(() => { // session.send(pdu.response()); // }, 200); diff --git a/cliOptions.js b/cliOptions.js index ef54500..119124f 100644 --- a/cliOptions.js +++ b/cliOptions.js @@ -56,6 +56,12 @@ const clientOptions = [ type: Boolean, description: "Display TX and RX bars. Can be used with logs (although it will make a mess)." }, + { + name: "metricsinterval", + type: Number, + defaultOption: 5, + description: "Sets the interval for measuring metrics. A value of 5 considers the packets within the last 5 seconds. Defaults to 5." + }, ]; const centerOptions = [ @@ -127,6 +133,12 @@ const centerOptions = [ type: Boolean, description: "Display TX and RX bars. Can be used with logs (although it will make a mess)." }, + { + name: "metricsinterval", + type: Number, + defaultOption: 5, + description: "Sets the interval for measuring metrics. A value of 5 considers the packets within the last 5 seconds. Defaults to 5." + }, ]; module.exports = { clientOptions, centerOptions }; diff --git a/metrics/circularBuffer.js b/metrics/circularBuffer.js index b23e185..603617d 100644 --- a/metrics/circularBuffer.js +++ b/metrics/circularBuffer.js @@ -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 }; diff --git a/metrics/metricManager.js b/metrics/metricManager.js index 79d8345..33e2e21 100644 --- a/metrics/metricManager.js +++ b/metrics/metricManager.js @@ -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; } } diff --git a/metrics/metrics.js b/metrics/metrics.js index 0de11ad..0fb6f39 100644 --- a/metrics/metrics.js +++ b/metrics/metrics.js @@ -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); } }