Fine tune built-in benchmark

Additionally, add a button in the About pane
to launch benchmark sessions. The button will
be available only when advanced setting
`benchmarkDatasetURL` is set and pointing to
a valid dataset.
This commit is contained in:
Raymond Hill 2020-10-27 12:59:31 -04:00
parent e5b932335c
commit 4059a92838
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
8 changed files with 76 additions and 11 deletions

View File

@ -40,6 +40,11 @@
<div class="li"><span><a href="https://fontawesome.com/" target="_blank">FontAwesome font family</a> by <a href="https://github.com/davegandy">Dave Gandy</a></span></div>
<div class="li"><span><a href="https://github.com/Swatinem/diff" target="_blank">An implementation of Myers' diff algorithm</a> by <a href="https://github.com/Swatinem">Arpad Borsos</a></span></div>
</div>
<hr>
<div id="dev">
<button id="sfneBenchmark">Benchmark static filtering engine</button>
<div id="sfneBenchmarkResult"></div>
</div>
</div>
<script src="js/vapi.js"></script>

View File

@ -9,3 +9,16 @@ body {
.entry {
margin: 0.5em 0;
}
#dev {
align-items: flex-start;
display: none;
}
#dev.enabled {
display: flex;
}
#dev > * {
margin-inline-end: 1em;
}
#dev > div {
white-space: pre;
}

View File

@ -36,5 +36,20 @@
const appData = await vAPI.messaging.send('dashboard', {
what: 'getAppData',
});
uDom('#aboutNameVer').text(appData.name + ' v' + appData.version);
if ( appData.canBenchmark !== true ) { return; }
document.getElementById('dev').classList.add('enabled');
const sfneBenchmark = async ( ) => {
const result = await vAPI.messaging.send('dashboard', {
what: 'sfneBenchmark',
});
document.getElementById('sfneBenchmarkResult').textContent = result;
};
document.getElementById('sfneBenchmark').addEventListener('click', ( ) => {
sfneBenchmark();
});
})();

View File

@ -1084,7 +1084,9 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
if ( injectedHideFilters.length !== 0 ) {
out.injectedHideFilters = injectedHideFilters.join(',\n');
details.code = out.injectedHideFilters + '\n{display:none!important;}';
vAPI.tabs.insertCSS(request.tabId, details);
if ( options.dontInject !== true ) {
vAPI.tabs.insertCSS(request.tabId, details);
}
}
// CSS selectors for collapsible blocked elements
@ -1093,7 +1095,9 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
cacheEntry.retrieve('net', networkFilters);
if ( networkFilters.length !== 0 ) {
details.code = networkFilters.join('\n') + '\n{display:none!important;}';
vAPI.tabs.insertCSS(request.tabId, details);
if ( options.dontInject !== true ) {
vAPI.tabs.insertCSS(request.tabId, details);
}
}
}
@ -1125,12 +1129,13 @@ FilterContainer.prototype.benchmark = async function() {
const options = {
noCosmeticFiltering: false,
noGenericCosmeticFiltering: false,
dontInject: true,
};
let count = 0;
const t0 = self.performance.now();
for ( let i = 0; i < requests.length; i++ ) {
const request = requests[i];
if ( request.cpt !== 'document' ) { continue; }
if ( request.cpt !== 'main_frame' ) { continue; }
count += 1;
details.hostname = µb.URI.hostnameFromURI(request.url);
details.domain = µb.URI.domainFromHostname(details.hostname);

View File

@ -97,6 +97,12 @@ const onMessage = function(request, sender, callback) {
µb.scriptlets.inject(request.tabId, request.scriptlet, callback);
return;
case 'sfneBenchmark':
µb.staticNetFilteringEngine.benchmark().then(result => {
callback(result);
});
return;
default:
break;
}
@ -127,7 +133,8 @@ const onMessage = function(request, sender, callback) {
case 'getAppData':
response = {
name: browser.runtime.getManifest().name,
version: vAPI.app.version
version: vAPI.app.version,
canBenchmark: µb.hiddenSettings.benchmarkDatasetURL !== 'unset',
};
break;

View File

@ -432,7 +432,7 @@
const t0 = self.performance.now();
for ( let i = 0; i < requests.length; i++ ) {
const request = requests[i];
if ( request.cpt !== 'document' ) { continue; }
if ( request.cpt !== 'main_frame' ) { continue; }
count += 1;
details.url = request.url;
details.hostname = µb.URI.hostnameFromURI(request.url);

View File

@ -3544,12 +3544,14 @@ FilterContainer.prototype.benchmark = async function(action, target) {
}
const t0 = self.performance.now();
let matchCount = 0;
for ( let i = 0; i < requests.length; i++ ) {
const request = requests[i];
fctxt.setURL(request.url);
fctxt.setDocOriginFromURL(request.frameUrl);
fctxt.setType(request.cpt);
const r = this.matchString(fctxt);
matchCount += 1;
if ( recorded !== undefined ) { recorded.push(r); }
if ( expected !== undefined && r !== expected[i] ) {
print(`Mismatch with reference results at ${i}:`);
@ -3558,22 +3560,35 @@ FilterContainer.prototype.benchmark = async function(action, target) {
print(`\turl=${fctxt.url}`);
print(`\tdocOrigin=${fctxt.getDocOrigin()}`);
}
if ( fctxt.type === 'main_frame' || fctxt.type === 'sub_frame' ) {
this.matchAndFetchData(fctxt, 'csp');
matchCount += 1;
}
}
const t1 = self.performance.now();
const dur = t1 - t0;
print(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
print(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
if ( expected !== undefined ) {
print(`\tBlocked: ${expected.reduce((n,r)=>{return r===1?n+1:n;},0)}`);
print(`\tExcepted: ${expected.reduce((n,r)=>{return r===2?n+1:n;},0)}`);
}
if ( recorded !== undefined ) {
vAPI.localStorage.setItem(
'FilterContainer.benchmark.results',
JSON.stringify(recorded)
);
}
const output = [
'Benchmarked static network filtering engine:',
`\tEvaluated ${matchCount} match calls in ${dur.toFixed(0)} ms`,
`\tAverage: ${(dur / matchCount).toFixed(3)} ms per request`,
];
if ( expected !== undefined ) {
output.push(
`\tBlocked: ${expected.reduce((n,r)=>{return r===1?n+1:n;},0)}`,
`\tExcepted: ${expected.reduce((n,r)=>{return r===2?n+1:n;},0)}`,
);
}
const s = output.join('\n');
print(s);
return s;
};
/******************************************************************************/

View File

@ -819,6 +819,11 @@
}
if ( request instanceof Object === false ) { continue; }
if ( !request.frameUrl || !request.url ) { continue; }
if ( request.cpt === 'document' ) {
request.cpt = 'main_frame';
} else if ( request.cpt === 'xhr' ) {
request.cpt = 'xmlhttprequest';
}
requests.push(request);
}
return requests;