Add more benchmark tools to devtool page

This commit is contained in:
Raymond Hill 2024-03-01 17:26:21 -05:00
parent 9215fefab4
commit 80b66c849a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 80 additions and 27 deletions

View File

@ -29,6 +29,8 @@
<button id="snfe-todnr" type="button">SNFE: DNR<span class="hover"></span></button> <button id="snfe-todnr" type="button">SNFE: DNR<span class="hover"></span></button>
<button id="snfe-benchmark" type="button" disabled>SNFE: Benchmark<span class="hover"></span></button> <button id="snfe-benchmark" type="button" disabled>SNFE: Benchmark<span class="hover"></span></button>
<button id="cfe-dump" type="button">CFE: Dump<span class="hover"></span></button> <button id="cfe-dump" type="button">CFE: Dump<span class="hover"></span></button>
<button id="cfe-benchmark" type="button">CFE: Benchmark<span class="hover"></span></button>
<button id="sfe-benchmark" type="button">SFE: Benchmark<span class="hover"></span></button>
<button id="purge-all-caches" type="button" data-i18n-title="3pPurgeAll"><span data-i18n="3pPurgeAll">_</span><span class="hover"></span></button> <button id="purge-all-caches" type="button" data-i18n-title="3pPurgeAll"><span data-i18n="3pPurgeAll">_</span><span class="hover"></span></button>
</div> </div>
<div id="console" class="codeMirrorContainer"></div> <div id="console" class="codeMirrorContainer"></div>

View File

