From e715c265adb32c12ba24bb3b4bb0d03427d9fbb8 Mon Sep 17 00:00:00 2001 From: anelissen Date: Wed, 10 Jan 2024 17:19:18 +0100 Subject: [PATCH] Fix reduction ratio calculation --- app.js | 9 +++++++-- controllers/longpolling.controller.js | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 2ecad2f..ec7aa10 100644 --- a/app.js +++ b/app.js @@ -92,17 +92,22 @@ app.use(function (req, res, next) { } else if (req.is('application/cbor')) { // CBOR frame ///////////// + var cbor_length = 0; + var json_length = 0; console.log('Got CBOR frame'); console.log('Encoded:'); console.log(req.body.toString('hex')) + cbor_length = req.body.length; ///// Decode CBOR body cbor.decodeFirst(req.body, (err, decoded) => { ///// Assign decoded data req.body = decoded; + json_length = JSON.stringify(decoded).length ///// Next handler console.log('Decoded:'); console.log(req.body) + console.log(cbor_length + ' Bytes (CBOR) -> ' + json_length + ' Bytes (JSON) -> ' + Math.round((cbor_length / json_length - 1) * 100) + '% reduction'); next(); }); @@ -136,7 +141,7 @@ app.use(function (req, res, next) { length = JSON.stringify(body).length; ///// Convert ' to " - if (body.data) { body.data = cbor.encode(JSON.parse(body.data.replace(/'/g, '"'))); } + // if (body.data) { body.data = cbor.encode(JSON.parse(body.data.replace(/'/g, '"'))); } ///// Encode JSON to CBOR body = cbor.encode(body); @@ -144,7 +149,7 @@ app.use(function (req, res, next) { // Log statistics console.log('Encoded:'); console.log(body.toString('hex')) - console.log(length + ' Bytes (JSON) -> ' + body.length + ' Bytes (CBOR) -> ' + Math.round((1 - length / body.length) * 100) + '% reduction'); + console.log(length + ' Bytes (JSON) -> ' + body.length + ' Bytes (CBOR) -> ' + Math.round((body.length / length -1) * 100) + '% reduction'); } ///// Send frame diff --git a/controllers/longpolling.controller.js b/controllers/longpolling.controller.js index 52a1ba2..4547392 100644 --- a/controllers/longpolling.controller.js +++ b/controllers/longpolling.controller.js @@ -24,7 +24,8 @@ poll_request = function (method, action, id, device, data, callback) { method: method, nsip: device || '', action: action || '/', - data: JSON.stringify(data).replace(/"/g, "'") || '{}', + // data: JSON.stringify(data).replace(/"/g, "'") || '{}', + data: data || '{}', qid: random_bytes.sync(16).toString('hex') };