blob: 86adbe2d558b9d59a31a8b17b2756870355f6ba8 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Rico Wind9d70f612018-08-31 09:17:43 +02002# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6# Script for running kotlin based benchmarks
7
8import optparse
9import os
10import subprocess
11import sys
12import toolhelper
13import utils
14
15
16BENCHMARK_ROOT = os.path.join(utils.REPO_ROOT, 'third_party', 'benchmarks',
17 'kotlin-benches')
18
19BENCHMARK_PATTERN = '{benchmark}/kotlin/perf/build/libs/perf-1.0-BENCH.jar'
20BENCHMARK_MAIN_CLASS = 'com.android.kt.bms.cli.Runner'
21ART = os.path.join(utils.TOOLS_DIR, 'linux', 'art', 'bin', 'art')
22
23PROGUARD_CONF = """
24# From Android rules
25-keepclasseswithmembers public class * {
26 public static void main(java.lang.String[]);
27}
28# Disable obfuscation to only focus on shrinking
29-dontobfuscate
30# Once we're ready for optimization, we might want to relax access modifiers.
31-allowaccessmodification
32"""
33
34DEVICE_TEMP='/data/local/temp/bench'
35
36
37def parse_options():
38 result = optparse.OptionParser()
39 result.add_option('--api',
40 help='Android api level',
41 default='26',
42 choices=['21', '22', '23', '24', '25', '26'])
43 result.add_option('--benchmark',
44 help='The benchmark to run',
45 default='rgx',
46 choices=['rgx', 'deltablue', 'sta', 'empty'])
Rico Wind9d70f612018-08-31 09:17:43 +020047 result.add_option('--use-device',
48 help='Run the benchmark on an attaced device',
49 default=False, action='store_true')
50 return result.parse_args()
51
52
53def get_jar_for_benchmark(benchmark):
54 return os.path.join(BENCHMARK_ROOT,
55 BENCHMARK_PATTERN.format(benchmark=benchmark))
56
57def run_art(dex):
58 command = ['bash', ART, '-cp', dex, BENCHMARK_MAIN_CLASS]
59 utils.PrintCmd(command)
60 benchmark_output = subprocess.check_output(command)
61 return get_result(benchmark_output)
62
63def adb(args):
64 command = ['adb'] + args
65 utils.PrintCmd(command)
66 return subprocess.check_output(['adb'] + args)
67
68def get_result(output):
69 # There is a lot of debug output, with the actual results being in the line with:
70 # RESULTS,KtBench,KtBench,15719
71 # structure.
72 for result in [s for s in output.splitlines() if s.startswith('RESULTS')]:
73 return s.split('RESULTS,KtBench,KtBench,')[1]
74
75def run_art_device(dex):
76 adb(['wait-for-device', 'root'])
77 device_dst = os.path.join(DEVICE_TEMP, os.path.basename(dex))
78 adb(['push', dex, device_dst])
79 benchmark_output = adb(['shell', 'dalvikvm', '-cp', device_dst, BENCHMARK_MAIN_CLASS])
80 return get_result(benchmark_output)
81
82def Main():
83 (options, args) = parse_options()
Rico Wind13152512018-09-12 12:56:50 +020084 with utils.TempDir() as temp:
85 dex_path = os.path.join(temp, "classes.jar")
86 proguard_conf = os.path.join(temp, 'proguard.conf')
87 with open(proguard_conf, 'w') as f:
88 f.write(PROGUARD_CONF)
89 benchmark_jar = get_jar_for_benchmark(options.benchmark)
90 r8_args = [
91 '--lib', utils.get_android_jar(26), # Only works with api 26
92 '--output', dex_path,
93 '--pg-conf', proguard_conf,
94 '--min-api', str(options.api),
95 benchmark_jar
96 ]
Morten Krogh-Jespersen254805e2022-06-03 09:32:42 +020097 toolhelper.run('r8', r8_args, True)
Rico Wind13152512018-09-12 12:56:50 +020098 if options.use_device:
99 result = run_art_device(dex_path)
100 else:
101 result = run_art(dex_path)
102 print('Kotlin_{}(RunTimeRaw): {} ms'.format(options.benchmark, result))
Rico Wind9d70f612018-08-31 09:17:43 +0200103
104if __name__ == '__main__':
105 sys.exit(Main())