Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DEBUG=1 flag #2156

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that matchDebug() returns something more useful than just a boolean value. In both the cases here (adblockplus and tsurlfilter) it returns the filter text.

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the engine does not implement matchDebug() the default value is null. In the end every engine should implement something useful.

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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output is pretty so it can be used with tools like diff.


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