Fix reduction ratio calculation

This commit is contained in:
anelissen
2024-01-10 17:19:18 +01:00
parent 3e7bcc81cf
commit e715c265ad
2 changed files with 9 additions and 3 deletions

9
app.js
View File

@@ -92,17 +92,22 @@ app.use(function (req, res, next) {
} else if (req.is('application/cbor')) { } else if (req.is('application/cbor')) {
// CBOR frame // CBOR frame
///////////// /////////////
var cbor_length = 0;
var json_length = 0;
console.log('Got CBOR frame'); console.log('Got CBOR frame');
console.log('Encoded:'); console.log('Encoded:');
console.log(req.body.toString('hex')) console.log(req.body.toString('hex'))
cbor_length = req.body.length;
///// Decode CBOR body ///// Decode CBOR body
cbor.decodeFirst(req.body, (err, decoded) => { cbor.decodeFirst(req.body, (err, decoded) => {
///// Assign decoded data ///// Assign decoded data
req.body = decoded; req.body = decoded;
json_length = JSON.stringify(decoded).length
///// Next handler ///// Next handler
console.log('Decoded:'); console.log('Decoded:');
console.log(req.body) console.log(req.body)
console.log(cbor_length + ' Bytes (CBOR) -> ' + json_length + ' Bytes (JSON) -> ' + Math.round((cbor_length / json_length - 1) * 100) + '% reduction');
next(); next();
}); });
@@ -136,7 +141,7 @@ app.use(function (req, res, next) {
length = JSON.stringify(body).length; length = JSON.stringify(body).length;
///// Convert ' to " ///// 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 ///// Encode JSON to CBOR
body = cbor.encode(body); body = cbor.encode(body);
@@ -144,7 +149,7 @@ app.use(function (req, res, next) {
// Log statistics // Log statistics
console.log('Encoded:'); console.log('Encoded:');
console.log(body.toString('hex')) 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 ///// Send frame

View File

@@ -24,7 +24,8 @@ poll_request = function (method, action, id, device, data, callback) {
method: method, method: method,
nsip: device || '', nsip: device || '',
action: action || '/', action: action || '/',
data: JSON.stringify(data).replace(/"/g, "'") || '{}', // data: JSON.stringify(data).replace(/"/g, "'") || '{}',
data: data || '{}',
qid: random_bytes.sync(16).toString('hex') qid: random_bytes.sync(16).toString('hex')
}; };