diff --git a/packages/adblocker-benchmarks/run.js b/packages/adblocker-benchmarks/run.js index 414df17599..9457438db7 100644 --- a/packages/adblocker-benchmarks/run.js +++ b/packages/adblocker-benchmarks/run.js @@ -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]; @@ -40,7 +42,7 @@ 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]); } @@ -48,7 +50,7 @@ function min(arr) { } function max(arr) { - let acc = -1; + let acc = -Infinity; for (let i = 0; i < arr.length; i += 1) { acc = Math.max(acc, arr[i]); } @@ -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; @@ -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' }); } diff --git a/packages/adblocker-benchmarks/string.js b/packages/adblocker-benchmarks/string.js new file mode 100644 index 0000000000..130eeb4458 --- /dev/null +++ b/packages/adblocker-benchmarks/string.js @@ -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]; + }, +};