Fix reduction ratio calculation
This commit is contained in:
9
app.js
9
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
|
||||
|
||||
Reference in New Issue
Block a user