mirror of
https://github.com/BrendanGreenlee/openclaw-android-heartbeat.git
synced 2026-08-17 16:49:14 +00:00
Import official OpenClaw Android app (apps/android @ 71a59512ba476df3328cf485d84748121cf341f2)
Pristine fork source for PROJ-0088. v1 will turn this into a WebView web shell; the WebSocket node infrastructure stays intact for v2 heartbeat.
This commit is contained in:
commit
7a380c40ed
656 changed files with 209982 additions and 0 deletions
447
scripts/build-release-artifacts.ts
Normal file
447
scripts/build-release-artifacts.ts
Normal file
|
|
@ -0,0 +1,447 @@
|
|||
#!/usr/bin/env bun
|
||||
/**
|
||||
* Android release helper that builds signed release artifacts from the pinned
|
||||
* version metadata, verifies signatures, and writes SHA-256 checksum files.
|
||||
*/
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
accessSync,
|
||||
constants,
|
||||
copyFileSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { resolveAndroidVersion, syncAndroidVersioning } from "../../../scripts/lib/android-version.ts";
|
||||
|
||||
type ReleaseArtifact = {
|
||||
flavorName: "play" | "wear" | "third-party";
|
||||
kind: "aab" | "apk";
|
||||
gradleTask: string;
|
||||
sourcePath: string;
|
||||
};
|
||||
|
||||
type CliOptions = {
|
||||
artifact: "all" | ReleaseArtifact["flavorName"];
|
||||
dryRun: boolean;
|
||||
verifyApk?: string;
|
||||
};
|
||||
|
||||
export type AndroidBuildMetadata = {
|
||||
commit: string;
|
||||
timestamp: string;
|
||||
};
|
||||
|
||||
type ResolveAndroidBuildMetadataOptions = {
|
||||
env?: NodeJS.ProcessEnv;
|
||||
now?: () => Date;
|
||||
readGitCommit?: () => string;
|
||||
};
|
||||
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const androidDir = join(scriptDir, "..");
|
||||
const rootDir = join(androidDir, "..", "..");
|
||||
const releaseOutputDir = join(androidDir, "build", "release-artifacts");
|
||||
const releaseSigningManifestPath = join(androidDir, "Config", "ReleaseSigning.json");
|
||||
const fullGitCommitPattern = /^[a-f0-9]{40}$/u;
|
||||
const isoUtcTimestampPattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/u;
|
||||
|
||||
function normalizeFullGitCommit(raw: string): string {
|
||||
const commit = raw.trim().toLowerCase();
|
||||
if (!fullGitCommitPattern.test(commit)) {
|
||||
throw new Error("Android build metadata requires a full 40-character hexadecimal Git commit");
|
||||
}
|
||||
return commit;
|
||||
}
|
||||
|
||||
function normalizeIsoUtcTimestamp(raw: string): string {
|
||||
const timestamp = raw.trim();
|
||||
if (!isoUtcTimestampPattern.test(timestamp)) {
|
||||
throw new Error("OPENCLAW_BUILD_TIMESTAMP must be an ISO-8601 UTC timestamp");
|
||||
}
|
||||
|
||||
const parsed = new Date(timestamp);
|
||||
if (Number.isNaN(parsed.getTime())) {
|
||||
throw new Error("OPENCLAW_BUILD_TIMESTAMP must be an ISO-8601 UTC timestamp");
|
||||
}
|
||||
const normalized = parsed.toISOString();
|
||||
if (normalized.slice(0, 19) !== timestamp.slice(0, 19)) {
|
||||
throw new Error("OPENCLAW_BUILD_TIMESTAMP must be a valid ISO-8601 UTC timestamp");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function readRepositoryCommit(): string {
|
||||
try {
|
||||
return execFileSync("git", ["rev-parse", "HEAD"], {
|
||||
cwd: rootDir,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
}).trim();
|
||||
} catch {
|
||||
throw new Error("Unable to resolve the Android release Git commit");
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveAndroidBuildMetadata(
|
||||
options: ResolveAndroidBuildMetadataOptions = {},
|
||||
): AndroidBuildMetadata {
|
||||
const env = options.env ?? process.env;
|
||||
const explicitCommit = env.GIT_COMMIT?.trim() || env.GIT_SHA?.trim();
|
||||
let repositoryCommit: string | undefined;
|
||||
if (!explicitCommit) {
|
||||
try {
|
||||
repositoryCommit = (options.readGitCommit ?? readRepositoryCommit)().trim() || undefined;
|
||||
} catch {
|
||||
// GitHub's ambient SHA is safe only when there is no readable checkout.
|
||||
}
|
||||
}
|
||||
const commitSource = explicitCommit || repositoryCommit || env.GITHUB_SHA?.trim();
|
||||
if (!commitSource) {
|
||||
throw new Error("Unable to resolve the Android release Git commit");
|
||||
}
|
||||
const commit = normalizeFullGitCommit(commitSource);
|
||||
|
||||
const configuredTimestamp = env.OPENCLAW_BUILD_TIMESTAMP?.trim();
|
||||
const timestamp = configuredTimestamp
|
||||
? normalizeIsoUtcTimestamp(configuredTimestamp)
|
||||
: (options.now ?? (() => new Date()))().toISOString();
|
||||
|
||||
return { commit, timestamp };
|
||||
}
|
||||
|
||||
export function androidBuildMetadataGradleArgs(metadata: AndroidBuildMetadata): string[] {
|
||||
return [
|
||||
`-PopenclawBuildCommit=${metadata.commit}`,
|
||||
`-PopenclawBuildTimestamp=${metadata.timestamp}`,
|
||||
];
|
||||
}
|
||||
|
||||
export function verifyAndroidReleaseSource(
|
||||
expectedCommit: string,
|
||||
options: {
|
||||
rootDir?: string;
|
||||
runGit?: (args: string[], cwd: string) => string;
|
||||
} = {},
|
||||
): void {
|
||||
const cwd = options.rootDir ?? rootDir;
|
||||
const runGit =
|
||||
options.runGit ??
|
||||
((args: string[], gitCwd: string) =>
|
||||
execFileSync("git", args, {
|
||||
cwd: gitCwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
}));
|
||||
let head: string;
|
||||
let status: string;
|
||||
try {
|
||||
head = normalizeFullGitCommit(runGit(["rev-parse", "HEAD"], cwd));
|
||||
status = runGit(["status", "--porcelain", "--untracked-files=all"], cwd).trim();
|
||||
} catch {
|
||||
throw new Error("Android release builds require a readable Git checkout");
|
||||
}
|
||||
if (head !== expectedCommit) {
|
||||
throw new Error(`Android release commit mismatch: metadata ${expectedCommit}, checkout ${head}`);
|
||||
}
|
||||
if (status) {
|
||||
throw new Error("Android release builds require a clean Git checkout");
|
||||
}
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): CliOptions {
|
||||
let artifact: CliOptions["artifact"] = "all";
|
||||
let dryRun = false;
|
||||
let verifyApk: string | undefined;
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const arg = argv[index];
|
||||
switch (arg) {
|
||||
case "--artifact": {
|
||||
const value = argv[index + 1];
|
||||
if (value !== "all" && value !== "play" && value !== "wear" && value !== "third-party") {
|
||||
throw new Error("--artifact must be one of: all, play, wear, third-party");
|
||||
}
|
||||
artifact = value;
|
||||
index += 1;
|
||||
break;
|
||||
}
|
||||
case "--dry-run": {
|
||||
dryRun = true;
|
||||
break;
|
||||
}
|
||||
case "--verify-apk": {
|
||||
const value = argv[index + 1];
|
||||
if (!value || value.startsWith("-")) {
|
||||
throw new Error("Missing value for --verify-apk");
|
||||
}
|
||||
verifyApk = value;
|
||||
index += 1;
|
||||
break;
|
||||
}
|
||||
case "-h":
|
||||
case "--help": {
|
||||
console.log(
|
||||
[
|
||||
"Usage: bun apps/android/scripts/build-release-artifacts.ts [--artifact all|play|wear|third-party] [--dry-run] [--verify-apk PATH]",
|
||||
"",
|
||||
"Builds the signed phone, Wear, and third-party Android artifacts.",
|
||||
].join("\n"),
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unknown argument: ${arg}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (verifyApk && (artifact !== "all" || dryRun)) {
|
||||
throw new Error("--verify-apk cannot be combined with --artifact or --dry-run");
|
||||
}
|
||||
|
||||
return { artifact, dryRun, verifyApk };
|
||||
}
|
||||
|
||||
function pinnedApkCertificateSha256(): string {
|
||||
const manifest = JSON.parse(readFileSync(releaseSigningManifestPath, "utf8")) as {
|
||||
apkCertificateSha256?: unknown;
|
||||
};
|
||||
const fingerprint = manifest.apkCertificateSha256;
|
||||
if (typeof fingerprint !== "string" || !/^[a-f0-9]{64}$/u.test(fingerprint)) {
|
||||
throw new Error("ReleaseSigning.json must pin apkCertificateSha256 as 64 lowercase hex digits");
|
||||
}
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
function releaseArtifacts(versionName: string): ReleaseArtifact[] {
|
||||
return [
|
||||
{
|
||||
flavorName: "wear",
|
||||
kind: "aab",
|
||||
gradleTask: ":wear:bundleRelease",
|
||||
sourcePath: join(androidDir, "wear", "build", "outputs", "bundle", "release", "wear-release.aab"),
|
||||
},
|
||||
{
|
||||
flavorName: "play",
|
||||
kind: "aab",
|
||||
gradleTask: ":app:bundlePlayRelease",
|
||||
sourcePath: join(
|
||||
androidDir,
|
||||
"app",
|
||||
"build",
|
||||
"outputs",
|
||||
"bundle",
|
||||
"playRelease",
|
||||
"app-play-release.aab",
|
||||
),
|
||||
},
|
||||
{
|
||||
flavorName: "third-party",
|
||||
kind: "apk",
|
||||
gradleTask: ":app:assembleThirdPartyRelease",
|
||||
sourcePath: join(
|
||||
androidDir,
|
||||
"app",
|
||||
"build",
|
||||
"outputs",
|
||||
"apk",
|
||||
"thirdParty",
|
||||
"release",
|
||||
`openclaw-${versionName}-thirdParty-release.apk`,
|
||||
),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function sha256Hex(path: string): string {
|
||||
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
||||
}
|
||||
|
||||
function writeSha256File(path: string): string {
|
||||
const hash = sha256Hex(path);
|
||||
const checksumPath = `${path}.sha256`;
|
||||
writeFileSync(checksumPath, `${hash} ${basename(path)}\n`);
|
||||
return hash;
|
||||
}
|
||||
|
||||
function verifyAabSignature(path: string, expectedCertificateSha256: string): void {
|
||||
execFileSync("jarsigner", ["-verify", path], { stdio: "ignore" });
|
||||
const output = execFileSync("keytool", ["-printcert", "-jarfile", path], {
|
||||
encoding: "utf8",
|
||||
env: { ...process.env, LC_ALL: "C", LANG: "C" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
const fingerprints = Array.from(output.matchAll(/^\s*SHA256:\s*([a-fA-F0-9:]+)\s*$/gmu)).map(
|
||||
(match) => match[1]?.replaceAll(":", "").toLowerCase(),
|
||||
);
|
||||
if (fingerprints.length !== 1 || !/^[a-f0-9]{64}$/u.test(fingerprints[0] ?? "")) {
|
||||
throw new Error(`Expected exactly one SHA-256 signing certificate for ${path}`);
|
||||
}
|
||||
if (fingerprints[0] !== expectedCertificateSha256) {
|
||||
throw new Error(
|
||||
`AAB signing certificate mismatch for ${path}: expected ${expectedCertificateSha256}, got ${fingerprints[0]}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveApkSignerFromSdk(sdkRoot: string | undefined): string | null {
|
||||
if (!sdkRoot) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const buildToolsDir = join(sdkRoot, "build-tools");
|
||||
if (!existsSync(buildToolsDir)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const candidates = readdirSync(buildToolsDir)
|
||||
.toSorted((left, right) => right.localeCompare(left))
|
||||
.map((version) => join(buildToolsDir, version, "apksigner"))
|
||||
.filter((candidate) => existsSync(candidate));
|
||||
|
||||
return candidates[0] ?? null;
|
||||
}
|
||||
|
||||
function resolveApkSigner(): string {
|
||||
const sdkApkSigner =
|
||||
resolveApkSignerFromSdk(process.env.ANDROID_HOME) ??
|
||||
resolveApkSignerFromSdk(process.env.ANDROID_SDK_ROOT);
|
||||
if (sdkApkSigner) {
|
||||
return sdkApkSigner;
|
||||
}
|
||||
|
||||
for (const pathDir of (process.env.PATH ?? "").split(delimiter)) {
|
||||
const candidate = join(pathDir, "apksigner");
|
||||
try {
|
||||
accessSync(candidate, constants.X_OK);
|
||||
return candidate;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Missing apksigner. Install Android SDK build-tools or put apksigner on PATH.");
|
||||
}
|
||||
|
||||
function verifyApkSignature(path: string, expectedCertificateSha256: string): void {
|
||||
const apkSigner = resolveApkSigner();
|
||||
let output: string;
|
||||
try {
|
||||
output = execFileSync(apkSigner, ["verify", "--print-certs", path], {
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
} catch {
|
||||
throw new Error(`apksigner verification failed for ${path}`);
|
||||
}
|
||||
|
||||
const fingerprints: string[] = [];
|
||||
for (const match of output.matchAll(
|
||||
/^Signer #[0-9]+ certificate SHA-256 digest: ([a-fA-F0-9:]+)$/gmu,
|
||||
)) {
|
||||
const fingerprint = match[1];
|
||||
if (!fingerprint) {
|
||||
throw new Error(`Malformed SHA-256 signing certificate output for ${path}`);
|
||||
}
|
||||
fingerprints.push(fingerprint.replaceAll(":", "").toLowerCase());
|
||||
}
|
||||
if (fingerprints.length !== 1 || !/^[a-f0-9]{64}$/u.test(fingerprints[0] ?? "")) {
|
||||
throw new Error(`Expected exactly one SHA-256 signing certificate for ${path}`);
|
||||
}
|
||||
if (fingerprints[0] !== expectedCertificateSha256) {
|
||||
throw new Error(
|
||||
`APK signing certificate mismatch for ${path}: expected ${expectedCertificateSha256}, got ${fingerprints[0]}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function copyArtifact(sourcePath: string, destinationPath: string): void {
|
||||
if (!existsSync(sourcePath)) {
|
||||
throw new Error(`Signed release artifact missing at ${sourcePath}`);
|
||||
}
|
||||
|
||||
copyFileSync(sourcePath, destinationPath);
|
||||
}
|
||||
|
||||
function verifyArtifactSignature(
|
||||
artifact: ReleaseArtifact,
|
||||
outputPath: string,
|
||||
expectedCertificateSha256: string,
|
||||
): void {
|
||||
if (artifact.kind === "aab") {
|
||||
verifyAabSignature(outputPath, expectedCertificateSha256);
|
||||
} else {
|
||||
verifyApkSignature(outputPath, expectedCertificateSha256);
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const expectedCertificateSha256 = pinnedApkCertificateSha256();
|
||||
if (options.verifyApk) {
|
||||
verifyApkSignature(options.verifyApk, expectedCertificateSha256);
|
||||
console.log(`Verified pinned APK signing certificate: ${options.verifyApk}`);
|
||||
return;
|
||||
}
|
||||
|
||||
syncAndroidVersioning({ mode: "check", rootDir });
|
||||
const version = resolveAndroidVersion(rootDir);
|
||||
const buildMetadata = resolveAndroidBuildMetadata();
|
||||
const artifacts = releaseArtifacts(version.canonicalVersion).filter(
|
||||
(artifact) => options.artifact === "all" || artifact.flavorName === options.artifact,
|
||||
);
|
||||
|
||||
console.log(`Android versionName: ${version.canonicalVersion}`);
|
||||
console.log(`Android versionCode: ${version.versionCode}`);
|
||||
console.log(`Android build commit: ${buildMetadata.commit}`);
|
||||
console.log(`Android build timestamp: ${buildMetadata.timestamp}`);
|
||||
for (const artifact of artifacts) {
|
||||
console.log(`Release artifact: ${artifact.flavorName} ${artifact.kind}`);
|
||||
console.log(`Gradle task: ${artifact.gradleTask}`);
|
||||
}
|
||||
|
||||
if (options.dryRun) {
|
||||
console.log("Dry run complete. No Gradle tasks were executed.");
|
||||
return;
|
||||
}
|
||||
|
||||
verifyAndroidReleaseSource(buildMetadata.commit);
|
||||
mkdirSync(releaseOutputDir, { recursive: true });
|
||||
execFileSync(
|
||||
"./gradlew",
|
||||
[
|
||||
...androidBuildMetadataGradleArgs(buildMetadata),
|
||||
...artifacts.map((artifact) => artifact.gradleTask),
|
||||
],
|
||||
{
|
||||
cwd: androidDir,
|
||||
stdio: "inherit",
|
||||
},
|
||||
);
|
||||
|
||||
for (const artifact of artifacts) {
|
||||
const outputPath = join(
|
||||
releaseOutputDir,
|
||||
`openclaw-${version.canonicalVersion}-${artifact.flavorName}-release.${artifact.kind}`,
|
||||
);
|
||||
|
||||
copyArtifact(artifact.sourcePath, outputPath);
|
||||
verifyArtifactSignature(artifact, outputPath, expectedCertificateSha256);
|
||||
const hash = writeSha256File(outputPath);
|
||||
|
||||
console.log(`Signed ${artifact.kind.toUpperCase()} (${artifact.flavorName}): ${outputPath}`);
|
||||
console.log(`SHA-256 (${artifact.flavorName}): ${hash}`);
|
||||
}
|
||||
}
|
||||
|
||||
const isMain = process.argv[1] ? resolve(process.argv[1]) === fileURLToPath(import.meta.url) : false;
|
||||
if (isMain) {
|
||||
main();
|
||||
}
|
||||
429
scripts/perf-online-benchmark.sh
Executable file
429
scripts/perf-online-benchmark.sh
Executable file
|
|
@ -0,0 +1,429 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ANDROID_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
|
||||
RESULTS_DIR="$ANDROID_DIR/benchmark/results"
|
||||
|
||||
PACKAGE="ai.openclaw.app"
|
||||
ACTIVITY=".MainActivity"
|
||||
DEVICE_SERIAL=""
|
||||
INSTALL_APP="1"
|
||||
LAUNCH_RUNS="4"
|
||||
SCREEN_LOOPS="6"
|
||||
CHAT_LOOPS="8"
|
||||
POLL_ATTEMPTS="40"
|
||||
POLL_INTERVAL_SECONDS="0.3"
|
||||
SCREEN_MODE="transition"
|
||||
CHAT_MODE="session-switch"
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage:
|
||||
./scripts/perf-online-benchmark.sh [options]
|
||||
|
||||
Measures the fully-online Android app path on a connected device/emulator.
|
||||
Assumes the app can reach a live gateway and will show "Connected" in the UI.
|
||||
|
||||
Options:
|
||||
--device <serial> adb device serial
|
||||
--package <pkg> package name (default: ai.openclaw.app)
|
||||
--activity <activity> launch activity (default: .MainActivity)
|
||||
--skip-install skip :app:installPlayDebug
|
||||
--launch-runs <n> launch-to-connected runs (default: 4)
|
||||
--screen-loops <n> screen benchmark loops (default: 6)
|
||||
--chat-loops <n> chat benchmark loops (default: 8)
|
||||
--screen-mode <mode> transition | scroll (default: transition)
|
||||
--chat-mode <mode> session-switch | scroll (default: session-switch)
|
||||
-h, --help show help
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--device)
|
||||
DEVICE_SERIAL="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--package)
|
||||
PACKAGE="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--activity)
|
||||
ACTIVITY="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--skip-install)
|
||||
INSTALL_APP="0"
|
||||
shift
|
||||
;;
|
||||
--launch-runs)
|
||||
LAUNCH_RUNS="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--screen-loops)
|
||||
SCREEN_LOOPS="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--chat-loops)
|
||||
CHAT_LOOPS="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--screen-mode)
|
||||
SCREEN_MODE="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--chat-mode)
|
||||
CHAT_MODE="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown arg: $1" >&2
|
||||
usage >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
require_cmd() {
|
||||
if ! command -v "$1" >/dev/null 2>&1; then
|
||||
echo "$1 required but missing." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
require_cmd adb
|
||||
require_cmd awk
|
||||
require_cmd rg
|
||||
require_cmd node
|
||||
|
||||
adb_cmd() {
|
||||
if [[ -n "$DEVICE_SERIAL" ]]; then
|
||||
adb -s "$DEVICE_SERIAL" "$@"
|
||||
else
|
||||
adb "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
device_count="$(adb devices | awk 'NR>1 && $2=="device" {c+=1} END {print c+0}')"
|
||||
if [[ -z "$DEVICE_SERIAL" && "$device_count" -lt 1 ]]; then
|
||||
echo "No connected Android device (adb state=device)." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "$DEVICE_SERIAL" && "$device_count" -gt 1 ]]; then
|
||||
echo "Multiple adb devices found. Pass --device <serial>." >&2
|
||||
adb devices -l >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$SCREEN_MODE" != "transition" && "$SCREEN_MODE" != "scroll" ]]; then
|
||||
echo "Unsupported --screen-mode: $SCREEN_MODE" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [[ "$CHAT_MODE" != "session-switch" && "$CHAT_MODE" != "scroll" ]]; then
|
||||
echo "Unsupported --chat-mode: $CHAT_MODE" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
mkdir -p "$RESULTS_DIR"
|
||||
|
||||
timestamp="$(date +%Y%m%d-%H%M%S)"
|
||||
run_dir="$RESULTS_DIR/online-$timestamp"
|
||||
mkdir -p "$run_dir"
|
||||
|
||||
cleanup() {
|
||||
rm -f "$run_dir"/ui-*.xml
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
if [[ "$INSTALL_APP" == "1" ]]; then
|
||||
(
|
||||
cd "$ANDROID_DIR"
|
||||
./gradlew :app:installPlayDebug --console=plain >"$run_dir/install.log" 2>&1
|
||||
)
|
||||
fi
|
||||
|
||||
read -r display_width display_height <<<"$(
|
||||
adb_cmd shell wm size \
|
||||
| awk '/Physical size:/ { split($3, dims, "x"); print dims[1], dims[2]; exit }'
|
||||
)"
|
||||
|
||||
if [[ -z "${display_width:-}" || -z "${display_height:-}" ]]; then
|
||||
echo "Failed to read device display size." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
pct_of() {
|
||||
local total="$1"
|
||||
local pct="$2"
|
||||
awk -v total="$total" -v pct="$pct" 'BEGIN { printf "%d", total * pct }'
|
||||
}
|
||||
|
||||
tab_chat_x="$(pct_of "$display_width" "0.31")"
|
||||
tab_screen_x="$(pct_of "$display_width" "0.69")"
|
||||
tab_y="$(pct_of "$display_height" "0.93")"
|
||||
chat_session_y="$(pct_of "$display_height" "0.13")"
|
||||
chat_session_left_x="$(pct_of "$display_width" "0.16")"
|
||||
chat_session_right_x="$(pct_of "$display_width" "0.85")"
|
||||
center_x="$(pct_of "$display_width" "0.50")"
|
||||
screen_swipe_top_y="$(pct_of "$display_height" "0.27")"
|
||||
screen_swipe_mid_y="$(pct_of "$display_height" "0.38")"
|
||||
screen_swipe_low_y="$(pct_of "$display_height" "0.75")"
|
||||
screen_swipe_bottom_y="$(pct_of "$display_height" "0.77")"
|
||||
chat_swipe_top_y="$(pct_of "$display_height" "0.29")"
|
||||
chat_swipe_mid_y="$(pct_of "$display_height" "0.38")"
|
||||
chat_swipe_bottom_y="$(pct_of "$display_height" "0.71")"
|
||||
|
||||
dump_ui() {
|
||||
local name="$1"
|
||||
local file="$run_dir/ui-$name.xml"
|
||||
adb_cmd shell uiautomator dump "/sdcard/$name.xml" >/dev/null 2>&1
|
||||
adb_cmd shell cat "/sdcard/$name.xml" >"$file"
|
||||
printf '%s\n' "$file"
|
||||
}
|
||||
|
||||
ui_has() {
|
||||
local pattern="$1"
|
||||
local name="$2"
|
||||
local file
|
||||
file="$(dump_ui "$name")"
|
||||
rg -q "$pattern" "$file"
|
||||
}
|
||||
|
||||
wait_for_pattern() {
|
||||
local pattern="$1"
|
||||
local prefix="$2"
|
||||
for attempt in $(seq 1 "$POLL_ATTEMPTS"); do
|
||||
if ui_has "$pattern" "$prefix-$attempt"; then
|
||||
return 0
|
||||
fi
|
||||
sleep "$POLL_INTERVAL_SECONDS"
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
ensure_connected() {
|
||||
if ! wait_for_pattern 'text="Connected"' "connected"; then
|
||||
echo "App never reached visible Connected state." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
ensure_screen_online() {
|
||||
adb_cmd shell input tap "$tab_screen_x" "$tab_y" >/dev/null
|
||||
sleep 2
|
||||
if ! ui_has 'android\.webkit\.WebView' "screen"; then
|
||||
echo "Screen benchmark expected a live WebView." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
ensure_chat_online() {
|
||||
adb_cmd shell input tap "$tab_chat_x" "$tab_y" >/dev/null
|
||||
sleep 2
|
||||
if ! ui_has 'Type a message' "chat"; then
|
||||
echo "Chat benchmark expected the live chat composer." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
capture_mem() {
|
||||
local file="$1"
|
||||
adb_cmd shell dumpsys meminfo "$PACKAGE" >"$file"
|
||||
}
|
||||
|
||||
start_cpu_sampler() {
|
||||
local file="$1"
|
||||
local samples="$2"
|
||||
: >"$file"
|
||||
(
|
||||
for _ in $(seq 1 "$samples"); do
|
||||
adb_cmd shell top -b -n 1 \
|
||||
| awk -v pkg="$PACKAGE" '$NF==pkg { print $9 }' >>"$file"
|
||||
sleep 0.5
|
||||
done
|
||||
) &
|
||||
CPU_SAMPLER_PID="$!"
|
||||
}
|
||||
|
||||
summarize_cpu() {
|
||||
local file="$1"
|
||||
local prefix="$2"
|
||||
local avg max median count
|
||||
avg="$(awk '{sum+=$1; n++} END {if(n) printf "%.1f", sum/n; else print 0}' "$file")"
|
||||
max="$(sort -n "$file" | tail -n 1)"
|
||||
median="$(
|
||||
sort -n "$file" \
|
||||
| awk '{a[NR]=$1} END { if (NR==0) { print 0 } else if (NR%2==1) { printf "%.1f", a[(NR+1)/2] } else { printf "%.1f", (a[NR/2]+a[NR/2+1])/2 } }'
|
||||
)"
|
||||
count="$(wc -l <"$file" | tr -d ' ')"
|
||||
printf '%s.cpu_avg_pct=%s\n' "$prefix" "$avg" >>"$run_dir/summary.txt"
|
||||
printf '%s.cpu_median_pct=%s\n' "$prefix" "$median" >>"$run_dir/summary.txt"
|
||||
printf '%s.cpu_peak_pct=%s\n' "$prefix" "$max" >>"$run_dir/summary.txt"
|
||||
printf '%s.cpu_count=%s\n' "$prefix" "$count" >>"$run_dir/summary.txt"
|
||||
}
|
||||
|
||||
summarize_mem() {
|
||||
local file="$1"
|
||||
local prefix="$2"
|
||||
awk -v prefix="$prefix" '
|
||||
/TOTAL PSS:/ { printf "%s.pss_kb=%s\n%s.rss_kb=%s\n", prefix, $3, prefix, $6 }
|
||||
/Graphics:/ { printf "%s.graphics_kb=%s\n", prefix, $2 }
|
||||
/WebViews:/ { printf "%s.webviews=%s\n", prefix, $NF }
|
||||
' "$file" >>"$run_dir/summary.txt"
|
||||
}
|
||||
|
||||
summarize_gfx() {
|
||||
local file="$1"
|
||||
local prefix="$2"
|
||||
awk -v prefix="$prefix" '
|
||||
/Total frames rendered:/ { printf "%s.frames=%s\n", prefix, $4 }
|
||||
/Janky frames:/ && $4 ~ /\(/ {
|
||||
pct=$4
|
||||
gsub(/[()%]/, "", pct)
|
||||
printf "%s.janky_frames=%s\n%s.janky_pct=%s\n", prefix, $3, prefix, pct
|
||||
}
|
||||
/50th percentile:/ { gsub(/ms/, "", $3); printf "%s.p50_ms=%s\n", prefix, $3 }
|
||||
/90th percentile:/ { gsub(/ms/, "", $3); printf "%s.p90_ms=%s\n", prefix, $3 }
|
||||
/95th percentile:/ { gsub(/ms/, "", $3); printf "%s.p95_ms=%s\n", prefix, $3 }
|
||||
/99th percentile:/ { gsub(/ms/, "", $3); printf "%s.p99_ms=%s\n", prefix, $3 }
|
||||
' "$file" >>"$run_dir/summary.txt"
|
||||
}
|
||||
|
||||
measure_launch() {
|
||||
: >"$run_dir/launch-runs.txt"
|
||||
for run in $(seq 1 "$LAUNCH_RUNS"); do
|
||||
adb_cmd shell am force-stop "$PACKAGE" >/dev/null
|
||||
sleep 1
|
||||
start_ms="$(node -e 'console.log(Date.now())')"
|
||||
am_out="$(adb_cmd shell am start -W -n "$PACKAGE/$ACTIVITY")"
|
||||
total_time="$(printf '%s\n' "$am_out" | awk -F: '/TotalTime:/{gsub(/ /, "", $2); print $2}')"
|
||||
connected_ms="timeout"
|
||||
for _ in $(seq 1 "$POLL_ATTEMPTS"); do
|
||||
if ui_has 'text="Connected"' "launch-run-$run"; then
|
||||
now_ms="$(node -e 'console.log(Date.now())')"
|
||||
connected_ms="$((now_ms - start_ms))"
|
||||
break
|
||||
fi
|
||||
sleep "$POLL_INTERVAL_SECONDS"
|
||||
done
|
||||
printf 'run=%s total_time_ms=%s connected_ms=%s\n' "$run" "${total_time:-na}" "$connected_ms" \
|
||||
| tee -a "$run_dir/launch-runs.txt"
|
||||
done
|
||||
|
||||
awk -F'[ =]' '
|
||||
/total_time_ms=[0-9]+/ {
|
||||
value=$4
|
||||
sum+=value
|
||||
count+=1
|
||||
if (min==0 || value<min) min=value
|
||||
if (value>max) max=value
|
||||
}
|
||||
END {
|
||||
if (count==0) exit
|
||||
printf "launch.total_time_avg_ms=%.1f\nlaunch.total_time_min_ms=%d\nlaunch.total_time_max_ms=%d\n", sum/count, min, max
|
||||
}
|
||||
' "$run_dir/launch-runs.txt" >>"$run_dir/summary.txt"
|
||||
|
||||
awk -F'[ =]' '
|
||||
/connected_ms=[0-9]+/ {
|
||||
value=$6
|
||||
sum+=value
|
||||
count+=1
|
||||
if (min==0 || value<min) min=value
|
||||
if (value>max) max=value
|
||||
}
|
||||
END {
|
||||
if (count==0) exit
|
||||
printf "launch.connected_avg_ms=%.1f\nlaunch.connected_min_ms=%d\nlaunch.connected_max_ms=%d\n", sum/count, min, max
|
||||
}
|
||||
' "$run_dir/launch-runs.txt" >>"$run_dir/summary.txt"
|
||||
}
|
||||
|
||||
run_screen_benchmark() {
|
||||
ensure_screen_online
|
||||
capture_mem "$run_dir/screen-mem-before.txt"
|
||||
adb_cmd shell dumpsys gfxinfo "$PACKAGE" reset >/dev/null
|
||||
start_cpu_sampler "$run_dir/screen-cpu.txt" 18
|
||||
|
||||
if [[ "$SCREEN_MODE" == "transition" ]]; then
|
||||
for _ in $(seq 1 "$SCREEN_LOOPS"); do
|
||||
adb_cmd shell input tap "$tab_screen_x" "$tab_y" >/dev/null
|
||||
sleep 1.0
|
||||
adb_cmd shell input tap "$tab_chat_x" "$tab_y" >/dev/null
|
||||
sleep 0.8
|
||||
done
|
||||
else
|
||||
adb_cmd shell input tap "$tab_screen_x" "$tab_y" >/dev/null
|
||||
sleep 1.5
|
||||
for _ in $(seq 1 "$SCREEN_LOOPS"); do
|
||||
adb_cmd shell input swipe "$center_x" "$screen_swipe_bottom_y" "$center_x" "$screen_swipe_top_y" 250 >/dev/null
|
||||
sleep 0.35
|
||||
adb_cmd shell input swipe "$center_x" "$screen_swipe_mid_y" "$center_x" "$screen_swipe_low_y" 250 >/dev/null
|
||||
sleep 0.35
|
||||
done
|
||||
fi
|
||||
|
||||
wait "$CPU_SAMPLER_PID"
|
||||
adb_cmd shell dumpsys gfxinfo "$PACKAGE" >"$run_dir/screen-gfx.txt"
|
||||
capture_mem "$run_dir/screen-mem-after.txt"
|
||||
summarize_gfx "$run_dir/screen-gfx.txt" "screen"
|
||||
summarize_cpu "$run_dir/screen-cpu.txt" "screen"
|
||||
summarize_mem "$run_dir/screen-mem-before.txt" "screen.before"
|
||||
summarize_mem "$run_dir/screen-mem-after.txt" "screen.after"
|
||||
}
|
||||
|
||||
run_chat_benchmark() {
|
||||
ensure_chat_online
|
||||
capture_mem "$run_dir/chat-mem-before.txt"
|
||||
adb_cmd shell dumpsys gfxinfo "$PACKAGE" reset >/dev/null
|
||||
start_cpu_sampler "$run_dir/chat-cpu.txt" 18
|
||||
|
||||
if [[ "$CHAT_MODE" == "session-switch" ]]; then
|
||||
for _ in $(seq 1 "$CHAT_LOOPS"); do
|
||||
adb_cmd shell input tap "$chat_session_left_x" "$chat_session_y" >/dev/null
|
||||
sleep 0.8
|
||||
adb_cmd shell input tap "$chat_session_right_x" "$chat_session_y" >/dev/null
|
||||
sleep 0.8
|
||||
done
|
||||
else
|
||||
for _ in $(seq 1 "$CHAT_LOOPS"); do
|
||||
adb_cmd shell input swipe "$center_x" "$chat_swipe_bottom_y" "$center_x" "$chat_swipe_top_y" 250 >/dev/null
|
||||
sleep 0.35
|
||||
adb_cmd shell input swipe "$center_x" "$chat_swipe_mid_y" "$center_x" "$chat_swipe_bottom_y" 250 >/dev/null
|
||||
sleep 0.35
|
||||
done
|
||||
fi
|
||||
|
||||
wait "$CPU_SAMPLER_PID"
|
||||
adb_cmd shell dumpsys gfxinfo "$PACKAGE" >"$run_dir/chat-gfx.txt"
|
||||
capture_mem "$run_dir/chat-mem-after.txt"
|
||||
summarize_gfx "$run_dir/chat-gfx.txt" "chat"
|
||||
summarize_cpu "$run_dir/chat-cpu.txt" "chat"
|
||||
summarize_mem "$run_dir/chat-mem-before.txt" "chat.before"
|
||||
summarize_mem "$run_dir/chat-mem-after.txt" "chat.after"
|
||||
}
|
||||
|
||||
printf 'device.serial=%s\n' "${DEVICE_SERIAL:-default}" >"$run_dir/summary.txt"
|
||||
printf 'device.display=%sx%s\n' "$display_width" "$display_height" >>"$run_dir/summary.txt"
|
||||
printf 'config.launch_runs=%s\n' "$LAUNCH_RUNS" >>"$run_dir/summary.txt"
|
||||
printf 'config.screen_loops=%s\n' "$SCREEN_LOOPS" >>"$run_dir/summary.txt"
|
||||
printf 'config.chat_loops=%s\n' "$CHAT_LOOPS" >>"$run_dir/summary.txt"
|
||||
printf 'config.screen_mode=%s\n' "$SCREEN_MODE" >>"$run_dir/summary.txt"
|
||||
printf 'config.chat_mode=%s\n' "$CHAT_MODE" >>"$run_dir/summary.txt"
|
||||
|
||||
ensure_connected
|
||||
measure_launch
|
||||
ensure_connected
|
||||
run_screen_benchmark
|
||||
ensure_connected
|
||||
run_chat_benchmark
|
||||
|
||||
printf 'results_dir=%s\n' "$run_dir"
|
||||
cat "$run_dir/summary.txt"
|
||||
124
scripts/perf-startup-benchmark.sh
Executable file
124
scripts/perf-startup-benchmark.sh
Executable file
|
|
@ -0,0 +1,124 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ANDROID_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
|
||||
RESULTS_DIR="$ANDROID_DIR/benchmark/results"
|
||||
CLASS_FILTER="ai.openclaw.app.benchmark.StartupMacrobenchmark#coldStartup"
|
||||
BASELINE_JSON=""
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage:
|
||||
./scripts/perf-startup-benchmark.sh [--baseline <benchmarkData.json>]
|
||||
|
||||
Runs cold-start macrobenchmark only, then prints a compact summary.
|
||||
Also saves a timestamped snapshot JSON under benchmark/results/.
|
||||
If --baseline is omitted, compares against latest previous snapshot when available.
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--baseline)
|
||||
BASELINE_JSON="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown arg: $1" >&2
|
||||
usage >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if ! command -v jq >/dev/null 2>&1; then
|
||||
echo "jq required but missing." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v adb >/dev/null 2>&1; then
|
||||
echo "adb required but missing." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
device_count="$(adb devices | awk 'NR>1 && $2=="device" {c+=1} END {print c+0}')"
|
||||
if [[ "$device_count" -lt 1 ]]; then
|
||||
echo "No connected Android device (adb state=device)." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$RESULTS_DIR"
|
||||
|
||||
run_log="$(mktemp -t openclaw-android-bench.XXXXXX.log)"
|
||||
trap 'rm -f "$run_log"' EXIT
|
||||
|
||||
cd "$ANDROID_DIR"
|
||||
|
||||
./gradlew :benchmark:connectedDebugAndroidTest \
|
||||
-Pandroid.testInstrumentationRunnerArguments.class="$CLASS_FILTER" \
|
||||
--console=plain \
|
||||
>"$run_log" 2>&1
|
||||
|
||||
latest_json="$(
|
||||
find "$ANDROID_DIR/benchmark/build/outputs/connected_android_test_additional_output/debug/connected" \
|
||||
-name '*benchmarkData.json' -type f \
|
||||
| while IFS= read -r file; do
|
||||
printf '%s\t%s\n' "$(stat -f '%m' "$file")" "$file"
|
||||
done \
|
||||
| sort -nr \
|
||||
| head -n1 \
|
||||
| cut -f2-
|
||||
)"
|
||||
|
||||
if [[ -z "$latest_json" || ! -f "$latest_json" ]]; then
|
||||
echo "benchmarkData.json not found after run." >&2
|
||||
tail -n 120 "$run_log" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
timestamp="$(date +%Y%m%d-%H%M%S)"
|
||||
snapshot_json="$RESULTS_DIR/startup-$timestamp.json"
|
||||
cp "$latest_json" "$snapshot_json"
|
||||
|
||||
median_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.median' "$snapshot_json")"
|
||||
min_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.minimum' "$snapshot_json")"
|
||||
max_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.maximum' "$snapshot_json")"
|
||||
cov="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.coefficientOfVariation' "$snapshot_json")"
|
||||
device="$(jq -r '.context.build.model' "$snapshot_json")"
|
||||
sdk="$(jq -r '.context.build.version.sdk' "$snapshot_json")"
|
||||
runs_count="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.runs | length' "$snapshot_json")"
|
||||
|
||||
printf 'startup.cold.median_ms=%.3f min_ms=%.3f max_ms=%.3f cov=%.4f runs=%s device=%s sdk=%s\n' \
|
||||
"$median_ms" "$min_ms" "$max_ms" "$cov" "$runs_count" "$device" "$sdk"
|
||||
echo "snapshot_json=$snapshot_json"
|
||||
|
||||
if [[ -z "$BASELINE_JSON" ]]; then
|
||||
BASELINE_JSON="$(
|
||||
find "$RESULTS_DIR" -name 'startup-*.json' -type f \
|
||||
| while IFS= read -r file; do
|
||||
if [[ "$file" == "$snapshot_json" ]]; then
|
||||
continue
|
||||
fi
|
||||
printf '%s\t%s\n' "$(stat -f '%m' "$file")" "$file"
|
||||
done \
|
||||
| sort -nr \
|
||||
| head -n1 \
|
||||
| cut -f2-
|
||||
)"
|
||||
fi
|
||||
|
||||
if [[ -n "$BASELINE_JSON" ]]; then
|
||||
if [[ ! -f "$BASELINE_JSON" ]]; then
|
||||
echo "Baseline file missing: $BASELINE_JSON" >&2
|
||||
exit 1
|
||||
fi
|
||||
base_median="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.median' "$BASELINE_JSON")"
|
||||
delta_ms="$(awk -v a="$median_ms" -v b="$base_median" 'BEGIN { printf "%.3f", (a-b) }')"
|
||||
delta_pct="$(awk -v a="$median_ms" -v b="$base_median" 'BEGIN { if (b==0) { print "nan" } else { printf "%.2f", ((a-b)/b)*100 } }')"
|
||||
echo "baseline_median_ms=$base_median delta_ms=$delta_ms delta_pct=$delta_pct%"
|
||||
fi
|
||||
154
scripts/perf-startup-hotspots.sh
Executable file
154
scripts/perf-startup-hotspots.sh
Executable file
|
|
@ -0,0 +1,154 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ANDROID_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
PACKAGE="ai.openclaw.app"
|
||||
ACTIVITY=".MainActivity"
|
||||
DURATION_SECONDS="10"
|
||||
OUTPUT_PERF_DATA=""
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage:
|
||||
./scripts/perf-startup-hotspots.sh [--package <pkg>] [--activity <activity>] [--duration <sec>] [--out <perf.data>]
|
||||
|
||||
Captures startup CPU profile via simpleperf (app_profiler.py), then prints concise hotspot summaries.
|
||||
Default package/activity target OpenClaw Android startup.
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--package)
|
||||
PACKAGE="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--activity)
|
||||
ACTIVITY="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--duration)
|
||||
DURATION_SECONDS="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
--out)
|
||||
OUTPUT_PERF_DATA="${2:-}"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Unknown arg: $1" >&2
|
||||
usage >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if ! command -v uv >/dev/null 2>&1; then
|
||||
echo "uv required but missing." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v adb >/dev/null 2>&1; then
|
||||
echo "adb required but missing." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "$OUTPUT_PERF_DATA" ]]; then
|
||||
OUTPUT_PERF_DATA="/tmp/openclaw-startup-$(date +%Y%m%d-%H%M%S).perf.data"
|
||||
fi
|
||||
|
||||
device_count="$(adb devices | awk 'NR>1 && $2=="device" {c+=1} END {print c+0}')"
|
||||
if [[ "$device_count" -lt 1 ]]; then
|
||||
echo "No connected Android device (adb state=device)." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
simpleperf_dir=""
|
||||
if [[ -n "${ANDROID_NDK_HOME:-}" && -f "${ANDROID_NDK_HOME}/simpleperf/app_profiler.py" ]]; then
|
||||
simpleperf_dir="${ANDROID_NDK_HOME}/simpleperf"
|
||||
elif [[ -n "${ANDROID_NDK_ROOT:-}" && -f "${ANDROID_NDK_ROOT}/simpleperf/app_profiler.py" ]]; then
|
||||
simpleperf_dir="${ANDROID_NDK_ROOT}/simpleperf"
|
||||
else
|
||||
latest_simpleperf="$(ls -d "${HOME}/Library/Android/sdk/ndk/"*/simpleperf 2>/dev/null | sort -V | tail -n1 || true)"
|
||||
if [[ -n "$latest_simpleperf" && -f "$latest_simpleperf/app_profiler.py" ]]; then
|
||||
simpleperf_dir="$latest_simpleperf"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "$simpleperf_dir" ]]; then
|
||||
echo "simpleperf not found. Set ANDROID_NDK_HOME or install NDK under ~/Library/Android/sdk/ndk/." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
app_profiler="$simpleperf_dir/app_profiler.py"
|
||||
report_py="$simpleperf_dir/report.py"
|
||||
ndk_path="$(cd -- "$simpleperf_dir/.." && pwd)"
|
||||
|
||||
tmp_dir="$(mktemp -d -t openclaw-android-hotspots.XXXXXX)"
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
||||
|
||||
capture_log="$tmp_dir/capture.log"
|
||||
dso_csv="$tmp_dir/dso.csv"
|
||||
symbols_csv="$tmp_dir/symbols.csv"
|
||||
children_txt="$tmp_dir/children.txt"
|
||||
|
||||
cd "$ANDROID_DIR"
|
||||
./gradlew :app:installPlayDebug --console=plain >"$tmp_dir/install.log" 2>&1
|
||||
|
||||
if ! uv run --no-project python3 "$app_profiler" \
|
||||
-p "$PACKAGE" \
|
||||
-a "$ACTIVITY" \
|
||||
-o "$OUTPUT_PERF_DATA" \
|
||||
--ndk_path "$ndk_path" \
|
||||
-r "-e task-clock:u -f 1000 -g --duration $DURATION_SECONDS" \
|
||||
>"$capture_log" 2>&1; then
|
||||
echo "simpleperf capture failed. tail(capture_log):" >&2
|
||||
tail -n 120 "$capture_log" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uv run --no-project python3 "$report_py" \
|
||||
-i "$OUTPUT_PERF_DATA" \
|
||||
--sort dso \
|
||||
--csv \
|
||||
--csv-separator "|" \
|
||||
--include-process-name "$PACKAGE" \
|
||||
>"$dso_csv" 2>"$tmp_dir/report-dso.err"
|
||||
|
||||
uv run --no-project python3 "$report_py" \
|
||||
-i "$OUTPUT_PERF_DATA" \
|
||||
--sort dso,symbol \
|
||||
--csv \
|
||||
--csv-separator "|" \
|
||||
--include-process-name "$PACKAGE" \
|
||||
>"$symbols_csv" 2>"$tmp_dir/report-symbols.err"
|
||||
|
||||
uv run --no-project python3 "$report_py" \
|
||||
-i "$OUTPUT_PERF_DATA" \
|
||||
--children \
|
||||
--sort dso,symbol \
|
||||
-n \
|
||||
--percent-limit 0.2 \
|
||||
--include-process-name "$PACKAGE" \
|
||||
>"$children_txt" 2>"$tmp_dir/report-children.err"
|
||||
|
||||
clean_csv() {
|
||||
awk 'BEGIN{print_on=0} /^Overhead\|/{print_on=1} print_on==1{print}' "$1"
|
||||
}
|
||||
|
||||
echo "perf_data=$OUTPUT_PERF_DATA"
|
||||
echo
|
||||
echo "top_dso_self:"
|
||||
clean_csv "$dso_csv" | tail -n +2 | awk -F'|' 'NR<=10 {printf " %s %s\n", $1, $2}'
|
||||
echo
|
||||
echo "top_symbols_self:"
|
||||
clean_csv "$symbols_csv" | tail -n +2 | awk -F'|' 'NR<=20 {printf " %s %s :: %s\n", $1, $2, $3}'
|
||||
echo
|
||||
echo "app_path_clues_children:"
|
||||
rg 'androidx\.compose|MainActivity|NodeRuntime|NodeForegroundService|SecurePrefs|WebView|libwebviewchromium' "$children_txt" | awk 'NR<=20 {print}' || true
|
||||
230
scripts/voice-e2e.sh
Executable file
230
scripts/voice-e2e.sh
Executable file
|
|
@ -0,0 +1,230 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
|
||||
ANDROID_DIR="$ROOT_DIR/apps/android"
|
||||
PACKAGE_NAME="ai.openclaw.app"
|
||||
RECEIVER="$PACKAGE_NAME/.VoiceE2eReceiver"
|
||||
RUN_ACTION="ai.openclaw.app.debug.RUN_VOICE_E2E"
|
||||
OPEN_ACTION="ai.openclaw.app.debug.OPEN_VOICE_E2E"
|
||||
PORT=18789
|
||||
HOST="127.0.0.1"
|
||||
MODE="both"
|
||||
TRANSCRIPT="Reply exactly: Android voice e2e normal path ok."
|
||||
REALTIME_ASSISTANT="Android realtime voice e2e relay path ok."
|
||||
TIMEOUT_MS=60000
|
||||
INSTALL=1
|
||||
CONNECT=1
|
||||
CLEANUP=0
|
||||
START_GATEWAY=0
|
||||
|
||||
usage() {
|
||||
cat <<'USAGE'
|
||||
Usage: apps/android/scripts/voice-e2e.sh [options]
|
||||
|
||||
Options:
|
||||
--mode connect|normal|realtime|both
|
||||
Gateway probe or voice path to test. Default: both.
|
||||
--transcript TEXT Synthetic user transcript for the voice turn.
|
||||
--realtime-assistant TEXT Synthetic realtime assistant relay text.
|
||||
--host HOST Gateway host visible from Android. Default: 127.0.0.1.
|
||||
--port PORT Gateway port. Default: 18789.
|
||||
--timeout-ms MS Per-mode timeout. Default: 60000.
|
||||
--skip-install Reuse the installed debug app.
|
||||
--no-connect Do not rewrite manual gateway settings.
|
||||
--start-gateway Start a temporary local gateway with bws_get_secret.
|
||||
--cleanup Stop voice capture after screenshots.
|
||||
USAGE
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--mode)
|
||||
MODE="$2"
|
||||
shift 2
|
||||
;;
|
||||
--transcript)
|
||||
TRANSCRIPT="$2"
|
||||
shift 2
|
||||
;;
|
||||
--realtime-assistant)
|
||||
REALTIME_ASSISTANT="$2"
|
||||
shift 2
|
||||
;;
|
||||
--host)
|
||||
HOST="$2"
|
||||
shift 2
|
||||
;;
|
||||
--port)
|
||||
PORT="$2"
|
||||
shift 2
|
||||
;;
|
||||
--timeout-ms)
|
||||
TIMEOUT_MS="$2"
|
||||
shift 2
|
||||
;;
|
||||
--skip-install)
|
||||
INSTALL=0
|
||||
shift
|
||||
;;
|
||||
--no-connect)
|
||||
CONNECT=0
|
||||
shift
|
||||
;;
|
||||
--start-gateway)
|
||||
START_GATEWAY=1
|
||||
shift
|
||||
;;
|
||||
--cleanup)
|
||||
CLEANUP=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "unknown option: $1" >&2
|
||||
usage >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
export JAVA_HOME="${JAVA_HOME:-/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home}"
|
||||
export ANDROID_HOME="${ANDROID_HOME:-/opt/homebrew/share/android-commandlinetools}"
|
||||
export ANDROID_SDK_ROOT="${ANDROID_SDK_ROOT:-$ANDROID_HOME}"
|
||||
export PATH="/opt/homebrew/opt/openjdk@17/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin:$PATH"
|
||||
|
||||
ARTIFACT_DIR="/tmp/openclaw-android-voice-e2e-$(date +%Y%m%d-%H%M%S)"
|
||||
mkdir -p "$ARTIFACT_DIR"
|
||||
|
||||
cleanup_gateway() {
|
||||
if [[ -n "${GATEWAY_PID:-}" ]]; then
|
||||
kill "$GATEWAY_PID" >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
trap cleanup_gateway EXIT
|
||||
|
||||
if ! adb devices -l | awk 'NR > 1 && $2 == "device" { found = 1 } END { exit(found ? 0 : 1) }'; then
|
||||
echo "no authorized Android device found" >&2
|
||||
adb devices -l >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
adb reverse "tcp:$PORT" "tcp:$PORT" >/dev/null
|
||||
|
||||
if [[ "$START_GATEWAY" -eq 1 ]]; then
|
||||
if command -v bws_get_secret >/dev/null 2>&1; then
|
||||
OPENCLAW_OPENAI_API_KEY="$(bws_get_secret OPENCLAW_OPENAI_API_KEY)"
|
||||
else
|
||||
OPENCLAW_OPENAI_API_KEY="$(zsh -ic 'bws_get_secret OPENCLAW_OPENAI_API_KEY')"
|
||||
fi
|
||||
(
|
||||
cd "$ROOT_DIR"
|
||||
OPENAI_API_KEY="$OPENCLAW_OPENAI_API_KEY" \
|
||||
pnpm openclaw gateway run \
|
||||
--port "$PORT" \
|
||||
--auth none \
|
||||
--bind loopback \
|
||||
--force \
|
||||
--allow-unconfigured \
|
||||
--ws-log compact
|
||||
) >"$ARTIFACT_DIR/gateway.log" 2>&1 &
|
||||
GATEWAY_PID=$!
|
||||
sleep 4
|
||||
if ! kill -0 "$GATEWAY_PID" >/dev/null 2>&1; then
|
||||
cat "$ARTIFACT_DIR/gateway.log" >&2
|
||||
exit 1
|
||||
fi
|
||||
unset OPENCLAW_OPENAI_API_KEY
|
||||
fi
|
||||
|
||||
if [[ "$INSTALL" -eq 1 ]]; then
|
||||
(cd "$ANDROID_DIR" && ./gradlew :app:installPlayDebug)
|
||||
fi
|
||||
|
||||
adb shell pm grant "$PACKAGE_NAME" android.permission.RECORD_AUDIO >/dev/null 2>&1 || true
|
||||
adb shell am force-stop "$PACKAGE_NAME" >/dev/null
|
||||
adb shell am start -a "$OPEN_ACTION" -n "$PACKAGE_NAME/.MainActivity" >/dev/null
|
||||
adb logcat -c
|
||||
|
||||
run_mode() {
|
||||
local test_mode="$1"
|
||||
local result_name="$ARTIFACT_DIR/result-$test_mode.json"
|
||||
local screenshot_name="$ARTIFACT_DIR/screen-$test_mode.png"
|
||||
local transcript_base64
|
||||
local realtime_assistant_base64
|
||||
transcript_base64="$(printf '%s' "$TRANSCRIPT" | base64 | tr -d '\n')"
|
||||
realtime_assistant_base64="$(printf '%s' "$REALTIME_ASSISTANT" | base64 | tr -d '\n')"
|
||||
|
||||
adb shell run-as "$PACKAGE_NAME" rm -f cache/voice_e2e_result.json >/dev/null 2>&1 || true
|
||||
local no_connect_flag=true
|
||||
if [[ "$CONNECT" -eq 1 ]]; then
|
||||
no_connect_flag=false
|
||||
fi
|
||||
|
||||
adb shell run-as "$PACKAGE_NAME" am broadcast --user 0 \
|
||||
-a "$RUN_ACTION" \
|
||||
-n "$RECEIVER" \
|
||||
--es mode "$test_mode" \
|
||||
--ez noConnect "$no_connect_flag" \
|
||||
--es host "$HOST" \
|
||||
--ei port "$PORT" \
|
||||
--ez tls false \
|
||||
--el timeoutMs "$TIMEOUT_MS" \
|
||||
--el connectTimeoutMs "$TIMEOUT_MS" \
|
||||
--es transcriptBase64 "$transcript_base64" \
|
||||
--es realtimeAssistantBase64 "$realtime_assistant_base64" >/dev/null
|
||||
|
||||
local deadline=$((SECONDS + TIMEOUT_MS / 1000 + 20))
|
||||
local result=""
|
||||
while [[ "$SECONDS" -lt "$deadline" ]]; do
|
||||
result="$(adb shell run-as "$PACKAGE_NAME" cat cache/voice_e2e_result.json 2>/dev/null | tr -d '\r' || true)"
|
||||
if [[ -n "$result" ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if [[ -z "$result" ]]; then
|
||||
echo "voice e2e $test_mode timed out waiting for result" >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s\n' "$result" >"$result_name"
|
||||
adb exec-out screencap -p >"$screenshot_name"
|
||||
if ! grep -q '"ok":true' "$result_name"; then
|
||||
echo "voice e2e $test_mode failed: $result" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
case "$MODE" in
|
||||
both)
|
||||
run_mode normal
|
||||
run_mode realtime
|
||||
;;
|
||||
normal|dictation)
|
||||
run_mode normal
|
||||
;;
|
||||
realtime|talk)
|
||||
run_mode realtime
|
||||
;;
|
||||
connect)
|
||||
run_mode connect
|
||||
;;
|
||||
*)
|
||||
echo "unknown mode: $MODE" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
adb logcat -d -v time |
|
||||
rg -i 'OpenClaw|TalkMode|MicCapture|AudioRecord|SpeechRecognizer|realtime|talk.session|appendAudio|transcript|Talk failed|Transcription failed|Speech network|VoiceE2E' |
|
||||
tail -250 >"$ARTIFACT_DIR/logcat.txt" || true
|
||||
|
||||
if [[ "$CLEANUP" -eq 1 ]]; then
|
||||
adb shell run-as "$PACKAGE_NAME" am broadcast --user 0 -a "$RUN_ACTION" -n "$RECEIVER" --es mode stop >/dev/null
|
||||
fi
|
||||
|
||||
echo "$ARTIFACT_DIR"
|
||||
Loading…
Add table
Add a link
Reference in a new issue