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 | |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 63 | remote_path = "/dev/null" |
| 64 | local_path = "/tmp/adb_benchmark_temp" |
| 65 | |
| 66 | with open(local_path, "wb") as f: |
| 67 | f.truncate(file_size_mb * 1024 * 1024) |
| 68 | |
| 69 | speeds = list() |
Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame^] | 70 | for _ in range(0, 10): |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 71 | begin = time.time() |
| 72 | device.push(local=local_path, remote=remote_path) |
| 73 | end = time.time() |
| 74 | speeds.append(file_size_mb / float(end - begin)) |
| 75 | |
| 76 | analyze("push %dMiB" % file_size_mb, speeds) |
| 77 | |
| 78 | def benchmark_pull(device=None, file_size_mb=100): |
| 79 | if device == None: |
| 80 | device = adb.get_device() |
| 81 | |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 82 | remote_path = "/data/local/tmp/adb_benchmark_temp" |
| 83 | local_path = "/tmp/adb_benchmark_temp" |
| 84 | |
| 85 | device.shell(["dd", "if=/dev/zero", "of=" + remote_path, "bs=1m", |
| 86 | "count=" + str(file_size_mb)]) |
| 87 | speeds = list() |
Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame^] | 88 | for _ in range(0, 10): |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 89 | begin = time.time() |
| 90 | device.pull(remote=remote_path, local=local_path) |
| 91 | end = time.time() |
| 92 | speeds.append(file_size_mb / float(end - begin)) |
| 93 | |
| 94 | analyze("pull %dMiB" % file_size_mb, speeds) |
| 95 | |
| 96 | def benchmark_shell(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 | speeds = list() |
Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame^] | 101 | for _ in range(0, 10): |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 102 | begin = time.time() |
| 103 | device.shell(["dd", "if=/dev/zero", "bs=1m", |
| 104 | "count=" + str(file_size_mb)]) |
| 105 | end = time.time() |
| 106 | speeds.append(file_size_mb / float(end - begin)) |
| 107 | |
| 108 | analyze("shell %dMiB" % file_size_mb, speeds) |
| 109 | |
| 110 | def main(): |
Josh Gao | 6c060cf | 2018-10-12 18:01:27 -0700 | [diff] [blame^] | 111 | device = adb.get_device() |
| 112 | unlock(device) |
| 113 | benchmark_push(device) |
| 114 | benchmark_pull(device) |
Josh Gao | fd3fd93 | 2018-04-06 17:55:24 -0700 | [diff] [blame] | 115 | |
| 116 | if __name__ == "__main__": |
| 117 | main() |