forked from palant/adblockpluscore-perftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test
executable file
·169 lines (145 loc) · 5.85 KB
/
run_test
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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import json
import os
import subprocess
import urllib
import webbrowser
import jinja2
from marionette_driver.marionette import Marionette
BASE_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.join(BASE_DIR, 'data')
CLASSIC_LIB = os.path.join(BASE_DIR, '..', 'adblockpluscore', 'lib')
EMSCRIPTEN_LIB = os.path.join(BASE_DIR, '..', 'adblockpluscore-emscripten',
'lib')
EASYLIST = os.path.join(DATA_DIR, 'easylist.txt')
NODE_TEST_SCRIPT = os.path.join(BASE_DIR, 'node_test.js')
BROWSER_TEST_PAGE = os.path.join(BASE_DIR, 'browser_test.html')
BROWSERIFY = os.path.join(DATA_DIR, 'node_modules', '.bin', 'browserify')
classic_env = {'NODE_PATH': CLASSIC_LIB}
emscripten_env = {'NODE_PATH': EMSCRIPTEN_LIB}
def get_median(results):
median = {}
for key in results[0].keys():
data = sorted(entry[key] for entry in results)
if len(data) % 2 == 0:
median[key] = (0.5 * data[len(data) // 2 - 1] +
0.5 * data[len(data) // 2])
else:
median[key] = data[len(data) // 2]
return median
def run_node_tests(iterations):
def run_node_test(env):
result = subprocess.check_output([
'/usr/bin/node', '--expose-gc', NODE_TEST_SCRIPT
], env=env)
return json.loads(result.decode('utf-8'))
classic_results = []
emscripten_results = []
for i in range(iterations):
classic_results.append(run_node_test(classic_env))
emscripten_results.append(run_node_test(emscripten_env))
return {
'Node.js, classic code': get_median(classic_results),
'Node.js, Emscripten code': get_median(emscripten_results),
}
def run_browser_tests(iterations):
if not os.path.exists(BROWSERIFY):
subprocess.check_call(['/usr/bin/npm', 'install', 'browserify'],
cwd=DATA_DIR)
subprocess.check_call([
BROWSERIFY,
'-o', os.path.join(DATA_DIR, 'lib_classic.js'),
'-r', 'filterClasses'
], env=classic_env)
subprocess.check_call([
BROWSERIFY,
'-o', os.path.join(DATA_DIR, 'lib_emscripten.js'),
'-r', 'filterClasses', '-r', 'compiled'
], env=emscripten_env)
driver = Marionette(app='fxdesktop', bin='/usr/bin/firefox',
gecko_log=os.path.join(DATA_DIR, 'gecko.log'))
driver.start_session()
driver.timeout.implicit = 10
def minimize_memory(driver):
with driver.using_context(driver.CONTEXT_CHROME):
driver.execute_async_script(r'''
const Cc = Components.classes;
const Ci = Components.interfaces;
let manager = Cc["@mozilla.org/memory-reporter-manager;1"]
.getService(Ci.nsIMemoryReporterManager);
manager.minimizeMemoryUsage(marionetteScriptFinished);
''')
def get_memory_usage(driver, url):
with driver.using_context(driver.CONTEXT_CHROME):
return driver.execute_async_script(r'''
const Cc = Components.classes;
const Ci = Components.interfaces;
let manager = Cc["@mozilla.org/memory-reporter-manager;1"]
.getService(Ci.nsIMemoryReporterManager);
let url = arguments[0].replace(/\//g, "\\");
let memory = 0;
manager.getReports((process, path, kind, units, amount,
description, data) =>
{
if (units == Ci.nsIMemoryReporter.UNITS_BYTES &&
path.includes(url))
{
memory += amount;
}
}, null, data =>
{
marionetteScriptFinished(memory);
}, null,false);
''', [url])
def run_browser_test(test_type):
url = 'file://{}?{}'.format(
os.path.abspath(BROWSER_TEST_PAGE), test_type
)
driver.navigate(url)
minimize_memory(driver)
memory_initial = get_memory_usage(driver, url)
driver.navigate('javascript:void(loadData())')
driver.find_element('id', 'ready')
driver.navigate('javascript:void(runTest())')
result = json.loads(
driver.find_element('id', 'done').get_attribute('data-result')
)
minimize_memory(driver)
result['memory'] = get_memory_usage(driver, url) - memory_initial
return result
classic_results = []
emscripten_results = []
try:
for i in range(iterations):
classic_results.append(run_browser_test('classic'))
emscripten_results.append(run_browser_test('emscripten'))
return {
'Firefox, classic code': get_median(classic_results),
'Firefox, Emscripten code': get_median(emscripten_results),
}
finally:
driver.cleanup()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run performance comparison')
parser.add_argument('-i', '--iterations', type=int, default=10,
help='Number of iterations to perform')
args = parser.parse_args()
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
if not os.path.exists(EASYLIST):
urllib.urlretrieve(
'https://easylist-downloads.adblockplus.org/easylist.txt',
EASYLIST
)
result = {}
result.update(run_node_tests(args.iterations))
result.update(run_browser_tests(args.iterations))
env = jinja2.Environment(loader=jinja2.FileSystemLoader(BASE_DIR),
autoescape=True)
template = env.get_template('result.html.tmpl')
result_url = 'data:text/html,' + urllib.quote(template.render({
'result': result,
}))
webbrowser.open(result_url)