| 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 | 
|  | 20 | import time | 
|  | 21 |  | 
|  | 22 | import adb | 
|  | 23 |  | 
|  | 24 | def lock_min(device): | 
|  | 25 | device.shell_nocheck([""" | 
|  | 26 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 27 | echo userspace > $x/scaling_governor | 
|  | 28 | cat $x/scaling_min_freq > $x/scaling_setspeed | 
|  | 29 | done | 
|  | 30 | """]) | 
|  | 31 |  | 
|  | 32 | def lock_max(device): | 
|  | 33 | device.shell_nocheck([""" | 
|  | 34 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 35 | echo userspace > $x/scaling_governor | 
|  | 36 | cat $x/scaling_max_freq > $x/scaling_setspeed | 
|  | 37 | done | 
|  | 38 | """]) | 
|  | 39 |  | 
|  | 40 | def unlock(device): | 
|  | 41 | device.shell_nocheck([""" | 
|  | 42 | for x in /sys/devices/system/cpu/cpu?/cpufreq; do | 
|  | 43 | echo ondemand > $x/scaling_governor | 
|  | 44 | echo sched > $x/scaling_governor | 
|  | 45 | echo schedutil > $x/scaling_governor | 
|  | 46 | done | 
|  | 47 | """]) | 
|  | 48 |  | 
|  | 49 | def harmonic_mean(xs): | 
|  | 50 | return 1.0 / statistics.mean([1.0 / x for x in xs]) | 
|  | 51 |  | 
|  | 52 | def analyze(name, speeds): | 
|  | 53 | median = statistics.median(speeds) | 
|  | 54 | mean = harmonic_mean(speeds) | 
|  | 55 | stddev = statistics.stdev(speeds) | 
|  | 56 | msg = "%s: %d runs: median %.2f MiB/s, mean %.2f MiB/s, stddev: %.2f MiB/s" | 
|  | 57 | print(msg % (name, len(speeds), median, mean, stddev)) | 
|  | 58 |  | 
|  | 59 | def benchmark_push(device=None, file_size_mb=100): | 
|  | 60 | if device == None: | 
|  | 61 | device = adb.get_device() | 
|  | 62 |  | 
|  | 63 | lock_max(device) | 
|  | 64 |  | 
|  | 65 | remote_path = "/dev/null" | 
|  | 66 | local_path = "/tmp/adb_benchmark_temp" | 
|  | 67 |  | 
|  | 68 | with open(local_path, "wb") as f: | 
|  | 69 | f.truncate(file_size_mb * 1024 * 1024) | 
|  | 70 |  | 
|  | 71 | speeds = list() | 
|  | 72 | for _ in range(0, 5): | 
|  | 73 | begin = time.time() | 
|  | 74 | device.push(local=local_path, remote=remote_path) | 
|  | 75 | end = time.time() | 
|  | 76 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 77 |  | 
|  | 78 | analyze("push %dMiB" % file_size_mb, speeds) | 
|  | 79 |  | 
|  | 80 | def benchmark_pull(device=None, file_size_mb=100): | 
|  | 81 | if device == None: | 
|  | 82 | device = adb.get_device() | 
|  | 83 |  | 
|  | 84 | lock_max(device) | 
|  | 85 |  | 
|  | 86 | remote_path = "/data/local/tmp/adb_benchmark_temp" | 
|  | 87 | local_path = "/tmp/adb_benchmark_temp" | 
|  | 88 |  | 
|  | 89 | device.shell(["dd", "if=/dev/zero", "of=" + remote_path, "bs=1m", | 
|  | 90 | "count=" + str(file_size_mb)]) | 
|  | 91 | speeds = list() | 
|  | 92 | for _ in range(0, 5): | 
|  | 93 | begin = time.time() | 
|  | 94 | device.pull(remote=remote_path, local=local_path) | 
|  | 95 | end = time.time() | 
|  | 96 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 97 |  | 
|  | 98 | analyze("pull %dMiB" % file_size_mb, speeds) | 
|  | 99 |  | 
|  | 100 | def benchmark_shell(device=None, file_size_mb=100): | 
|  | 101 | if device == None: | 
|  | 102 | device = adb.get_device() | 
|  | 103 |  | 
|  | 104 | lock_max(device) | 
|  | 105 |  | 
|  | 106 | speeds = list() | 
|  | 107 | for _ in range(0, 5): | 
|  | 108 | begin = time.time() | 
|  | 109 | device.shell(["dd", "if=/dev/zero", "bs=1m", | 
|  | 110 | "count=" + str(file_size_mb)]) | 
|  | 111 | end = time.time() | 
|  | 112 | speeds.append(file_size_mb / float(end - begin)) | 
|  | 113 |  | 
|  | 114 | analyze("shell %dMiB" % file_size_mb, speeds) | 
|  | 115 |  | 
|  | 116 | def main(): | 
|  | 117 | benchmark_pull() | 
|  | 118 |  | 
|  | 119 | if __name__ == "__main__": | 
|  | 120 | main() |