Skip to content

Commit

Permalink
Show human-friendly output (#2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjethani committed Jul 31, 2021
1 parent b9d551d commit 63efa7d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
52 changes: 26 additions & 26 deletions packages/adblocker-benchmarks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
const fs = require('fs');
const path = require('path');

const { _N, _T, _S } = require('./string.js');

const ENGINE = process.argv[process.argv.length - 2];
const REQUESTS_PATH = process.argv[process.argv.length - 1];

Expand Down Expand Up @@ -40,15 +42,15 @@ const WEBREQUEST_OPTIONS = {
};

function min(arr) {
let acc = Number.MAX_VALUE;
let acc = Infinity;
for (let i = 0; i < arr.length; i += 1) {
acc = Math.min(acc, arr[i]);
}
return acc;
}

function max(arr) {
let acc = -1;
let acc = -Infinity;
for (let i = 0; i < arr.length; i += 1) {
acc = Math.max(acc, arr[i]);
}
Expand Down Expand Up @@ -183,7 +185,7 @@ async function main() {
let index = 0;
for (const request of requests) {
if (index !== 0 && index % 10000 === 0) {
console.log(`Processed ${index} requests`);
console.log(_N`Processed ${index} requests`);
}
index += 1;

Expand Down Expand Up @@ -215,36 +217,34 @@ async function main() {

console.log();
console.log(
`Avg serialization time (${serializationTimings.length} samples): ${avg(
serializationTimings,
)}`,
_N`Avg serialization time (${serializationTimings.length} samples): ` +
_T`${avg(serializationTimings)}`,
);
console.log(
`Avg deserialization time (${deserializationTimings.length} samples): ${avg(
deserializationTimings,
)}`,
_N`Avg deserialization time (${deserializationTimings.length} samples): ` +
_T`${avg(deserializationTimings)}`,
);
console.log(`Serialized size: ${cacheSize}`);
console.log(`List parsing time: ${parsingTime}`);
console.log(_S`Serialized size: ${cacheSize}`);
console.log(_T`List parsing time: ${parsingTime}`);
console.log();
console.log(`Total requests: ${all.length}`);
console.log(`Total match: ${matches.length}`);
console.log(`Total no match: ${noMatches.length}`);
console.log(_N`Total requests: ${all.length}`);
console.log(_N`Total match: ${matches.length}`);
console.log(_N`Total no match: ${noMatches.length}`);
console.log();
console.log(`Number of samples: ${matches.length}`);
console.log(`Min match: ${min(matches)}`);
console.log(`Max match: ${max(matches)}`);
console.log(`Avg match: ${avg(matches)}`);
console.log(_N`Number of samples: ${matches.length}`);
console.log(_T`Min match: ${min(matches)}`);
console.log(_T`Max match: ${max(matches)}`);
console.log(_T`Avg match: ${avg(matches)}`);
console.log();
console.log(`Number of samples: ${noMatches.length}`);
console.log(`Min no match: ${min(noMatches)}`);
console.log(`Max no match: ${max(noMatches)}`);
console.log(`Avg no match: ${avg(noMatches)}`);
console.log(_N`Number of samples: ${noMatches.length}`);
console.log(_T`Min no match: ${min(noMatches)}`);
console.log(_T`Max no match: ${max(noMatches)}`);
console.log(_T`Avg no match: ${avg(noMatches)}`);
console.log();
console.log(`Number of samples: ${all.length}`);
console.log(`Min (total): ${min(all)}`);
console.log(`Max (total): ${max(all)}`);
console.log(`Avg (total): ${avg(all)}`);
console.log(_N`Number of samples: ${all.length}`);
console.log(_T`Min (total): ${min(all)}`);
console.log(_T`Max (total): ${max(all)}`);
console.log(_T`Avg (total): ${avg(all)}`);

fs.writeFileSync(`./data/${ENGINE}_timings.json`, JSON.stringify(stats), { encoding: 'utf-8' });
}
Expand Down
27 changes: 27 additions & 0 deletions packages/adblocker-benchmarks/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Copyright (c) 2017-present Cliqz GmbH. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

const _N = (strings, number) => strings[0] + number.toLocaleString() + strings[1];

module.exports = {
_N,

_T: (strings, time) => {
if (time === Infinity || time === -Infinity) {
time = NaN;
}

const formatted = isNaN(time) ? 'n/a' : _N`${Math.round(time * 1e6) / 1e3} μs`;
return strings[0] + formatted + strings[1];
},

_S: (strings, size) => {
const formatted = size === null ? 'n/a' : _N`${Math.ceil(size / 1024)} KiB`;
return strings[0] + formatted + strings[1];
},
};

0 comments on commit 63efa7d

Please sign in to comment.