Skip to content

Commit 928ab91

Browse files
committed
Add support to benchmark the dynamic filtering pane
From uBO's dev console, type: - `µBlock.sessionFirewall.benchmark();` Keep in mind that it's the temporary ruleset being benchmarked.
1 parent 3ee2553 commit 928ab91

File tree

3 files changed

+116
-59
lines changed

3 files changed

+116
-59
lines changed

src/js/dynamic-net-filtering.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ Matrix.prototype.removeFromRuleParts = function(parts) {
528528

529529
/******************************************************************************/
530530

531-
var magicId = 1;
531+
const magicId = 1;
532532

533533
Matrix.prototype.toSelfie = function() {
534534
return {
@@ -546,6 +546,35 @@ Matrix.prototype.fromSelfie = function(selfie) {
546546

547547
/******************************************************************************/
548548

549+
Matrix.prototype.benchmark = function() {
550+
µBlock.loadBenchmarkDataset().then(requests => {
551+
if ( Array.isArray(requests) === false || requests.length === 0 ) {
552+
console.info('No requests found to benchmark');
553+
return;
554+
}
555+
console.info(`Benchmarking sessionFirewall.evaluateCellZY()...`);
556+
const fctxt = µBlock.filteringContext.duplicate();
557+
const t0 = self.performance.now();
558+
for ( const request of requests ) {
559+
fctxt.setURL(request.url);
560+
fctxt.setTabOriginFromURL(request.frameUrl);
561+
fctxt.setType(request.cpt);
562+
this.evaluateCellZY(
563+
fctxt.getTabHostname(),
564+
fctxt.getHostname(),
565+
fctxt.type
566+
);
567+
}
568+
const t1 = self.performance.now();
569+
const dur = t1 - t0;
570+
console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
571+
console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
572+
});
573+
return 'ok';
574+
};
575+
576+
/******************************************************************************/
577+
549578
return Matrix;
550579

551580
/******************************************************************************/

src/js/static-net-filtering.js

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,74 +2690,20 @@ FilterContainer.prototype.getFilterCount = function() {
26902690

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

2693-
// The requests.json.gz file can be downloaded from:
2694-
// https://cdn.cliqz.com/adblocking/requests_top500.json.gz
2695-
//
2696-
// Which is linked from:
2697-
// https://whotracks.me/blog/adblockers_performance_study.html
2698-
//
2699-
// Copy the file into ./tmp/requests.json.gz
2700-
//
2701-
// If the file is present when you build uBO using `make-[target].sh` from
2702-
// the shell, the resulting package will have `./assets/requests.json`, which
2703-
// will be looked-up by the method below to launch a benchmark session.
2704-
//
2705-
// From uBO's dev console, launch the benchmark:
2706-
// µBlock.staticNetFilteringEngine.benchmark();
2707-
//
2708-
// The advanced setting `consoleLogLevel` must be set to `info` to see the
2709-
// results in uBO's dev console, see:
2710-
// https://github.com/gorhill/uBlock/wiki/Advanced-settings#consoleloglevel
2711-
//
2712-
// The usual browser dev tools can be used to obtain useful profiling
2713-
// data, i.e. start the profiler, call the benchmark method from the
2714-
// console, then stop the profiler when it completes.
2715-
//
2716-
// Keep in mind that the measurements at the blog post above where obtained
2717-
// with ONLY EasyList. The CPU reportedly used was:
2718-
// https://www.cpubenchmark.net/cpu.php?cpu=Intel+Core+i7-6600U+%40+2.60GHz&id=2608
2719-
//
2720-
// Rename ./tmp/requests.json.gz to something else if you no longer want
2721-
// ./assets/requests.json in the build.
2722-
27232693
FilterContainer.prototype.benchmark = function() {
2724-
new Promise(resolve => {
2725-
console.info(`Loading benchmark dataset...`);
2726-
const url = vAPI.getURL('/assets/requests.json');
2727-
µb.assets.fetchText(url, details => {
2728-
if ( details.error !== undefined ) {
2729-
console.info(`Not found: ${url}`);
2730-
resolve();
2731-
return;
2732-
}
2733-
console.info(`Parsing benchmark dataset...`);
2734-
const requests = [];
2735-
const lineIter = new µb.LineIterator(details.content);
2736-
while ( lineIter.eot() === false ) {
2737-
let request;
2738-
try {
2739-
request = JSON.parse(lineIter.next());
2740-
} catch(ex) {
2741-
}
2742-
if ( request instanceof Object === false ) { continue; }
2743-
if ( !request.frameUrl || !request.url ) { continue; }
2744-
requests.push(request);
2745-
}
2746-
resolve(requests);
2747-
});
2748-
}).then(requests => {
2694+
µb.loadBenchmarkDataset().then(requests => {
27492695
if ( Array.isArray(requests) === false || requests.length === 0 ) {
27502696
console.info('No requests found to benchmark');
27512697
return;
27522698
}
2753-
console.info(`Benchmarking...`);
2754-
const fctxt = µb.filteringContext;
2699+
console.info(`Benchmarking staticNetFilteringEngine.matchString()...`);
2700+
const fctxt = µb.filteringContext.duplicate();
27552701
const t0 = self.performance.now();
27562702
for ( const request of requests ) {
27572703
fctxt.setURL(request.url);
27582704
fctxt.setDocOriginFromURL(request.frameUrl);
27592705
fctxt.setType(request.cpt);
2760-
void this.matchString(fctxt);
2706+
this.matchString(fctxt);
27612707
}
27622708
const t1 = self.performance.now();
27632709
const dur = t1 - t0;

src/js/utils.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,85 @@
605605
return rem === 0 ? size : size + rem - 1;
606606
},
607607
};
608+
609+
/******************************************************************************/
610+
611+
// The requests.json.gz file can be downloaded from:
612+
// https://cdn.cliqz.com/adblocking/requests_top500.json.gz
613+
//
614+
// Which is linked from:
615+
// https://whotracks.me/blog/adblockers_performance_study.html
616+
//
617+
// Copy the file into ./tmp/requests.json.gz
618+
//
619+
// If the file is present when you build uBO using `make-[target].sh` from
620+
// the shell, the resulting package will have `./assets/requests.json`, which
621+
// will be looked-up by the method below to launch a benchmark session.
622+
//
623+
// From uBO's dev console, launch the benchmark:
624+
// µBlock.staticNetFilteringEngine.benchmark();
625+
//
626+
// The advanced setting `consoleLogLevel` must be set to `info` to see the
627+
// results in uBO's dev console, see:
628+
// https://github.com/gorhill/uBlock/wiki/Advanced-settings#consoleloglevel
629+
//
630+
// The usual browser dev tools can be used to obtain useful profiling
631+
// data, i.e. start the profiler, call the benchmark method from the
632+
// console, then stop the profiler when it completes.
633+
//
634+
// Keep in mind that the measurements at the blog post above where obtained
635+
// with ONLY EasyList. The CPU reportedly used was:
636+
// https://www.cpubenchmark.net/cpu.php?cpu=Intel+Core+i7-6600U+%40+2.60GHz&id=2608
637+
//
638+
// Rename ./tmp/requests.json.gz to something else if you no longer want
639+
// ./assets/requests.json in the build.
640+
641+
µBlock.loadBenchmarkDataset = (function() {
642+
let datasetPromise;
643+
let ttlTimer;
644+
645+
return function() {
646+
if ( ttlTimer !== undefined ) {
647+
clearTimeout(ttlTimer);
648+
ttlTimer = undefined;
649+
}
650+
651+
vAPI.setTimeout(( ) => {
652+
ttlTimer = undefined;
653+
datasetPromise = undefined;
654+
}, 60000);
655+
656+
if ( datasetPromise !== undefined ) {
657+
return datasetPromise;
658+
}
659+
660+
datasetPromise = new Promise(resolve => {
661+
console.info(`Loading benchmark dataset...`);
662+
const url = vAPI.getURL('/assets/requests.json');
663+
µBlock.assets.fetchText(url, details => {
664+
if ( details.error !== undefined ) {
665+
datasetPromise = undefined;
666+
console.info(`Not found: ${url}`);
667+
resolve();
668+
return;
669+
}
670+
console.info(`Parsing benchmark dataset...`);
671+
const requests = [];
672+
const lineIter = new µBlock.LineIterator(details.content);
673+
while ( lineIter.eot() === false ) {
674+
let request;
675+
try {
676+
request = JSON.parse(lineIter.next());
677+
} catch(ex) {
678+
}
679+
if ( request instanceof Object === false ) { continue; }
680+
if ( !request.frameUrl || !request.url ) { continue; }
681+
requests.push(request);
682+
}
683+
resolve(requests);
684+
});
685+
});
686+
687+
return datasetPromise;
688+
};
689+
})();

0 commit comments

Comments
 (0)