@ -74,8 +74,8 @@ const loadBenchmarkDataset = (( ) => {
datasetPromise = undefined; datasetPromise = undefined;
}); });
return function() { return async function() {
ttlTimer.offon({ min: 5 }); ttlTimer.offon({ min: 2 });
if ( datasetPromise !== undefined ) { if ( datasetPromise !== undefined ) {
return datasetPromise; return datasetPromise;
@ -84,7 +84,7 @@ const loadBenchmarkDataset = (( ) => {
const datasetURL = µb.hiddenSettings.benchmarkDatasetURL; const datasetURL = µb.hiddenSettings.benchmarkDatasetURL;
if ( datasetURL === 'unset' ) { if ( datasetURL === 'unset' ) {
console.info(`No benchmark dataset available.`); console.info(`No benchmark dataset available.`);
return Promise.resolve(); return;
} }
console.info(`Loading benchmark dataset...`); console.info(`Loading benchmark dataset...`);
datasetPromise = io.fetchText(datasetURL).then(details => { datasetPromise = io.fetchText(datasetURL).then(details => {
@ -136,7 +136,7 @@ const loadBenchmarkDataset = (( ) => {
// action: 1=test // action: 1=test
µb.benchmarkStaticNetFiltering = async function(options = {}) { export async function benchmarkStaticNetFiltering(options = {}) {
const { target, redirectEngine } = options; const { target, redirectEngine } = options;
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
@ -231,11 +231,11 @@ const loadBenchmarkDataset = (( ) => {
const s = output.join('\n'); const s = output.join('\n');
console.info(s); console.info(s);
return s; return s;
}; }
/******************************************************************************/ /******************************************************************************/
µb.tokenHistograms = async function() { export async function tokenHistogramsfunction() {
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) { if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark'); console.info('No requests found to benchmark');
@ -272,11 +272,11 @@ const loadBenchmarkDataset = (( ) => {
const tophits = Array.from(hitTokenMap).sort(customSort).slice(0, 100); const tophits = Array.from(hitTokenMap).sort(customSort).slice(0, 100);
console.info('Misses:', JSON.stringify(topmisses)); console.info('Misses:', JSON.stringify(topmisses));
console.info('Hits:', JSON.stringify(tophits)); console.info('Hits:', JSON.stringify(tophits));
}; }
/******************************************************************************/ /******************************************************************************/
µb.benchmarkDynamicNetFiltering = async function() { export async function benchmarkDynamicNetFiltering() {
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) { if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark'); console.info('No requests found to benchmark');
@ -299,17 +299,19 @@ const loadBenchmarkDataset = (( ) => {
const dur = t1 - t0; const dur = t1 - t0;
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`); console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`); console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
}; }
/******************************************************************************/ /******************************************************************************/
µb.benchmarkCosmeticFiltering = async function() { export async function benchmarkCosmeticFiltering() {
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) { if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark'); console.info('No requests found to benchmark');
return; return;
} }
console.info('Benchmarking cosmeticFilteringEngine.retrieveSpecificSelectors()...'); const output = [
'Benchmarking cosmeticFilteringEngine.retrieveSpecificSelectors()...',
];
const details = { const details = {
tabId: undefined, tabId: undefined,
frameId: undefined, frameId: undefined,
@ -320,6 +322,7 @@ const loadBenchmarkDataset = (( ) => {
const options = { const options = {
noSpecificCosmeticFiltering: false, noSpecificCosmeticFiltering: false,
noGenericCosmeticFiltering: false, noGenericCosmeticFiltering: false,
dontInject: true,
}; };
let count = 0; let count = 0;
const t0 = performance.now(); const t0 = performance.now();
@ -334,19 +337,26 @@ const loadBenchmarkDataset = (( ) => {
} }
const t1 = performance.now(); const t1 = performance.now();
const dur = t1 - t0; const dur = t1 - t0;
console.info(`Evaluated ${count} requests in ${dur.toFixed(0)} ms`); output.push(
console.info(`\tAverage: ${(dur / count).toFixed(3)} ms per request`); `Evaluated ${count} retrieval in ${dur.toFixed(0)} ms`,
}; `\tAverage: ${(dur / count).toFixed(3)} ms per document`
);
const s = output.join('\n');
console.info(s);
return s;
}
/******************************************************************************/ /******************************************************************************/
µb.benchmarkScriptletFiltering = async function() { export async function benchmarkScriptletFiltering() {
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) { if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark'); console.info('No requests found to benchmark');
return; return;
} }
console.info('Benchmarking scriptletFilteringEngine.retrieve()...'); const output = [
'Benchmarking scriptletFilteringEngine.retrieve()...',
];
const details = { const details = {
domain: '', domain: '',
entity: '', entity: '',
@ -369,13 +379,18 @@ const loadBenchmarkDataset = (( ) => {
} }
const t1 = performance.now(); const t1 = performance.now();
const dur = t1 - t0; const dur = t1 - t0;
console.info(`Evaluated ${count} requests in ${dur.toFixed(0)} ms`); output.push(
console.info(`\tAverage: ${(dur / count).toFixed(3)} ms per request`); `Evaluated ${count} retrieval in ${dur.toFixed(0)} ms`,
}; `\tAverage: ${(dur / count).toFixed(3)} ms per document`
);
const s = output.join('\n');
console.info(s);
return s;
}
/******************************************************************************/ /******************************************************************************/
µb.benchmarkOnBeforeRequest = async function() { export async function benchmarkOnBeforeRequest() {
const requests = await loadBenchmarkDataset(); const requests = await loadBenchmarkDataset();
if ( Array.isArray(requests) === false || requests.length === 0 ) { if ( Array.isArray(requests) === false || requests.length === 0 ) {
console.info('No requests found to benchmark'); console.info('No requests found to benchmark');
@ -417,6 +432,6 @@ const loadBenchmarkDataset = (( ) => {
console.info(`\tBlocked ${blockCount} requests`); console.info(`\tBlocked ${blockCount} requests`);
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`); console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
}); });
}; }
/******************************************************************************/ /******************************************************************************/

View File

@ -930,7 +930,7 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
if ( injectedCSS.length !== 0 ) { if ( injectedCSS.length !== 0 ) {
out.injectedCSS = injectedCSS.join('\n\n'); out.injectedCSS = injectedCSS.join('\n\n');
details.code = out.injectedCSS; details.code = out.injectedCSS;
if ( request.tabId !== undefined ) { if ( request.tabId !== undefined && options.dontInject !== true ) {
vAPI.tabs.insertCSS(request.tabId, details); vAPI.tabs.insertCSS(request.tabId, details);
} }
} }
@ -940,7 +940,7 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
const networkFilters = []; const networkFilters = [];
if ( cacheEntry.retrieveNet(networkFilters) ) { if ( cacheEntry.retrieveNet(networkFilters) ) {
details.code = `${networkFilters.join('\n')}\n{display:none!important;}`; details.code = `${networkFilters.join('\n')}\n{display:none!important;}`;
if ( request.tabId !== undefined ) { if ( request.tabId !== undefined && options.dontInject !== true ) {
vAPI.tabs.insertCSS(request.tabId, details); vAPI.tabs.insertCSS(request.tabId, details);
} }
} }

View File

@ -187,6 +187,26 @@ vAPI.messaging.send('dashboard', {
dom.attr(button, 'disabled', null); dom.attr(button, 'disabled', null);
}); });
}); });
dom.on('#cfe-benchmark', 'click', ev => {
const button = ev.target;
dom.attr(button, 'disabled', '');
vAPI.messaging.send('devTools', {
what: 'cfeBenchmark',
}).then(result => {
log(result);
dom.attr(button, 'disabled', null);
});
});
dom.on('#sfe-benchmark', 'click', ev => {
const button = ev.target;
dom.attr(button, 'disabled', '');
vAPI.messaging.send('devTools', {
what: 'sfeBenchmark',
}).then(result => {
log(result);
dom.attr(button, 'disabled', null);
});
});
}); });
/******************************************************************************/ /******************************************************************************/

View File

@ -64,8 +64,6 @@ import {
isNetworkURI, isNetworkURI,
} from './uri-utils.js'; } from './uri-utils.js';
import './benchmarks.js';
/******************************************************************************/ /******************************************************************************/
// https://github.com/uBlockOrigin/uBlock-issues/issues/710 // https://github.com/uBlockOrigin/uBlock-issues/issues/710
@ -1847,8 +1845,26 @@ const onMessage = function(request, sender, callback) {
return; return;
case 'snfeBenchmark': case 'snfeBenchmark':
µb.benchmarkStaticNetFiltering({ redirectEngine }).then(result => { import('/js/benchmarks.js').then(module => {
callback(result); module.benchmarkStaticNetFiltering({ redirectEngine }).then(result => {
callback(result);
});
});
return;
case 'cfeBenchmark':
import('/js/benchmarks.js').then(module => {
module.benchmarkCosmeticFiltering().then(result => {
callback(result);
});
});
return;
case 'sfeBenchmark':
import('/js/benchmarks.js').then(module => {
module.benchmarkScriptletFiltering().then(result => {
callback(result);
});
}); });
return; return;