From 92c5f17b78e5056340f462b049c1871ae0467220 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 14 Apr 2019 09:44:24 -0400 Subject: [PATCH] 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. --- src/js/static-net-filtering.js | 35 +++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index aaa8e64b0..0f6ef9d9b 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -2690,7 +2690,7 @@ FilterContainer.prototype.getFilterCount = function() { /******************************************************************************/ -FilterContainer.prototype.benchmark = function() { +FilterContainer.prototype.benchmark = function(action) { µb.loadBenchmarkDataset().then(requests => { if ( Array.isArray(requests) === false || requests.length === 0 ) { console.info('No requests found to benchmark'); @@ -2698,19 +2698,44 @@ FilterContainer.prototype.benchmark = function() { } console.info(`Benchmarking staticNetFilteringEngine.matchString()...`); const fctxt = µb.filteringContext.duplicate(); - let blockCount = 0; + let expected, recorded; + if ( action === 1 ) { + try { + expected = JSON.parse( + vAPI.localStorage.getItem('FilterContainer.benchmark.results') + ); + } catch(ex) { + } + } + if ( action === 2 ) { + recorded = []; + } const t0 = self.performance.now(); - for ( const request of requests ) { + for ( let i = 0; i < requests.length; i++ ) { + const request = requests[i]; fctxt.setURL(request.url); fctxt.setDocOriginFromURL(request.frameUrl); fctxt.setType(request.cpt); - if ( this.matchString(fctxt) === 1 ) { blockCount += 1; } + const r = this.matchString(fctxt); + if ( recorded !== undefined ) { recorded.push(r); } + if ( expected !== undefined && r !== expected[i] ) { + throw 'Mismatch with reference results'; + } } const t1 = self.performance.now(); const dur = t1 - t0; console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`); - console.info(`\tBlocked: ${blockCount}`); console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`); + if ( expected !== undefined ) { + console.info(`\tBlocked: ${expected.reduce((n,r)=>{return r===1?n+1:n;},0)}`); + console.info(`\tExcepted: ${expected.reduce((n,r)=>{return r===2?n+1:n;},0)}`); + } + if ( recorded !== undefined ) { + vAPI.localStorage.setItem( + 'FilterContainer.benchmark.results', + JSON.stringify(recorded) + ); + } }); return 'ok'; };