Skip to content

Commit 92c5f17

Browse files
committed
Improve usefulness of FilterContainer.benchmark()
Add ability to test/record results. This allows to compare against output after code changes to detect and more accurately report regressions.
1 parent 813d961 commit 92c5f17

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/js/static-net-filtering.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,27 +2690,52 @@ FilterContainer.prototype.getFilterCount = function() {
26902690

26912691
/******************************************************************************/
26922692

2693-
FilterContainer.prototype.benchmark = function() {
2693+
FilterContainer.prototype.benchmark = function(action) {
26942694
µb.loadBenchmarkDataset().then(requests => {
26952695
if ( Array.isArray(requests) === false || requests.length === 0 ) {
26962696
console.info('No requests found to benchmark');
26972697
return;
26982698
}
26992699
console.info(`Benchmarking staticNetFilteringEngine.matchString()...`);
27002700
const fctxt = µb.filteringContext.duplicate();
2701-
let blockCount = 0;
2701+
let expected, recorded;
2702+
if ( action === 1 ) {
2703+
try {
2704+
expected = JSON.parse(
2705+
vAPI.localStorage.getItem('FilterContainer.benchmark.results')
2706+
);
2707+
} catch(ex) {
2708+
}
2709+
}
2710+
if ( action === 2 ) {
2711+
recorded = [];
2712+
}
27022713
const t0 = self.performance.now();
2703-
for ( const request of requests ) {
2714+
for ( let i = 0; i < requests.length; i++ ) {
2715+
const request = requests[i];
27042716
fctxt.setURL(request.url);
27052717
fctxt.setDocOriginFromURL(request.frameUrl);
27062718
fctxt.setType(request.cpt);
2707-
if ( this.matchString(fctxt) === 1 ) { blockCount += 1; }
2719+
const r = this.matchString(fctxt);
2720+
if ( recorded !== undefined ) { recorded.push(r); }
2721+
if ( expected !== undefined && r !== expected[i] ) {
2722+
throw 'Mismatch with reference results';
2723+
}
27082724
}
27092725
const t1 = self.performance.now();
27102726
const dur = t1 - t0;
27112727
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
2712-
console.info(`\tBlocked: ${blockCount}`);
27132728
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
2729+
if ( expected !== undefined ) {
2730+
console.info(`\tBlocked: ${expected.reduce((n,r)=>{return r===1?n+1:n;},0)}`);
2731+
console.info(`\tExcepted: ${expected.reduce((n,r)=>{return r===2?n+1:n;},0)}`);
2732+
}
2733+
if ( recorded !== undefined ) {
2734+
vAPI.localStorage.setItem(
2735+
'FilterContainer.benchmark.results',
2736+
JSON.stringify(recorded)
2737+
);
2738+
}
27142739
});
27152740
return 'ok';
27162741
};

0 commit comments

Comments
 (0)