1#!/bin/sh
2# A utility script that runs benchmark on Android device
3#
4# Note: Only one Android device can be connected when running this script
5#
6# Example usage:
7#   $ cd system/bt
8#   $ ./test/run_benchmarks.sh bluetooth_benchmark_example
9
10known_benchmarks=(
11  bluetooth_benchmark_thread_performance
12  bluetooth_benchmark_timer_performance
13)
14
15usage() {
16  binary="$(basename "$0")"
17  echo "Usage: ${binary} --help"
18  echo "       ${binary} [-i <iterations>] [-s <specific device>] [--all] [<benchmark name>[.<filter>] ...] [--<arg> ...]"
19  echo
20  echo "Unknown long arguments are passed to the benchmark."
21  echo
22  echo "Known benchmark names:"
23
24  for name in "${known_benchmarks[@]}"
25  do
26    echo "    ${name}"
27  done
28}
29
30iterations=1
31device=
32benchmarks=()
33benchmark_args=()
34while [ $# -gt 0 ]
35do
36  case "$1" in
37    -h|--help)
38      usage
39      exit 0
40      ;;
41    -i)
42      shift
43      if [ $# -eq 0 ]; then
44        echo "error: number of iterations expected" 1>&2
45        usage
46        exit 2
47      fi
48      iterations=$(( $1 ))
49      shift
50      ;;
51    -s)
52      shift
53      if [ $# -eq 0 ]; then
54        echo "error: no device specified" 1>&2
55        usage
56        exit 2
57      fi
58      device="$1"
59      shift
60      ;;
61    --all)
62      benchmarks+=( "${known_benchmarks[@]}" )
63      shift
64      ;;
65    --*)
66      benchmark_args+=( "$1" )
67      shift
68      ;;
69    *)
70      benchmarks+=( "$1" )
71      shift
72      ;;
73  esac
74done
75
76if [ "${#benchmarks[@]}" -eq 0 ]; then
77  benchmarks+=( "${known_benchmarks[@]}" )
78fi
79
80adb=( "adb" )
81if [ -n "${device}" ]; then
82  adb+=( "-s" "${device}" )
83fi
84
85source ${ANDROID_BUILD_TOP}/build/envsetup.sh
86target_arch=$(gettargetarch)
87
88failed_benchmarks=()
89for spec in "${benchmarks[@]}"
90do
91  name="${spec%%.*}"
92  if [[ $target_arch == *"64"* ]]; then
93    binary="/data/benchmarktest64/${name}/${name}"
94  else
95    binary="/data/benchmarktest/${name}/${name}"
96  fi
97
98  push_command=( "${adb[@]}" push {"${ANDROID_PRODUCT_OUT}",}"${binary}" )
99  benchmark_command=( "${adb[@]}" shell "${binary}" )
100  if [ "${name}" != "${spec}" ]; then
101    filter="${spec#*.}"
102    benchmark_command+=( "--benchmark_filter=${filter}" )
103  fi
104  benchmark_command+=( "${benchmark_args[@]}" )
105
106  echo "--- ${name} ---"
107  echo "pushing..."
108  "${push_command[@]}"
109  echo "running..."
110  failed_count=0
111  for i in $(seq 1 ${iterations})
112  do
113    "${benchmark_command[@]}" || failed_count=$(( $failed_count + 1 ))
114  done
115
116  if [ $failed_count != 0 ]; then
117    failed_benchmarks+=( "${name} ${failed_count}/${iterations}" )
118  fi
119done
120
121if [ "${#failed_benchmarks[@]}" -ne 0 ]; then
122  for failed_benchmark in "${failed_benchmarks[@]}"
123  do
124    echo "!!! FAILED TEST: ${failed_benchmark} !!!"
125  done
126  exit 1
127fi
128
129exit 0
130