| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2018 The Android Open Source Project | 
|  | 4 | # | 
|  | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | # you may not use this file except in compliance with the License. | 
|  | 7 | # You may obtain a copy of the License at | 
|  | 8 | # | 
|  | 9 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | # Unless required by applicable law or agreed to in writing, software | 
|  | 12 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | # See the License for the specific language governing permissions and | 
|  | 15 | # limitations under the License. | 
|  | 16 | # | 
|  | 17 |  | 
|  | 18 | import os | 
|  | 19 | import statistics | 
| Josh Gao | b416f22 | 2019-02-28 15:36:48 -0800 | [diff] [blame] | 20 | import subprocess | 
|  | 21 | import tempfile | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 22 | import time | 
|  | 23 |  | 
|  | 24 | import adb | 
|  | 25 |  | 
|  | 26 | def lock_min(device): | 
|  | 27 | device.shell_nocheck([""" | 
|  | 28 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 29 | echo userspace > $x/scaling_governor | 
|  | 30 | cat $x/scaling_min_freq > $x/scaling_setspeed | 
|  | 31 | done | 
|  | 32 | """]) | 
|  | 33 |  | 
|  | 34 | def lock_max(device): | 
|  | 35 | device.shell_nocheck([""" | 
|  | 36 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 37 | echo userspace > $x/scaling_governor | 
|  | 38 | cat $x/scaling_max_freq > $x/scaling_setspeed | 
|  | 39 | done | 
|  | 40 | """]) | 
|  | 41 |  | 
|  | 42 | def unlock(device): | 
|  | 43 | device.shell_nocheck([""" | 
|  | 44 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 45 | echo ondemand > $x/scaling_governor | 
|  | 46 | echo sched > $x/scaling_governor | 
|  | 47 | echo schedutil > $x/scaling_governor | 
|  | 48 | done | 
|  | 49 | """]) | 
|  | 50 |  | 
|  | 51 | def harmonic_mean(xs): | 
|  | 52 | return 1.0 / statistics.mean([1.0 / x for x in xs]) | 
|  | 53 |  | 
|  | 54 | def analyze(name, speeds): | 
|  | 55 | median = statistics.median(speeds) | 
|  | 56 | mean = harmonic_mean(speeds) | 
|  | 57 | stddev = statistics.stdev(speeds) | 
|  | 58 | msg = "%s: %d runs: median %.2f MiB/s, mean %.2f MiB/s, stddev: %.2f MiB/s" | 
|  | 59 | print(msg % (name, len(speeds), median, mean, stddev)) | 
|  | 60 |  | 
| Josh Gao | b416f22 | 2019-02-28 15:36:48 -0800 | [diff] [blame] | 61 | def benchmark_sink(device=None, size_mb=100): | 
|  | 62 | if device == None: | 
|  | 63 | device = adb.get_device() | 
|  | 64 |  | 
|  | 65 | speeds = list() | 
|  | 66 | cmd = device.adb_cmd + ["raw", "sink:%d" % (size_mb * 1024 * 1024)] | 
|  | 67 |  | 
|  | 68 | with tempfile.TemporaryFile() as tmpfile: | 
|  | 69 | tmpfile.truncate(size_mb * 1024 * 1024) | 
|  | 70 |  | 
|  | 71 | for _ in range(0, 10): | 
|  | 72 | tmpfile.seek(0) | 
|  | 73 | begin = time.time() | 
|  | 74 | subprocess.check_call(cmd, stdin=tmpfile) | 
|  | 75 | end = time.time() | 
|  | 76 | speeds.append(size_mb / float(end - begin)) | 
|  | 77 |  | 
|  | 78 | analyze("sink %dMiB" % size_mb, speeds) | 
|  | 79 |  | 
|  | 80 | def benchmark_source(device=None, size_mb=100): | 
|  | 81 | if device == None: | 
|  | 82 | device = adb.get_device() | 
|  | 83 |  | 
|  | 84 | speeds = list() | 
|  | 85 | cmd = device.adb_cmd + ["raw", "source:%d" % (size_mb * 1024 * 1024)] | 
|  | 86 |  | 
|  | 87 | with open(os.devnull, 'w') as devnull: | 
|  | 88 | for _ in range(0, 10): | 
|  | 89 | begin = time.time() | 
|  | 90 | subprocess.check_call(cmd, stdout=devnull) | 
|  | 91 | end = time.time() | 
|  | 92 | speeds.append(size_mb / float(end - begin)) | 
|  | 93 |  | 
|  | 94 | analyze("source %dMiB" % size_mb, speeds) | 
|  | 95 |  | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 96 | def benchmark_push(device=None, file_size_mb=100): | 
|  | 97 | if device == None: | 
|  | 98 | device = adb.get_device() | 
|  | 99 |  | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 100 | remote_path = "/dev/null" | 
|  | 101 | local_path = "/tmp/adb_benchmark_temp" | 
|  | 102 |  | 
|  | 103 | with open(local_path, "wb") as f: | 
|  | 104 | f.truncate(file_size_mb * 1024 * 1024) | 
|  | 105 |  | 
|  | 106 | speeds = list() | 
| Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame] | 107 | for _ in range(0, 10): | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 108 | begin = time.time() | 
|  | 109 | device.push(local=local_path, remote=remote_path) | 
|  | 110 | end = time.time() | 
|  | 111 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 112 |  | 
|  | 113 | analyze("push %dMiB" % file_size_mb, speeds) | 
|  | 114 |  | 
|  | 115 | def benchmark_pull(device=None, file_size_mb=100): | 
|  | 116 | if device == None: | 
|  | 117 | device = adb.get_device() | 
|  | 118 |  | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 119 | remote_path = "/data/local/tmp/adb_benchmark_temp" | 
|  | 120 | local_path = "/tmp/adb_benchmark_temp" | 
|  | 121 |  | 
|  | 122 | device.shell(["dd", "if=/dev/zero", "of=" + remote_path, "bs=1m", | 
|  | 123 | "count=" + str(file_size_mb)]) | 
|  | 124 | speeds = list() | 
| Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame] | 125 | for _ in range(0, 10): | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 126 | begin = time.time() | 
|  | 127 | device.pull(remote=remote_path, local=local_path) | 
|  | 128 | end = time.time() | 
|  | 129 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 130 |  | 
|  | 131 | analyze("pull %dMiB" % file_size_mb, speeds) | 
|  | 132 |  | 
|  | 133 | def benchmark_shell(device=None, file_size_mb=100): | 
|  | 134 | if device == None: | 
|  | 135 | device = adb.get_device() | 
|  | 136 |  | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 137 | speeds = list() | 
| Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame] | 138 | for _ in range(0, 10): | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 139 | begin = time.time() | 
|  | 140 | device.shell(["dd", "if=/dev/zero", "bs=1m", | 
|  | 141 | "count=" + str(file_size_mb)]) | 
|  | 142 | end = time.time() | 
|  | 143 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 144 |  | 
|  | 145 | analyze("shell %dMiB" % file_size_mb, speeds) | 
|  | 146 |  | 
|  | 147 | def main(): | 
| Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame] | 148 | device = adb.get_device() | 
|  | 149 | unlock(device) | 
| Josh Gao | b416f22 | 2019-02-28 15:36:48 -0800 | [diff] [blame] | 150 | benchmark_sink(device) | 
|  | 151 | benchmark_source(device) | 
| Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame] | 152 | benchmark_push(device) | 
|  | 153 | benchmark_pull(device) | 
| Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | if __name__ == "__main__": | 
|  | 156 | main() |