-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathbenchmark.js
246 lines (215 loc) · 8.06 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-env node */
/* eslint no-console: "off" */
/* global gc */
"use strict";
const path = require("path");
const helpers = require("./benchmark/helpers.js");
const {createHistogram, performance} = require("perf_hooks");
let saveData = helpers.getFlagExists("save");
let saveTempData = helpers.getFlagExists("save-temp");
let deleteTempData = helpers.getFlagValue("dt");
let filterListName = helpers.getFlagValue("filter-list");
let matchListCase = helpers.getFlagValue("match-list");
let runMatchingBenchmark = helpers.getFlagExists("match");
let benchmarkDate = helpers.getFlagValue("ts") || new Date().toISOString();
let benchmarkDataEntryName;
if (runMatchingBenchmark) {
if (filterListName)
benchmarkDataEntryName = `Matching_${matchListCase}_${filterListName}`;
else
benchmarkDataEntryName = `Matching_${matchListCase}`;
}
else {
benchmarkDataEntryName = `FilterList_${filterListName}`;
}
// Holds benchmark results that will be merged into json file
let benchmarkResults = {};
let matchResults = {};
benchmarkResults[benchmarkDate] = {};
let dataToSaveForTimestamp = benchmarkResults[benchmarkDate];
dataToSaveForTimestamp[benchmarkDataEntryName] = {};
let filterBenchmarkData = dataToSaveForTimestamp[benchmarkDataEntryName];
const {Filter} = require("./lib/filterClasses");
const {parseURL} = require("./lib/url");
const {contentTypes} = require("./lib/contentTypes");
const BENCHMARK_RESULTS = path.join(
__dirname,
"/benchmark/benchmarkresults.json"
);
const TEMP_BENCHMARK_RESULTS = path.join(
__dirname,
"/benchmark/tempresults.json"
);
function sliceString(str) {
// Create a new string in V8 to free up the parent string.
return JSON.parse(JSON.stringify(str));
}
function toMiB(numBytes) {
return numBytes / 1024 / 1024;
}
function nanosToMillis(nanos) {
return nanos / 1e6;
}
async function mergeAndSaveData(dataToMerge) {
if (saveData) {
let benchmarkDataToSave =
helpers.mergeToBenchmarkResults(dataToMerge, BENCHMARK_RESULTS);
await helpers.saveToFile(benchmarkDataToSave, BENCHMARK_RESULTS);
}
if (saveTempData) {
let tempBenchmarkDataToSave =
helpers.mergeToBenchmarkResults(dataToMerge, TEMP_BENCHMARK_RESULTS);
await helpers.saveToFile(tempBenchmarkDataToSave, TEMP_BENCHMARK_RESULTS);
}
}
function printMemory(type = "filterEngine", index = 1) {
gc();
let {heapUsed, heapTotal} = process.memoryUsage();
if (type != "Matching") {
console.log(`Heap (used): ${toMiB(heapUsed)} MiB`);
console.log(`Heap (total): ${toMiB(heapTotal)} MiB`);
console.log();
filterBenchmarkData["HeapUsed"] = toMiB(heapUsed);
filterBenchmarkData["HeapTotal"] = toMiB(heapTotal);
}
// For counting match results
matchResults[`${type}_HeapUsed_${index}`] = toMiB(heapUsed);
matchResults[`${type}_HeapTotal_${index}`] = toMiB(heapTotal);
}
function runMatch(filterEngine, filters, location, contentType, docDomain,
sitekey, specificOnly) {
let url = parseURL(location);
for (let filter of filters)
filterEngine.add(Filter.fromText(filter));
for (let arg of [url, location]) {
filterEngine.match(arg,
contentTypes[contentType],
docDomain,
sitekey,
specificOnly);
}
}
async function performMatchingBenchmark(initialFilters) {
const {filterEngine} = require("./lib/filterEngine");
filterEngine.initialize(initialFilters);
let matchFilterList = [];
switch (matchListCase) {
case "slowlist":
matchFilterList.push("slowlist");
break;
case "unitlist":
matchFilterList.push("unitlist");
break;
case "all":
matchFilterList.push("slowlist");
matchFilterList.push("unitlist");
break;
}
let performanceHistogram = createHistogram();
filterEngine.match = performance.timerify(filterEngine.match, {
histogram: performanceHistogram
});
for (let filter of matchFilterList) {
let filterToMatch = require(`./benchmark/${filter}.json`);
for (let key in filterToMatch) {
let rounds = helpers.getFlagValue("rounds");
if (rounds == null)
rounds = 3;
for (let i = 1; i <= rounds; i++) {
let args = filterToMatch[key]["args"];
await runMatch(filterEngine, ...args);
await Promise.resolve().then(printMemory("Matching", i));
}
}
}
// Counting average and margin for runs
let matchingHeapUsed =
await helpers.countStatisticsOfRuns(matchResults, "HeapUsed");
let matchingHeapTotal =
await helpers.countStatisticsOfRuns(matchResults, "HeapTotal");
filterBenchmarkData["TimeMin"] = nanosToMillis(performanceHistogram.min);
filterBenchmarkData["TimeMean"] = nanosToMillis(performanceHistogram.mean);
filterBenchmarkData["TimeMax"] = nanosToMillis(performanceHistogram.max);
filterBenchmarkData["HeapUsed"] = matchingHeapUsed.average;
filterBenchmarkData["HeapTotal"] = matchingHeapTotal.average;
console.log(`Matching time (min): ${filterBenchmarkData["TimeMin"]}ms`);
console.log(`Matching time (mean): ${filterBenchmarkData["TimeMean"]}ms`);
console.log(`Matching time (max): ${filterBenchmarkData["TimeMax"]}ms`);
console.log(`Matching heap (used): ${filterBenchmarkData["HeapUsed"]} MiB +/- ${matchingHeapUsed.margin} MiB`);
console.log(`Matching heap (total): ${filterBenchmarkData["HeapTotal"]} MiB +/- ${matchingHeapTotal.margin} MiB`);
}
async function performInitializationBenchmark(filters) {
const {filterEngine} = require("./lib/filterEngine");
let performanceHistogram = createHistogram();
filterEngine.initialize = performance.timerify(filterEngine.initialize, {
histogram: performanceHistogram
});
await filterEngine.initialize(filters);
filterBenchmarkData["TimeMean"] = nanosToMillis(performanceHistogram.mean);
console.log(`Initialization time: ${filterBenchmarkData["TimeMean"]}ms`);
// Call printMemory() asynchronously so GC can clean up any objects from
// here.
await new Promise(resolve => setTimeout(() => {
printMemory(); resolve();
}, 1000));
}
async function main() {
try {
console.log("Date", benchmarkDate);
await helpers.populateGitMetadata(dataToSaveForTimestamp);
let filters = [];
if (filterListName) {
let lists = helpers.divideSetToArray(filterListName);
for (let list of lists) {
let content = await helpers.loadFile(list);
filters = filters.concat(content.split(/[\r\n]+/).map(sliceString));
}
}
if (helpers.getFlagValue("cleanup")) {
await helpers.cleanBenchmarkData();
return;
}
if (saveData) {
// Saving data that was initialized at the begining
await mergeAndSaveData(benchmarkResults);
}
if (runMatchingBenchmark) {
if (filterListName)
console.log(`## Matching ${matchListCase} matches with ${filterListName} filters`);
else
console.log(`## Matching ${matchListCase} matches`);
await performMatchingBenchmark(filters);
await mergeAndSaveData(benchmarkResults);
}
else {
console.log(`## Initializing with ${filterListName} filters`);
await performInitializationBenchmark(filters);
await mergeAndSaveData(benchmarkResults);
}
if (helpers.getFlagValue("compare"))
helpers.compareResults(benchmarkDate);
}
finally {
// deleting temporary file to keep it clean
if (deleteTempData)
await helpers.deleteFile(TEMP_BENCHMARK_RESULTS);
}
}
if (require.main == module)
main();