forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasure_cpu_android.sh
More file actions
executable file
·154 lines (128 loc) · 3.97 KB
/
Copy pathmeasure_cpu_android.sh
File metadata and controls
executable file
·154 lines (128 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
# Android CPU Measurement Script for Omi App
#
# Usage:
# ./measure_cpu_android.sh -p <package> [-s <device>] [-n <samples>] [-d <delay>] [-o <csv>]
#
# Legacy usage (kept for backward compatibility):
# ./measure_cpu_android.sh [duration_seconds] [output_name]
#
# Examples:
# ./measure_cpu_android.sh -p com.friend.ios.dev -n 15 -d 2 -o /tmp/omi_cpu.csv
# ./measure_cpu_android.sh 60 "baseline"
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: measure_cpu_android.sh -p <package> [-s <device>] [-n <samples>] [-d <delay>] [-o <csv>]
Options:
-p Android package name (default: env PACKAGE or com.friend.ios.dev)
-s Device ID (default: first connected device)
-n Number of samples (default: 15)
-d Delay between samples in seconds (default: 2)
-o Write CSV output (sample,cpu) to this file
USAGE
}
PACKAGE="${PACKAGE_NAME:-${PACKAGE:-}}"
PACKAGE_SET=false
DEVICE="${DEVICE_ID:-}"
SAMPLES=15
DELAY=2
OUTFILE=""
LEGACY_MODE=false
while getopts ":p:s:n:d:o:h" opt; do
case "$opt" in
p) PACKAGE="$OPTARG"; PACKAGE_SET=true ;;
s) DEVICE="$OPTARG" ;;
n) SAMPLES="$OPTARG" ;;
d) DELAY="$OPTARG" ;;
o) OUTFILE="$OPTARG" ;;
h) usage; exit 0 ;;
\?) echo "Unknown option: -$OPTARG" >&2; usage; exit 2 ;;
:) echo "Missing argument for -$OPTARG" >&2; usage; exit 2 ;;
esac
done
shift $((OPTIND - 1))
if ! $PACKAGE_SET && [[ $# -le 2 ]]; then
# Legacy positional args: duration_seconds output_name
LEGACY_MODE=true
DURATION=${1:-60}
OUTPUT_NAME=${2:-"measurement"}
if ! [[ "$DURATION" =~ ^[0-9]+$ ]]; then
echo "Error: invalid duration '$DURATION'" >&2
usage
exit 2
fi
SAMPLES=$((DURATION / 2))
if (( SAMPLES < 1 )); then
SAMPLES=1
fi
if [[ -z "$OUTFILE" ]]; then
OUTPUT_DIR="/tmp/omi_cpu_profiling"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
mkdir -p "$OUTPUT_DIR"
OUTFILE="$OUTPUT_DIR/${OUTPUT_NAME}_${TIMESTAMP}.csv"
fi
elif [[ $# -gt 0 ]]; then
echo "Error: unexpected extra arguments: $*" >&2
usage
exit 2
fi
if [[ -z "$PACKAGE" ]]; then
PACKAGE="com.friend.ios.dev"
echo "Warning: no package provided; defaulting to $PACKAGE" >&2
fi
if [[ -z "$DEVICE" ]]; then
DEVICE=$(adb devices | awk 'NR>1 && $2=="device" {print $1; exit}')
fi
if [[ -z "$DEVICE" ]]; then
echo "Error: no connected Android devices found." >&2
exit 2
fi
if ! adb -s "$DEVICE" get-state >/dev/null 2>&1; then
echo "Error: device '$DEVICE' not available." >&2
exit 2
fi
# Use standard top output without -o flag for consistent CPU% parsing
TOP_ARGS="-b -n 1 -d 1"
values=()
if [[ -n "$OUTFILE" ]]; then
mkdir -p "$(dirname "$OUTFILE")"
echo "sample,cpu" > "$OUTFILE"
fi
echo "Sampling CPU for $PACKAGE on $DEVICE ($SAMPLES samples, ${DELAY}s delay)..."
for ((i=1; i<=SAMPLES; i++)); do
line=$(adb -s "$DEVICE" shell "top $TOP_ARGS" | grep -m 1 "$PACKAGE" | head -1 || true)
# Try format with % suffix first, then fall back to column 9 (standard top format)
cpu=$(echo "$line" | awk '{for (i=1; i<=NF; i++) if ($i ~ /^[0-9.]+%$/) {gsub(/%/,"",$i); print $i; exit}}')
if [[ -z "$cpu" ]]; then
# Fallback: extract column 9 which is CPU% in standard Android top output
cpu=$(echo "$line" | awk '{print $9}' | grep -E '^[0-9.]+$' || true)
fi
if [[ -z "$cpu" ]]; then
echo "Sample $i: missing (package not found or CPU column not parsed)" >&2
else
values+=("$cpu")
echo "Sample $i: $cpu%"
if [[ -n "$OUTFILE" ]]; then
echo "$i,$cpu" >> "$OUTFILE"
fi
fi
if (( i < SAMPLES )); then
sleep "$DELAY"
fi
done
count=${#values[@]}
if (( count == 0 )); then
echo "Error: no CPU samples collected." >&2
exit 1
fi
sorted=$(printf "%s\n" "${values[@]}" | sort -n)
if (( count % 2 == 1 )); then
median=$(printf "%s\n" "$sorted" | awk -v n="$count" 'NR==(n+1)/2 {print $1}')
else
median=$(printf "%s\n" "$sorted" | awk -v n="$count" 'NR==n/2 {a=$1} NR==n/2+1 {print (a+$1)/2}')
fi
echo "Median CPU: ${median}% (n=${count})"
if [[ -n "$OUTFILE" ]]; then
echo "Wrote CSV to $OUTFILE"
fi