Fully implement the logs and bars switch

This commit is contained in:
2023-11-27 11:45:18 +01:00
parent a0b4e51695
commit fc66f568ea
3 changed files with 55 additions and 38 deletions

View File

@@ -28,7 +28,7 @@ if (options.help) {
process.exit(0); process.exit(0);
} }
const metricManager = new MetricManager(); const metricManager = new MetricManager(options);
verifyDefaults(options, centerOptions); verifyDefaults(options, centerOptions);
verifyExists(options.port, "Port can not be undefined or empty! (--port)", logger); verifyExists(options.port, "Port can not be undefined or empty! (--port)", logger);
@@ -122,7 +122,7 @@ const server = smpp.createServer(
}); });
session.on("submit_sm", async function (pdu) { session.on("submit_sm", async function (pdu) {
if (!options.dr) { if (!options.dr) {
// sessionLogger.info("Replying to incoming submit_sm"); sessionLogger.info("Replying to incoming submit_sm");
rxMetrics.AddEvent(); rxMetrics.AddEvent();
// setTimeout(() => { // setTimeout(() => {
// session.send(pdu.response()); // session.send(pdu.response());
@@ -168,7 +168,9 @@ const server = smpp.createServer(
}; };
sessionLogger.info(`Generated DR as ${drMessage}`); sessionLogger.info(`Generated DR as ${drMessage}`);
session.deliver_sm(DRPdu); session.deliver_sm(DRPdu);
if (txMetrics) {
txMetrics.AddEvent(); txMetrics.AddEvent();
}
}); });
session.on("close", function () { session.on("close", function () {

View File

@@ -7,8 +7,8 @@ const { verifyDefaults, verifyExists } = require("./utils");
const { clientOptions } = require("./cliOptions"); const { clientOptions } = require("./cliOptions");
const { MetricManager } = require("./metrics/metricManager"); const { MetricManager } = require("./metrics/metricManager");
const logger = createBaseLogger();
const options = commandLineArgs(clientOptions); const options = commandLineArgs(clientOptions);
const logger = createBaseLogger(options);
if (options.help) { if (options.help) {
const usage = commandLineUsage([ const usage = commandLineUsage([
@@ -40,7 +40,7 @@ let failed = 0;
const sendTimer = new NanoTimer(); const sendTimer = new NanoTimer();
function startInterval(session, sessionLogger, metrics) { function startInterval(session, sessionLogger, metrics) {
if (!metrics.progress) { if (!metrics.progress && options.bars === true) {
metrics.progress = metricManager.AddMetrics("Send progress", false); metrics.progress = metricManager.AddMetrics("Send progress", false);
metrics.progress.bar.total = options.messagecount; metrics.progress.bar.total = options.messagecount;
metrics.window = metricManager.AddMetrics("Send window", false); metrics.window = metricManager.AddMetrics("Send window", false);
@@ -49,12 +49,14 @@ function startInterval(session, sessionLogger, metrics) {
sendTimer.setInterval( sendTimer.setInterval(
async () => { async () => {
if (sent >= options.messagecount) { if (sent >= options.messagecount) {
// sessionLogger.info(`Finished sending messages success:${success}, failed:${failed}, idling...`); sessionLogger.info(`Finished sending messages success:${success}, failed:${failed}, idling...`);
sendTimer.clearInterval(); sendTimer.clearInterval();
} else if (inFlight < options.window) { } else if (inFlight < options.window) {
// sessionLogger.info(`Sending message ${sent + 1}/${options.messagecount}`); sessionLogger.info(`Sending message ${sent + 1}/${options.messagecount}`);
if (options.bars) {
metrics.progress.bar.increment(); metrics.progress.bar.increment();
metrics.window.bar.increment(); metrics.window.bar.increment();
}
session.submit_sm( session.submit_sm(
{ {
source_addr: options.source, source_addr: options.source,
@@ -62,10 +64,12 @@ function startInterval(session, sessionLogger, metrics) {
short_message: options.message, short_message: options.message,
}, },
function (pdu) { function (pdu) {
if (metrics.window?.bar) {
metrics.window.bar.update(metrics.window.bar.value - 1); metrics.window.bar.update(metrics.window.bar.value - 1);
}
inFlight--; inFlight--;
if (pdu.command_status === 0) { if (pdu.command_status === 0) {
// sessionLogger.info(`Received response with id ${pdu.message_id}`); sessionLogger.info(`Received response with id ${pdu.message_id}`);
success++; success++;
} else { } else {
sessionLogger.warn(`Message failed with id ${pdu.message_id}`); sessionLogger.warn(`Message failed with id ${pdu.message_id}`);
@@ -73,13 +77,15 @@ function startInterval(session, sessionLogger, metrics) {
} }
} }
); );
if (metrics.txMetrics) {
metrics.txMetrics.AddEvent(); metrics.txMetrics.AddEvent();
}
sent++; sent++;
inFlight++; inFlight++;
} else { } else {
// sessionLogger.warn( sessionLogger.warn(
// `${inFlight}/${options.window} messages pending, waiting for a reply before sending more` `${inFlight}/${options.window} messages pending, waiting for a reply before sending more`
// ); );
sendTimer.clearInterval(); sendTimer.clearInterval();
setTimeout(() => startInterval(session, sessionLogger, metrics), options.windowsleep); setTimeout(() => startInterval(session, sessionLogger, metrics), options.windowsleep);
} }
@@ -89,10 +95,10 @@ function startInterval(session, sessionLogger, metrics) {
); );
} }
const metricManager = new MetricManager(); const metricManager = new MetricManager(options);
for (let i = 0; i < options.sessions; i++) { for (let i = 0; i < options.sessions; i++) {
const sessionLogger = createSessionLogger(i); const sessionLogger = createSessionLogger(options, i);
sessionLogger.info(`Connecting to ${options.host}:${options.port}...`); sessionLogger.info(`Connecting to ${options.host}:${options.port}...`);
const session = smpp.connect( const session = smpp.connect(
{ {
@@ -123,14 +129,18 @@ for (let i = 0; i < options.sessions; i++) {
// TODO: Add error message for invalid systemid and password // TODO: Add error message for invalid systemid and password
session.on("deliver_sm", function (pdu) { session.on("deliver_sm", function (pdu) {
if (rxMetrics) {
rxMetrics.AddEvent(); rxMetrics.AddEvent();
// sessionLogger.info("Got deliver_sm, replying..."); }
setTimeout(() => { sessionLogger.info("Got deliver_sm, replying...");
session.send(pdu.response()); // setTimeout(() => {
txMetrics.AddEvent();
}, 200);
// session.send(pdu.response()); // session.send(pdu.response());
// txMetrics.AddEvent(); // txMetrics.AddEvent();
// }, 200);
session.send(pdu.response());
if (txMetrics) {
txMetrics.AddEvent();
}
}); });
session.on("enquire_link", function (pdu) { session.on("enquire_link", function (pdu) {
session.send(pdu.response()); session.send(pdu.response());

View File

@@ -2,7 +2,9 @@ const cliProgress = require("cli-progress");
const { Metric } = require("./metrics"); const { Metric } = require("./metrics");
class MetricManager { class MetricManager {
constructor() { constructor(options) {
this.options = options;
if (options.bars) {
this.metricBufferSize = 1000; this.metricBufferSize = 1000;
this.multibar = new cliProgress.MultiBar( this.multibar = new cliProgress.MultiBar(
{ {
@@ -15,11 +17,14 @@ class MetricManager {
); );
setInterval(() => this.multibar.update(), 100); setInterval(() => this.multibar.update(), 100);
} }
}
AddMetrics(name, refresh = true) { 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, refresh);
return metric; return metric;
} }
}
} }
module.exports = { MetricManager }; module.exports = { MetricManager };