forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_flutter_android.sh
More file actions
executable file
·272 lines (243 loc) · 7.59 KB
/
Copy pathprofile_flutter_android.sh
File metadata and controls
executable file
·272 lines (243 loc) · 7.59 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env bash
set -euo pipefail
# Full automated Flutter Android profiling script
# - Builds profile APK
# - Starts screen recording (scrcpy)
# - Installs and launches app
# - Waits for UI state (via uiautomator)
# - Measures CPU
# - Stops recording
usage() {
cat <<'USAGE'
Usage: profile_flutter_android.sh [options]
Options:
-p, --package <name> Android package name (default: com.friend.ios.dev)
-f, --flavor <flavor> Flutter build flavor (default: dev)
-s, --session <name> Session name for recording (default: profile_<timestamp>)
-w, --wait-for <text> UI text to wait for via Semantics label (default: device_state_Listening)
-d, --delay <seconds> Delay after UI state before measuring (default: 10)
-n, --samples <count> Number of CPU samples (default: 15)
-i, --interval <secs> Interval between samples (default: 2)
-o, --output <dir> Output directory for recordings (default: ~/Downloads)
--no-build Skip building APK (use existing build)
--no-record Skip screen recording
-h, --help Show this help message
Examples:
# Full automated profiling with defaults
./profile_flutter_android.sh
# Skip build, custom session name
./profile_flutter_android.sh --no-build -s "test_v2"
# Custom wait condition
./profile_flutter_android.sh -w "device_state_Connected" -d 5
USAGE
}
# Defaults
PACKAGE="com.friend.ios.dev"
FLAVOR="dev"
SESSION=""
WAIT_FOR="device_state_Listening"
DELAY_AFTER_STATE=10
SAMPLES=15
INTERVAL=2
OUTPUT_DIR="$HOME/Downloads"
DO_BUILD=true
DO_RECORD=true
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--package) PACKAGE="$2"; shift 2 ;;
-f|--flavor) FLAVOR="$2"; shift 2 ;;
-s|--session) SESSION="$2"; shift 2 ;;
-w|--wait-for) WAIT_FOR="$2"; shift 2 ;;
-d|--delay) DELAY_AFTER_STATE="$2"; shift 2 ;;
-n|--samples) SAMPLES="$2"; shift 2 ;;
-i|--interval) INTERVAL="$2"; shift 2 ;;
-o|--output) OUTPUT_DIR="$2"; shift 2 ;;
--no-build) DO_BUILD=false; shift ;;
--no-record) DO_RECORD=false; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1" >&2; usage; exit 2 ;;
esac
done
# Generate session ID if not provided
if [[ -z "$SESSION" ]]; then
SESSION="profile_$(date +%Y%m%d_%H%M%S)"
fi
echo "=== Flutter Android Profiling ==="
echo "Package: $PACKAGE"
echo "Session: $SESSION"
echo "Wait for: $WAIT_FOR"
echo ""
# Check prerequisites
if ! command -v adb &>/dev/null; then
echo "Error: adb not found in PATH" >&2
exit 1
fi
DEVICE=$(adb devices | awk 'NR>1 && $2=="device" {print $1; exit}')
if [[ -z "$DEVICE" ]]; then
echo "Error: No Android device connected" >&2
exit 1
fi
echo "Device: $DEVICE"
# Find app directory (look for pubspec.yaml)
APP_DIR="."
if [[ -f "pubspec.yaml" ]]; then
APP_DIR="."
elif [[ -f "app/pubspec.yaml" ]]; then
APP_DIR="app"
elif [[ -f "../pubspec.yaml" ]]; then
APP_DIR=".."
else
echo "Error: Cannot find Flutter app directory (pubspec.yaml)" >&2
exit 1
fi
# Step 1: Build APK
if [[ "$DO_BUILD" == true ]]; then
echo ""
echo "=== Building profile APK ==="
pushd "$APP_DIR" > /dev/null
case "$FLAVOR" in
dev)
PROFILE='local_dev'
ENV_FILE='.dev.env'
ANDROID_DEV_HOST="${OMI_ANDROID_DEV_HOST:-${OMI_DEV_HOST:-10.0.2.2}}"
;;
prod)
PROFILE='mobile_beta'
ENV_FILE='.env'
ANDROID_DEV_HOST=''
;;
*)
echo "Error: unsupported mobile flavor '$FLAVOR' (expected dev or prod)" >&2
exit 1
;;
esac
scripts/validate_mobile_build_config.sh \
--flavor "$FLAVOR" \
--profile "$PROFILE" \
--env-file "$ENV_FILE"
API_BASE_URL="$(awk -F= '$1 == "API_BASE_URL" { value = $2 } END { print value }' "$ENV_FILE")"
flutter_args=(
--flavor "$FLAVOR"
--profile
"--dart-define=OMI_APP_PROFILE=$PROFILE"
"--dart-define=OMI_API_BASE_URL=$API_BASE_URL"
)
if [[ -n "$ANDROID_DEV_HOST" ]]; then
flutter_args+=("--dart-define=OMI_FIREBASE_AUTH_EMULATOR_HOST=$ANDROID_DEV_HOST")
fi
flutter build apk "${flutter_args[@]}"
popd > /dev/null
fi
APK_PATH="$APP_DIR/build/app/outputs/flutter-apk/app-${FLAVOR}-profile.apk"
if [[ ! -f "$APK_PATH" ]]; then
echo "Error: APK not found at $APK_PATH" >&2
exit 1
fi
# Step 2: Start screen recording
SCRCPY_PID=""
RECORDING_FILE=""
if [[ "$DO_RECORD" == true ]]; then
if command -v scrcpy &>/dev/null; then
echo ""
echo "=== Starting screen recording ==="
RECORDING_FILE="$OUTPUT_DIR/profiling_${SESSION}.mp4"
scrcpy --no-audio -r "$RECORDING_FILE" &
SCRCPY_PID=$!
sleep 2
echo "Recording to: $RECORDING_FILE (PID: $SCRCPY_PID)"
else
echo "Warning: scrcpy not found, skipping screen recording"
DO_RECORD=false
fi
fi
# Cleanup function
cleanup() {
if [[ -n "$SCRCPY_PID" ]] && kill -0 "$SCRCPY_PID" 2>/dev/null; then
echo ""
echo "=== Stopping recording ==="
kill "$SCRCPY_PID" 2>/dev/null || true
wait "$SCRCPY_PID" 2>/dev/null || true
echo "Recording saved to: $RECORDING_FILE"
fi
}
trap cleanup EXIT
# Step 3: Install APK
echo ""
echo "=== Installing APK ==="
adb -s "$DEVICE" install -r "$APK_PATH"
# Step 4: Launch app
echo ""
echo "=== Launching app ==="
# Extract main activity from package
ACTIVITY=$(adb -s "$DEVICE" shell pm dump "$PACKAGE" | grep -A1 "android.intent.action.MAIN" | grep -oP '[\w.]+/[\w.]+' | head -1 || echo "")
if [[ -z "$ACTIVITY" ]]; then
# Fallback: try common activity name
ACTIVITY="$PACKAGE/.MainActivity"
fi
adb -s "$DEVICE" shell am start -n "$ACTIVITY" || adb -s "$DEVICE" shell monkey -p "$PACKAGE" -c android.intent.category.LAUNCHER 1
# Step 5: Wait for UI state
echo ""
echo "=== Waiting for UI state: $WAIT_FOR ==="
FOUND=false
for i in {1..60}; do
if adb -s "$DEVICE" shell uiautomator dump /sdcard/ui.xml 2>/dev/null && \
adb -s "$DEVICE" shell cat /sdcard/ui.xml 2>/dev/null | grep -q "$WAIT_FOR"; then
echo "Found '$WAIT_FOR' after $i seconds"
FOUND=true
break
fi
printf "."
sleep 1
done
echo ""
if [[ "$FOUND" != true ]]; then
echo "Warning: Timeout waiting for '$WAIT_FOR' - continuing anyway"
fi
# Step 6: Wait additional delay
echo "Waiting ${DELAY_AFTER_STATE}s for stabilization..."
sleep "$DELAY_AFTER_STATE"
# Step 7: Measure CPU
echo ""
echo "=== Measuring CPU ==="
# Use the measure script if available, otherwise inline
MEASURE_SCRIPT="$APP_DIR/scripts/measure_cpu_android.sh"
if [[ -f "$MEASURE_SCRIPT" ]]; then
"$MEASURE_SCRIPT" -p "$PACKAGE" -s "$DEVICE" -n "$SAMPLES" -d "$INTERVAL"
else
# Inline CPU measurement
values=()
echo "Sampling CPU for $PACKAGE ($SAMPLES samples, ${INTERVAL}s interval)..."
for ((i=1; i<=SAMPLES; i++)); do
line=$(adb -s "$DEVICE" shell "top -b -n 1 -d 1" | grep -m 1 "$PACKAGE" | head -1 || true)
cpu=$(echo "$line" | awk '{for (j=1; j<=NF; j++) if ($j ~ /^[0-9.]+%$/) {gsub(/%/,"",$j); print $j; exit}}')
if [[ -n "$cpu" ]]; then
values+=("$cpu")
echo "Sample $i: $cpu%"
else
echo "Sample $i: (not found)"
fi
if (( i < SAMPLES )); then
sleep "$INTERVAL"
fi
done
# Calculate median
count=${#values[@]}
if (( count > 0 )); then
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})"
else
echo "Error: No CPU samples collected"
fi
fi
echo ""
echo "=== Profiling complete ==="
echo "Session: $SESSION"
if [[ -n "$RECORDING_FILE" ]] && [[ -f "$RECORDING_FILE" ]]; then
echo "Recording: $RECORDING_FILE"
fi