forked from Kudo/react-native-js-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
375 lines (318 loc) · 12.5 KB
/
start.py
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python
import argparse
import glob
import json
import os
import sys
import tempfile
from lib.colorful import colorful
from lib.logger import (get_logger, setup_logger)
from lib.section import (h1, h2)
from lib.tools import (AdbTool, ApkTool)
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
logger = get_logger(__name__)
class RenderComponentThroughput:
def __init__(self, app_id, interval):
self._app_id = app_id
self._interval = interval
def run(self):
AdbTool.stop_apps()
AdbTool.clear_log()
AdbTool.start_with_link(
self._app_id,
'/RenderComponentThroughput?interval={}'.format(self._interval))
result = AdbTool.wait_for_console_log(r'count=(\d+)').group(1)
memory = AdbTool.get_memory(self._app_id)
return (int(result), int(memory))
def run_with_average(self, times):
ret = {
'result': 0,
'memory': 0,
}
for _ in range(times):
(result, memory) = self.run()
ret['result'] += result
ret['memory'] += memory
# NOTE(kudo): Keeps thing simpler to trim as integer
ret['result'] = int(ret['result'] / times)
ret['memory'] = int(ret['memory'] / times)
return ret
class TTI:
def __init__(self, app_id, size):
self._app_id = app_id
self._size = size
def run(self, apk_install_kwargs):
data_file_path = os.path.join(ROOT_DIR, 'src', 'TTI', 'data.json')
with self.PatchBundleContext(data_file_path, self._size):
ApkTool.reinstall(**apk_install_kwargs)
result = self._run_batch_with_average(3)
logger.info('{app} {result}'.format(
app=self._app_id, result=result))
class PatchBundleContext:
def __init__(self, data_file_path, size):
self._data_file_path = data_file_path
self._size = size
def __enter__(self):
os.rename(self._data_file_path, self._data_file_path + '.bak')
with open(self._data_file_path, 'w') as f:
f.write(self._generate_json_string(self._size))
def _generate_json_string(self, size):
data = {
'description': 'GENERATE_FAKE_DATA',
'size': size,
'data': 'a' * size,
}
return json.dumps(data)
def __exit__(self, type, value, traceback):
os.rename(self._data_file_path + '.bak', self._data_file_path)
@classmethod
def _wait_for_tti_log(cls):
return int(AdbTool.wait_for_log(r'TTI=(\d+)', 'MeasureTTI').group(1))
@classmethod
def _start(cls, app_id):
os.system(
'adb shell am start -a android.intent.action.VIEW -d "rnbench://{}/TTI" > /dev/null'
.format(app_id))
def _run_batch(self):
AdbTool.stop_apps()
AdbTool.clear_log()
self._start(self._app_id)
return self._wait_for_tti_log()
def _run_batch_with_average(self, times):
result = 0
for _ in range(times):
result += self._run_batch()
result = int(result / times)
return result
class JSDistManager:
STORE_DIST_DIR = os.path.join(ROOT_DIR, 'js_dist')
DISTS = {
'jsc_official_245459': {
'download_url':
'https://registry.npmjs.org/jsc-android/-/jsc-android-245459.0.0.tgz',
'version':
'245459.0.0',
'meta': ('Baseline JIT (but not x86)', 'WebKitGTK 2.24.2',
'Support Intl'),
'aar_glob':
'**/android-jsc-intl/**/*.aar',
'binary_name':
'libjsc.so',
},
'jsc_245459_no_jit': {
'download_url':
'https://registry.npmjs.org/@kudo-ci/jsc-android/-/jsc-android-245459.0.0-no-jit.tgz',
'version':
'245459.0.0-no-jit',
'meta': ('JIT-less', 'WebKitGTK 2.24.2', 'Support Intl'),
'aar_glob':
'**/android-jsc-intl/**/*.aar',
'binary_name':
'libjsc.so',
},
'v8_751': {
'download_url':
'https://registry.npmjs.org/v8-android/-/v8-android-7.5.1.tgz',
'version':
'7.5.1',
'meta': ('JIT-less (but not arm64-v8a)', 'V8 7.5.288.23',
'Support Intl'),
'aar_glob':
'**/*.aar',
'binary_name':
'libv8.so',
},
'v8_751_jit': {
'download_url':
'https://registry.npmjs.org/v8-android/-/v8-android-7.5.1-jit.tgz',
'version':
'7.5.1',
'meta': ('JIT', 'V8 7.5.288.23', 'Support Intl'),
'aar_glob':
'**/*.aar',
'binary_name':
'libv8.so',
},
}
def __init__(self, dist_id):
self._dist_id = dist_id
self._dist_info = self.DISTS[dist_id]
def prepare(self):
js_dist_path = os.path.join(self.STORE_DIST_DIR, self._dist_id)
maven_dist_path = os.path.join(js_dist_path, 'package', 'dist')
if not os.path.isdir(maven_dist_path):
logger.info('JSDistManager::prepare() - Download and extract\n')
os.system('mkdir -p {}'.format(js_dist_path))
self._download_dist(self._dist_info['download_url'], js_dist_path)
return maven_dist_path
def get_binary_size(self, abi=None):
js_dist_path = os.path.join(self.STORE_DIST_DIR, self._dist_id)
if not os.path.exists(js_dist_path):
raise RuntimeError('js_dist_path is not existed - ' + js_dist_path)
aar_paths = glob.glob(
os.path.join(js_dist_path, self._dist_info['aar_glob']),
recursive=True)
if len(aar_paths) < 1:
return -1
aar_path = aar_paths[0]
_abi = abi or 'armeabi-v7a'
binary_path = os.path.join('jni', _abi, self._dist_info['binary_name'])
output_file = tempfile.NamedTemporaryFile(delete=False)
output_path = output_file.name
output_file.close()
cmd = 'unzip -p {aar_path} {binary_path} > {output_path}'.format(
aar_path=aar_path,
binary_path=binary_path,
output_path=output_path)
logger.debug('get_binary_size - cmd: {}'.format(cmd))
os.system(cmd)
size = os.path.getsize(output_path)
self._strip_binary(output_path, _abi)
size = os.path.getsize(output_path)
os.unlink(output_path)
return size
@property
def info(self):
return self._dist_info
@classmethod
def _download_dist(cls, url, output_path):
cmd = 'wget -O- "{url}" | tar x - -C "{output_path}"'.format(
url=url, output_path=output_path)
logger.debug('download_dist - cmd: {}'.format(cmd))
os.system(cmd)
@classmethod
def _strip_binary(cls, file_path, abi):
ndk_path = os.environ['NDK_PATH']
if not ndk_path:
raise RuntimeError('NDK_PATH environment variable is not defined.')
mappings = {
'armeabi-v7a': 'arm-linux-androideabi-*',
'arm64-v8a': 'aarch64-linux-android-*',
'x86': 'x86-*',
'x86_64': 'x86_64-*',
}
strip_tool_paths = glob.glob(
os.path.join(ndk_path, 'toolchains', mappings[abi], '**',
'*-strip'),
recursive=True)
if len(strip_tool_paths) < 1:
raise RuntimeError('Unable to find strip from NDK toolchains')
strip_tool_path = strip_tool_paths[0]
cmd = strip_tool_path + ' ' + file_path
logger.debug('strip_binary - cmd: {}'.format(cmd))
os.system(cmd)
def show_configs(abi, jsc_dist_manager, v8_dist_manager):
logger.info('ABI: {}\n'.format(abi or 'default'))
logger.info('JSC version: {}\nJSC meta: {}\nJSC binary size: {}\n'.format(
jsc_dist_manager.info['version'],
', '.join(jsc_dist_manager.info['meta']),
jsc_dist_manager.get_binary_size(abi)))
logger.info('V8 version: {}\nV8 meta: {}\nV8 binary size: {}\n'.format(
v8_dist_manager.info['version'],
', '.join(v8_dist_manager.info['meta']),
v8_dist_manager.get_binary_size(abi)))
def parse_args():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
'--verbose', '-v', action='store_true', help='Enable verbose log')
arg_parser.add_argument(
'--all', '-a', action='store_true', help='Run all benchmarks')
arg_parser.add_argument(
'--config-only', action='store_true', help='Show JS dist config only')
arg_parser.add_argument(
'suites',
nargs='*',
help=
'Benchmark suites to run - supported arguments: RenderComponentThroughput, TTI'
)
args = arg_parser.parse_args()
if not any((args.all, args.config_only)) and len(args.suites) == 0:
arg_parser.print_help()
sys.exit(1)
return args
class RenderComponentThroughputSuite:
def run(self, jsc_apk_install_kwargs, v8_apk_install_kwargs, hermes_apk_install_kwargs):
logger.info(h1('RenderComponentThroughput Suite'))
ApkTool.reinstall(**jsc_apk_install_kwargs)
ApkTool.reinstall(**v8_apk_install_kwargs)
ApkTool.reinstall(**hermes_apk_install_kwargs)
logger.info(h2('RenderComponentThroughput 10s'))
logger.info('jsc {}'.format(
RenderComponentThroughput('jsc', 10000).run_with_average(3)))
logger.info('v8 {}'.format(
RenderComponentThroughput('v8', 10000).run_with_average(3)))
logger.info('hermes {}'.format(
RenderComponentThroughput('v8', 10000).run_with_average(3)))
logger.info(h2('RenderComponentThroughput 60s'))
logger.info('jsc {}'.format(
RenderComponentThroughput('jsc', 60000).run_with_average(3)))
logger.info('v8 {}'.format(
RenderComponentThroughput('v8', 60000).run_with_average(3)))
logger.info('hermes {}'.format(
RenderComponentThroughput('v8', 60000).run_with_average(3)))
logger.info(h2('RenderComponentThroughput 180s'))
logger.info('jsc {}'.format(
RenderComponentThroughput('jsc', 180000).run_with_average(3)))
logger.info('v8 {}'.format(
RenderComponentThroughput('v8', 180000).run_with_average(3)))
logger.info('hermes {}'.format(
RenderComponentThroughput('v8', 180000).run_with_average(3)))
class TTISuite:
def run(self, jsc_apk_install_kwargs, v8_apk_install_kwargs, hermes_apk_install_kwargs):
logger.info(h1('TTI Suite'))
logger.info(h2('TTI 3MiB'))
size = 1024 * 1024 * 3
TTI('jsc', size).run(jsc_apk_install_kwargs)
TTI('v8', size).run(v8_apk_install_kwargs)
TTI('hermes', size).run(v8_apk_install_kwargs)
logger.info(h2('TTI 10MiB'))
size = 1024 * 1024 * 10
TTI('jsc', size).run(jsc_apk_install_kwargs)
TTI('v8', size).run(v8_apk_install_kwargs)
TTI('hermes', size).run(v8_apk_install_kwargs)
logger.info(h2('TTI 15MiB'))
size = 1024 * 1024 * 15
TTI('jsc', size).run(jsc_apk_install_kwargs)
TTI('v8', size).run(v8_apk_install_kwargs)
TTI('hermes', size).run(v8_apk_install_kwargs)
def main():
args = parse_args()
setup_logger(logger, args.verbose)
suites = []
if args.all or 'RenderComponentThroughput' in args.suites:
suites.append(RenderComponentThroughputSuite())
if args.all or 'TTI' in args.suites:
suites.append(TTISuite())
# {armeabi-v7a, arm64-v8a, x86, x86_64}
abi = 'armeabi-v7a'
# abi = 'x86'
jsc_dist_manager = JSDistManager('jsc_official_245459')
jsc_dist_manager.prepare()
v8_dist_manager = JSDistManager('v8_751')
v8_dist_manager.prepare()
logger.info(h1('Config'))
show_configs(abi, jsc_dist_manager, v8_dist_manager)
jsc_apk_install_kwargs = {
'app_id': 'jsc',
'maven_repo_prop': 'JSC_DIST_REPO=' + jsc_dist_manager.prepare(),
'abi': abi,
'verbose': args.verbose,
}
v8_apk_install_kwargs = {
'app_id': 'v8',
'maven_repo_prop': 'V8_DIST_REPO=' + v8_dist_manager.prepare(),
'abi': abi,
'verbose': args.verbose,
}
hermes_apk_install_kwargs = {
'app_id': 'hermes',
'maven_repo_prop': 'V8_DIST_REPO=' + v8_dist_manager.prepare(),
'abi': abi,
'verbose': args.verbose,
}
for suite in suites:
suite.run(jsc_apk_install_kwargs, v8_apk_install_kwargs, hermes_apk_install_kwargs)
return 0
if __name__ == '__main__':
os.chdir(ROOT_DIR)
main()