blob: e56ef5a55192acd983972fdc78a1a102e210a1a7 [file] [log] [blame]
Josh Gaofd3fd932018-04-06 17:55:24 -07001#!/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
18import os
19import statistics
20import time
21
22import adb
23
24def 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
32def 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
40def 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
49def harmonic_mean(xs):
50 return 1.0 / statistics.mean([1.0 / x for x in xs])
51
52def 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
59def benchmark_push(device=None, file_size_mb=100):
60 if device == None:
61 device = adb.get_device()
62
Josh Gaofd3fd932018-04-06 17:55:24 -070063 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 Gao6c060cf2018-10-12 18:01:27 -070070 for _ in range(0, 10):
Josh Gaofd3fd932018-04-06 17:55:24 -070071 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
78def benchmark_pull(device=None, file_size_mb=100):
79 if device == None:
80 device = adb.get_device()
81
Josh Gaofd3fd932018-04-06 17:55:24 -070082 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 Gao6c060cf2018-10-12 18:01:27 -070088 for _ in range(0, 10):
Josh Gaofd3fd932018-04-06 17:55:24 -070089 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
96def benchmark_shell(device=None, file_size_mb=100):
97 if device == None:
98 device = adb.get_device()
99
Josh Gaofd3fd932018-04-06 17:55:24 -0700100 speeds = list()
Josh Gao6c060cf2018-10-12 18:01:27 -0700101 for _ in range(0, 10):
Josh Gaofd3fd932018-04-06 17:55:24 -0700102 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
110def main():
Josh Gao6c060cf2018-10-12 18:01:27 -0700111 device = adb.get_device()
112 unlock(device)
113 benchmark_push(device)
114 benchmark_pull(device)
Josh Gaofd3fd932018-04-06 17:55:24 -0700115
116if __name__ == "__main__":
117 main()