diff --git a/packages/adblocker-benchmarks/Makefile b/packages/adblocker-benchmarks/Makefile index 9f418b27c8..356d520170 100644 --- a/packages/adblocker-benchmarks/Makefile +++ b/packages/adblocker-benchmarks/Makefile @@ -1,5 +1,9 @@ node_command := NODE_ENV=production node +ifeq ($(DEBUG), 1) + run_options := $(run_options) --debug +endif + ifeq ($(SHOW_MEMORY), 1) node_command := $(node_command) --expose-gc run_options := $(run_options) --memory diff --git a/packages/adblocker-benchmarks/blockers/adblockplus.js b/packages/adblocker-benchmarks/blockers/adblockplus.js index 499c0ecf28..fb94939dce 100644 --- a/packages/adblocker-benchmarks/blockers/adblockplus.js +++ b/packages/adblocker-benchmarks/blockers/adblockplus.js @@ -79,6 +79,11 @@ module.exports = class AdblockPlus { } match(request) { + const text = this.matchDebug(request); + return text !== null && !text.startsWith('@@'); + } + + matchDebug(request) { const url = parseURL(request.url); const sourceURL = parseURL(request.frameUrl); const filter = this.matcher.match( @@ -89,6 +94,6 @@ module.exports = class AdblockPlus { false, ); - return filter !== null && !filter.text.startsWith('@@'); + return filter === null ? null : filter.text; } }; diff --git a/packages/adblocker-benchmarks/blockers/tsurlfilter.js b/packages/adblocker-benchmarks/blockers/tsurlfilter.js index 210cf2d2ab..668539e7b0 100644 --- a/packages/adblocker-benchmarks/blockers/tsurlfilter.js +++ b/packages/adblocker-benchmarks/blockers/tsurlfilter.js @@ -75,4 +75,11 @@ module.exports = class TSUrlFilter { const result = this.engine.matchRequest(request); return result.getBasicResult(); } + + matchDebug({ url, frameUrl, type }) { + const result = TSUrlFilter.hostsOnly ? + this.matchHostname(url) : + this.matchRequest({ url, frameUrl, type }); + return result === null ? null : result.ruleText; + } }; diff --git a/packages/adblocker-benchmarks/run.js b/packages/adblocker-benchmarks/run.js index e3e8fdbe7d..324bac363a 100644 --- a/packages/adblocker-benchmarks/run.js +++ b/packages/adblocker-benchmarks/run.js @@ -16,6 +16,7 @@ const requests = require('./requests.json'); const ENGINE = process.argv[2]; const FLAGS = process.argv.slice(3).filter(arg => arg.startsWith('--')); +const DEBUG = FLAGS.includes('--debug'); const HOSTS_ONLY = FLAGS.includes('--hosts-only'); // Mute info-level output from uBlock Origin @@ -127,6 +128,39 @@ async function memoryUsage(base = { heapUsed: 0, heapTotal: 0, }) { return ({ heapUsed, heapTotal, }); } +async function debug(moduleId, rawLists) { + const output = []; + + const Cls = require(moduleId); + + if (Cls.initialize) { + await Cls.initialize({ hostsOnly: HOSTS_ONLY }); + } + + const engine = await Cls.parse(rawLists); + + for (let index = 0; index < requests.length; index += 1) { + const { url, frameUrl, cpt } = requests[index]; + + if (!isSupportedUrl(url) || !isSupportedUrl(frameUrl)) { + continue; + } + + const match = engine.match({ type: WEBREQUEST_OPTIONS[cpt], frameUrl, url }); + + let matchDebug = null; + if (engine.matchDebug) { + matchDebug = engine.matchDebug({ type: WEBREQUEST_OPTIONS[cpt], frameUrl, url }); + } + + output.push({ index, url, frameUrl, cpt, match, matchDebug }); + } + + fs.writeFileSync(`./${ENGINE}.debug.json`, JSON.stringify(output, null, 2)); + + console.log(`./${ENGINE}.debug.json`); +} + async function main() { const rawLists = loadLists(); @@ -176,6 +210,10 @@ async function main() { process.exit(1); } + if (DEBUG) { + return debug(moduleId, rawLists); + } + const baseMemory = await memoryUsage(); // Initialize