Skip to content

Commit

Permalink
Add DEBUG=1 flag (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjethani committed Aug 12, 2021
1 parent f70c870 commit d941fd3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/adblocker-benchmarks/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/adblocker-benchmarks/blockers/adblockplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -89,6 +94,6 @@ module.exports = class AdblockPlus {
false,
);

return filter !== null && !filter.text.startsWith('@@');
return filter === null ? null : filter.text;
}
};
7 changes: 7 additions & 0 deletions packages/adblocker-benchmarks/blockers/tsurlfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
38 changes: 38 additions & 0 deletions packages/adblocker-benchmarks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -176,6 +210,10 @@ async function main() {
process.exit(1);
}

if (DEBUG) {
return debug(moduleId, rawLists);
}

const baseMemory = await memoryUsage();

// Initialize
Expand Down

0 comments on commit d941fd3

Please sign in to comment.