mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 09:07:54 +01:00
Start using async/await where it makes sense
This commit is contained in:
parent
3d31a5f5d7
commit
7b8c087fdd
1 changed files with 52 additions and 50 deletions
|
@ -3125,58 +3125,60 @@ FilterContainer.prototype.getFilterCount = function() {
|
|||
|
||||
// action: 1=test, 2=record
|
||||
|
||||
FilterContainer.prototype.benchmark = function(action) {
|
||||
µb.loadBenchmarkDataset().then(requests => {
|
||||
if ( Array.isArray(requests) === false || requests.length === 0 ) {
|
||||
console.info('No requests found to benchmark');
|
||||
return;
|
||||
}
|
||||
console.info(`Benchmarking staticNetFilteringEngine.matchString()...`);
|
||||
const fctxt = µb.filteringContext.duplicate();
|
||||
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 ( 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);
|
||||
if ( recorded !== undefined ) { recorded.push(r); }
|
||||
if ( expected !== undefined && r !== expected[i] ) {
|
||||
console.log('Mismatch with reference results:');
|
||||
console.log(`\tExpected ${expected[i]}, got ${r}:`);
|
||||
console.log(`\ttype=${fctxt.type}`);
|
||||
console.log(`\turl=${fctxt.url}`);
|
||||
console.log(`\tdocOrigin=${fctxt.getDocOrigin()}`);
|
||||
}
|
||||
}
|
||||
const t1 = self.performance.now();
|
||||
const dur = t1 - t0;
|
||||
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
|
||||
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)
|
||||
FilterContainer.prototype.benchmark = async function(action) {
|
||||
const requests = await µb.loadBenchmarkDataset();
|
||||
|
||||
if ( Array.isArray(requests) === false || requests.length === 0 ) {
|
||||
console.info('No requests found to benchmark');
|
||||
return;
|
||||
}
|
||||
|
||||
console.info(`Benchmarking staticNetFilteringEngine.matchString()...`);
|
||||
const fctxt = µb.filteringContext.duplicate();
|
||||
let expected, recorded;
|
||||
if ( action === 1 ) {
|
||||
try {
|
||||
expected = JSON.parse(
|
||||
vAPI.localStorage.getItem('FilterContainer.benchmark.results')
|
||||
);
|
||||
} catch(ex) {
|
||||
}
|
||||
});
|
||||
return 'ok';
|
||||
}
|
||||
if ( action === 2 ) {
|
||||
recorded = [];
|
||||
}
|
||||
|
||||
const t0 = self.performance.now();
|
||||
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);
|
||||
if ( recorded !== undefined ) { recorded.push(r); }
|
||||
if ( expected !== undefined && r !== expected[i] ) {
|
||||
console.log('Mismatch with reference results:');
|
||||
console.log(`\tExpected ${expected[i]}, got ${r}:`);
|
||||
console.log(`\ttype=${fctxt.type}`);
|
||||
console.log(`\turl=${fctxt.url}`);
|
||||
console.log(`\tdocOrigin=${fctxt.getDocOrigin()}`);
|
||||
}
|
||||
}
|
||||
const t1 = self.performance.now();
|
||||
const dur = t1 - t0;
|
||||
|
||||
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
|
||||
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)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************-
|
||||
|
|
Loading…
Reference in a new issue