Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
import: keep Android evidence (gfxinfo, Perfetto, simpleperf, JSON) apart from host numbers (#143)
EL4CTEO113eea2d6c8e
diff --git a/README.md b/README.mdindex 7d49507..5670cdb 100644--- a/README.md+++ b/README.md@@ -48,6 +48,7 @@ Or step by step:| `enable` / `disable` | swaps the Rust in or out (one marked block, removed byte for byte) || `bench` | times the whole app in rotated rounds of original, original again (an A/A control for noise) and Rust, plus one warm-up per side (`--runs 5` = 11 original runs, 6 Rust runs); wall and CPU time || `report` | markdown + SVG card for your PR |+| `import gfxinfo` / `perfetto` / `simpleperf` / `json` | keeps evidence measured on an Android device (ART) with its metadata and SHA-256; `report` shows it in its own section, never as a speed-up against the host numbers || `doctor` | checks Rust, your runtimes and your agents |`--agent` takes `claude`, `codex`, or any command that reads a prompt on stdin. Without it, hand `TASK.md` to whatever you use.diff --git a/src/android.rs b/src/android.rsnew file mode 100644index 0000000..21092c0--- /dev/null+++ b/src/android.rs@@ -0,0 +1,812 @@+//! Evidence measured on an Android device (dumpsys gfxinfo, Perfetto, simpleperf, or a JSON summary in+//! the same schema), kept apart from the host scan and bench: ART timings can't be compared with them (#143).++use crate::{sha256, util};+use anyhow::{Context, Result, bail, ensure};+use serde::Serialize;+use serde_json::{Map, Value, json};+use std::fs;+use std::path::{Component, Path, PathBuf};++pub const SCHEMA_VERSION: u64 = 1;+/// Fewer frames than this make a jank percentage meaningless: one frame is 0% or 100%.+pub const MIN_FRAMES: u64 = 30;+const NOT_COMPARABLE: &str = "ART frame timing is not comparable to native Rust wall time";+const NOT_PARSED: &str = "fast-anything keeps the trace and its hash but doesn't parse or symbolize traces yet";++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum Format {+ Gfxinfo,+ Json,+ Perfetto,+ Simpleperf,+}++impl Format {+ pub fn name(self) -> &'static str {+ match self {+ Format::Gfxinfo => "gfxinfo",+ Format::Json => "json",+ Format::Perfetto => "perfetto",+ Format::Simpleperf => "simpleperf",+ }+ }+}++/// What was measured, as given on the command line: never guessed from the capture.+#[derive(Debug, Clone, Default)]+pub struct Meta {+ pub package: Option<String>,+ pub device: Option<String>,+ pub model: Option<String>,+ pub android_version: Option<String>,+ pub api: Option<u32>,+ pub abi: Option<String>,+ pub build_variant: Option<String>,+ pub version_name: Option<String>,+ pub version_code: Option<u64>,+ pub thermal_status: Option<u8>,+ pub runs: Option<u32>,+ pub warmup: Option<u32>,+ pub captured_at: Option<String>,+ pub captured_by: Option<String>,+}++type Field = (&'static str, [&'static str; 2], Option<Value>);++impl Meta {+ /// Each flag, where it goes in the report, and its value.+ fn fields(&self) -> Vec<Field> {+ let s = |v: &Option<String>| v.as_ref().map(|v| json!(v));+ vec![+ ("--device", ["device", "serial"], s(&self.device)),+ ("--model", ["device", "model"], s(&self.model)),+ ("--android-version", ["device", "android_version"], s(&self.android_version)),+ ("--api", ["device", "api_level"], self.api.map(|v| json!(v))),+ ("--abi", ["device", "abi"], s(&self.abi)),+ ("--package", ["build", "package"], s(&self.package)),+ ("--build-variant", ["build", "variant"], s(&self.build_variant)),+ ("--version-name", ["build", "version_name"], s(&self.version_name)),+ ("--version-code", ["build", "version_code"], self.version_code.map(|v| json!(v))),+ ("--thermal-status", ["thermal", "status"], self.thermal_status.map(|v| json!(v))),+ ("--runs", ["samples", "runs"], self.runs.map(|v| json!(v))),+ ("--warmup", ["samples", "warmup_runs"], self.warmup.map(|v| json!(v))),+ ("--captured-by", ["provenance", "captured_by"], s(&self.captured_by)),+ ]+ }+}++/// The metadata a report can't do without, as (flag, place in the report).+const REQUIRED: [(&str, [&str; 2]); 5] = [+ ("--device", ["device", "serial"]),+ ("--package", ["build", "package"]),+ ("--android-version", ["device", "android_version"]),+ ("--abi", ["device", "abi"]),+ ("--build-variant", ["build", "variant"]),+];++pub struct Import {+ pub format: Format,+ pub input: PathBuf,+ pub meta: Meta,+ /// Default .fast/android/<format>.+ pub out: Option<PathBuf>,+ /// Perfetto: the R8/ProGuard mapping.txt; simpleperf: the symbol folder. Recorded, not used yet.+ pub symbols: Option<PathBuf>,+}++#[derive(Debug, Serialize)]+pub struct ImportOut {+ pub format: &'static str,+ pub report: PathBuf,+ pub raw: PathBuf,+ pub evidence: Value,+}++/// What a dumpsys gfxinfo text says about the frames of one process.+#[derive(Debug, Default, PartialEq)]+pub struct Gfxinfo {+ pub package: Option<String>,+ pub frames: u64,+ pub janky: Option<u64>,+ pub janky_percent: Option<f64>,+ pub p50: Option<f64>,+ pub p90: Option<f64>,+ pub p95: Option<f64>,+ pub p99: Option<f64>,+ /// The lowest and highest HISTOGRAM buckets that have frames.+ pub min: Option<f64>,+ pub max: Option<f64>,+}++fn ms(text: &str) -> Option<f64> {+ text.trim().strip_suffix("ms")?.trim().parse().ok()+}++/// Reads the text of adb shell dumpsys gfxinfo <package> . Only its frame lines count: "Janky frames+/// (legacy)" and the "gpu percentile" lines are other measures.+pub fn parse_gfxinfo(text: &str) -> Result<Gfxinfo> {+ let mut g = Gfxinfo::default();+ let mut packages = Vec::new();+ let mut sections = 0;+ for line in text.lines().map(str::trim) {+ if let Some(rest) = line.strip_prefix("** Graphics info for pid ") {+ if let Some(pkg) = rest.split_once('[').and_then(|(_, r)| r.split_once(']')).map(|(p, _)| p.trim()) {+ packages.push(pkg.to_string());+ }+ } else if let Some(n) = line.strip_prefix("Total frames rendered:") {+ sections += 1;+ g.frames = n.trim().parse().with_context(|| format!("reading \"{line}\""))?;+ } else if let Some(rest) = line.strip_prefix("Janky frames:") {+ let (count, percent) = rest.split_once('(').unwrap_or((rest, ""));+ g.janky = count.trim().parse().ok();+ g.janky_percent = percent.split_once('%').and_then(|(p, _)| p.trim().parse().ok());+ } else if let Some(v) = line.strip_prefix("50th percentile:") {+ g.p50 = ms(v);+ } else if let Some(v) = line.strip_prefix("90th percentile:") {+ g.p90 = ms(v);+ } else if let Some(v) = line.strip_prefix("95th percentile:") {+ g.p95 = ms(v);+ } else if let Some(v) = line.strip_prefix("99th percentile:") {+ g.p99 = ms(v);+ } else if let Some(buckets) = line.strip_prefix("HISTOGRAM:") {+ let used: Vec<f64> = buckets+ .split_whitespace()+ .filter_map(|b| b.split_once('='))+ .filter(|(_, n)| n.parse::<u64>().is_ok_and(|n| n > 0))+ .filter_map(|(at, _)| ms(at))+ .collect();+ g.min = used.iter().copied().reduce(f64::min);+ g.max = used.iter().copied().reduce(f64::max);+ }+ }+ ensure!(+ sections > 0,+ "no \"Total frames rendered\" line: is this the output of adb shell dumpsys gfxinfo <package> ?"+ );+ ensure!(+ sections == 1,+ "this has the frames of {sections} processes: capture one with adb shell dumpsys gfxinfo <package>"+ );+ g.package = packages.into_iter().next();+ Ok(g)+}++/// The text of a capture: adb output redirected in Windows PowerShell 5 is UTF-16.+fn decode(bytes: &[u8]) -> String {+ if let Some(rest) = bytes.strip_prefix(&[0xff, 0xfe]) {+ let units: Vec<u16> = rest.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect();+ return String::from_utf16_lossy(&units);+ }+ let bytes = bytes.strip_prefix(&[0xef, 0xbb, 0xbf]).unwrap_or(bytes);+ String::from_utf8_lossy(bytes).into_owned()+}++fn get<'a>(doc: &'a Value, path: &[&str]) -> Option<&'a Value> {+ path.iter().try_fold(doc, |v, key| v.get(key)).filter(|v| !v.is_null())+}++fn text(doc: &Value, path: &[&str]) -> Option<String> {+ match get(doc, path)? {+ Value::String(s) if !s.trim().is_empty() => Some(s.clone()),+ Value::Number(n) => Some(n.to_string()),+ _ => None,+ }+}++fn number(doc: &Value, path: &[&str]) -> Option<f64> {+ get(doc, path)?.as_f64()+}++fn object<'a>(doc: &'a mut Value, key: &str) -> Result<&'a mut Map<String, Value>> {+ let map = doc.as_object_mut().context("the report isn't a JSON object")?;+ map.entry(key).or_insert_with(|| json!({})).as_object_mut().with_context(|| format!("{key} isn't an object"))+}++fn text_of(v: &Value) -> String {+ v.as_str().map(String::from).unwrap_or_else(|| v.to_string())+}++/// Puts each given flag in its place, unless the report already says something else there.+fn apply(doc: &mut Value, meta: &Meta) -> Result<()> {+ for (flag, [section, key], value) in meta.fields() {+ let Some(value) = value else { continue };+ let map = object(doc, section)?;+ match map.get(key).filter(|v| !v.is_null()) {+ Some(had) if text_of(had) != text_of(&value) => {+ bail!("{flag} is {} but the report says {section}.{key} is {had}", text_of(&value))+ }+ _ => {+ map.insert(key.into(), value);+ }+ }+ }+ if let Some(at) = &meta.captured_at {+ doc.as_object_mut().context("the report isn't a JSON object")?.insert("captured_at".into(), json!(at));+ }+ Ok(())+}++/// Checks a report against schema version 1 and fills in what follows from it (comparability,+/// sample_status). Refuses a report that would let ART numbers pass for host ones.+pub fn validate(mut doc: Value) -> Result<Value> {+ ensure!(doc.is_object(), "the report isn't a JSON object");+ let version = doc.get("schema_version").and_then(Value::as_u64);+ ensure!(+ version == Some(SCHEMA_VERSION),+ "schema_version is {}: fast-anything reads version {SCHEMA_VERSION}",+ doc.get("schema_version").map_or("missing".into(), Value::to_string)+ );+ let measured_on = text(&doc, &["measured_on"]).context("measured_on is required (ART, host, ...)")?;+ ensure!(text(&doc, &["source"]).is_some(), "source is required, e.g. android-gfxinfo");+ let missing: Vec<String> = REQUIRED+ .iter()+ .filter(|(_, path)| text(&doc, path).is_none())+ .map(|(flag, [a, b])| format!("{flag} ({a}.{b})"))+ .collect();+ if !missing.is_empty() {+ bail!(+ "E_ANDROID_METADATA_REQUIRED: missing {}: fast-anything doesn't guess what was measured from the capture",+ missing.join(", ")+ );+ }+ for section in ["thermal", "samples", "provenance"] {+ ensure!(doc.get(section).is_some_and(Value::is_object), "{section} is required and must be an object");+ }+ if let Some(status) = get(&doc, &["thermal", "status"]) {+ ensure!(+ status.as_u64().is_some_and(|s| s <= 6),+ "thermal.status is {status}: Android's thermal status runs from 0 (none) to 6 (shutdown)"+ );+ }+ let sha = text(&doc, &["provenance", "input_sha256"]).unwrap_or_default();+ ensure!(+ sha.len() == 64 && sha.bytes().all(|b| b.is_ascii_hexdigit()),+ "provenance.input_sha256 is required: the SHA-256 of the raw capture, as 64 hex digits"+ );+ let frames = match get(&doc, &["samples", "frames"]) {+ Some(f) => Some(f.as_u64().with_context(|| format!("samples.frames is {f}: a count of frames"))?),+ None => None,+ };+ let art = measured_on == "ART";+ let warmup = number(&doc, &["samples", "warmup_runs"]).is_some_and(|w| w > 0.0);+ // thermal status 0 means the device wasn't throttling when it was captured+ let cool = number(&doc, &["thermal", "status"]) == Some(0.0);+ let c = object(&mut doc, "comparability")?;+ if art {+ ensure!(+ c.get("host_scan") != Some(&json!(true)),+ "comparability.host_scan is true, but ART numbers can't be compared with a host scan"+ );+ c.insert("host_scan".into(), json!(false));+ c.entry("reason").or_insert_with(|| json!(NOT_COMPARABLE));+ }+ c.entry("warmup").or_insert(json!(warmup));+ c.entry("thermal_controlled").or_insert(json!(cool));+ if frames.is_some_and(|f| f >= MIN_FRAMES) {+ c.entry("sample_status").or_insert(json!("ok"));+ } else {+ c.insert("sample_status".into(), json!("insufficient"));+ let why = match frames {+ Some(f) => format!("{f} frame{}, {MIN_FRAMES} needed for a jank percentage", if f == 1 { "" } else { "s" }),+ None => "no frame count".into(),+ };+ c.entry("sample_reason").or_insert(json!(why));+ }+ Ok(doc)+}++/// A new report for a capture, before the flags are applied.+fn skeleton(format: Format, samples: Map<String, Value>, input: &str, sha: &str) -> Value {+ json!({+ "schema_version": SCHEMA_VERSION,+ "source": format!("android-{}", format.name()),+ "measured_on": "ART",+ "imported_at": util::now_utc(),+ "imported_by": format!("fast-anything {}", env!("CARGO_PKG_VERSION")),+ "device": {},+ "build": {},+ "thermal": { "status": null },+ "samples": samples,+ "provenance": { "input": input, "input_sha256": sha },+ })+}++fn gfxinfo_samples(g: &Gfxinfo) -> Map<String, Value> {+ let mut m = Map::new();+ m.insert("frames".into(), json!(g.frames));+ let fields = [+ ("janky_frames", g.janky.map(|j| j as f64)),+ ("janky_percent", g.janky_percent),+ ("p50_ms", g.p50),+ ("p90_ms", g.p90),+ ("p95_ms", g.p95),+ ("p99_ms", g.p99),+ ("min_ms", g.min),+ ("max_ms", g.max),+ ];+ for (key, value) in fields {+ if let Some(v) = value {+ m.insert(key.into(), if v.fract() == 0.0 { json!(v as u64) } else { json!(v) });+ }+ }+ m+}++/// The checked report for an import, without writing anything.+pub fn build(imp: &Import, input: &Path, stored_as: &str) -> Result<Value> {+ let bytes = fs::read(input).with_context(|| format!("reading {}", input.display()))?;+ ensure!(!bytes.is_empty(), "{} is empty", input.display());+ let sha = sha256::hex(&bytes);+ let mut doc = match imp.format {+ Format::Gfxinfo => {+ let g = parse_gfxinfo(&decode(&bytes))?;+ if let (Some(found), Some(given)) = (&g.package, &imp.meta.package) {+ ensure!(found == given, "the capture is of {found}, not --package {given}");+ }+ skeleton(imp.format, gfxinfo_samples(&g), stored_as, &sha)+ }+ Format::Perfetto | Format::Simpleperf => {+ let mut doc = skeleton(imp.format, Map::new(), stored_as, &sha);+ doc["symbolization"] = json!("not attempted");+ doc["symbolization_reason"] = json!(NOT_PARSED);+ if let Some(path) = &imp.symbols {+ ensure!(path.exists(), "{} doesn't exist", path.display());+ let key = if imp.format == Format::Perfetto { "mapping" } else { "symfs" };+ let mut entry = json!({ "path": util::forward_slashes(&util::absolute(path)) });+ if path.is_file() {+ entry["sha256"] = json!(sha256::file(path)?);+ }+ doc["provenance"][key] = entry;+ }+ doc+ }+ Format::Json => {+ let mut doc: Value =+ serde_json::from_str(&decode(&bytes)).with_context(|| format!("{} isn't JSON", input.display()))?;+ let p = object(&mut doc, "provenance")?;+ if !p.contains_key("input_sha256") {+ p.insert("input".into(), json!(stored_as));+ p.insert("input_sha256".into(), json!(sha));+ }+ doc+ }+ };+ apply(&mut doc, &imp.meta)?;+ validate(doc)+}++/// Where an import goes: .fast/android/<format>, or --out when that's inside .fast/android.+fn out_dir(fast: &Path, imp: &Import) -> Result<PathBuf> {+ let android = fast.join("android");+ let Some(out) = &imp.out else { return Ok(android.join(imp.format.name())) };+ let out = util::absolute(out);+ ensure!(+ out.starts_with(&android) && out != android && !out.components().any(|c| c == Component::ParentDir),+ "--out has to be a folder inside {}, where report looks for on-device evidence",+ android.display()+ );+ Ok(out)+}++/// Imports a capture: checks it and its metadata first, so a refused import writes nothing, then keeps+/// the raw file next to its report.json. Never touches state.json, the scan or the bench.+pub fn import(root: &Path, imp: &Import) -> Result<ImportOut> {+ let root = util::absolute(root);+ ensure!(root.is_dir(), "{} isn't a folder", root.display());+ let input = util::absolute(&imp.input);+ ensure!(input.is_file(), "{} isn't a file", imp.input.display());+ let out = out_dir(&root.join(".fast"), imp)?;+ let name = input.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "input".into());+ let name = if name == "report.json" { "input-report.json".to_string() } else { name };+ let doc = build(imp, &input, &name)?;+ crate::state::Ctx::open(&root)?;+ fs::create_dir_all(&out).with_context(|| format!("creating {}", out.display()))?;+ let raw = out.join(&name);+ if raw != input {+ fs::copy(&input, &raw).with_context(|| format!("copying {} to {}", input.display(), raw.display()))?;+ }+ let (report, tmp) = (out.join("report.json"), out.join("report.json.tmp"));+ fs::write(&tmp, serde_json::to_string_pretty(&doc)?)?;+ fs::rename(&tmp, &report).with_context(|| format!("saving {}", report.display()))?;+ Ok(ImportOut { format: imp.format.name(), report, raw, evidence: doc })+}++/// An imported report, as found under .fast/android.+pub struct Evidence {+ pub path: PathBuf,+ pub doc: Value,+}++/// Every .fast/android/*/report.json, by folder name.+pub fn load(fast: &Path) -> Result<Vec<Evidence>> {+ let Ok(dirs) = fs::read_dir(fast.join("android")) else { return Ok(vec![]) };+ let mut paths: Vec<PathBuf> =+ dirs.filter_map(|d| d.ok()).map(|d| d.path().join("report.json")).filter(|p| p.is_file()).collect();+ paths.sort();+ paths+ .into_iter()+ .map(|path| {+ let doc = fs::read_to_string(&path)+ .ok()+ .and_then(|t| serde_json::from_str(&t).ok())+ .with_context(|| format!("reading {}", path.display()))?;+ Ok(Evidence { path, doc })+ })+ .collect()+}++fn thermal_name(status: u64) -> &'static str {+ ["none", "light", "moderate", "severe", "critical", "emergency", "shutdown"]+ .get(status as usize)+ .copied()+ .unwrap_or("?")+}++/// One line on what the samples say. No jank percentage when there are too few frames for one.+pub fn samples_line(doc: &Value) -> String {+ let n = |key: &str| number(doc, &["samples", key]);+ let runs = match (n("runs"), n("warmup_runs")) {+ (Some(r), Some(w)) if w > 0.0 => format!(" over {r} runs after {w} warm-up"),+ (Some(r), _) => format!(" over {r} runs"),+ _ => String::new(),+ };+ let why = text(doc, &["comparability", "sample_reason"]).unwrap_or_else(|| "too few frames".into());+ let Some(frames) = n("frames") else { return format!("samples{runs}: insufficient ({why})") };+ let s = if frames == 1.0 { "" } else { "s" };+ if text(doc, &["comparability", "sample_status"]).as_deref() != Some("ok") {+ return format!("{frames} frame{s}{runs}: insufficient for a jank percentage ({why})");+ }+ let mut parts = vec![format!("**{frames}** frame{s}{runs}")];+ if let (Some(j), Some(p)) = (n("janky_frames"), n("janky_percent")) {+ parts.push(format!("**{p:.2}%** janky ({j})"));+ }+ let pct: Vec<String> = ["p50", "p90", "p95", "p99"]+ .iter()+ .filter_map(|p| n(&format!("{p}_ms")).map(|v| format!("{p} {v}ms")))+ .collect();+ if !pct.is_empty() {+ parts.push(pct.join(", "));+ }+ if let (Some(lo), Some(hi)) = (n("min_ms"), n("max_ms")) {+ parts.push(format!("frames from {lo}ms to {hi}ms"));+ }+ parts.join("; ")+}++/// The report's "On-device (ART)" section. It never sets these numbers against the host ones.+pub fn markdown(list: &[Evidence], after_host: bool) -> String {+ let mut md = String::from("\n## On-device (ART)\n\n");+ md.push_str(if after_host {+ "Measured on the device and imported with `fast-anything import`, not by fast-anything's bench. ART timings can't be compared with the host numbers above, so no speed-up is computed across the two.\n"+ } else {+ "Measured on the device and imported with `fast-anything import`, not by fast-anything's bench. ART timings can't be compared with host numbers, so no speed-up is computed from them.\n"+ });+ for e in list {+ let t = |path: &[&str]| text(&e.doc, path);+ let with = |out: &mut String, path: &[&str], fmt: &dyn Fn(String) -> String| {+ if let Some(v) = t(path) {+ out.push_str(&fmt(v));+ }+ };+ let mut build = t(&["build", "package"]).unwrap_or_default();+ with(&mut build, &["build", "version_name"], &|v| format!(" {v}"));+ with(&mut build, &["build", "version_code"], &|v| format!(" ({v})"));+ with(&mut build, &["build", "variant"], &|v| format!(", {v}"));+ let source = t(&["source"]).unwrap_or_else(|| "evidence".into());+ md.push_str(&format!("\n### {source}: {build}\n\n"));+ let mut device = t(&["device", "serial"]).unwrap_or_default();+ with(&mut device, &["device", "model"], &|v| format!(" ({v})"));+ with(&mut device, &["device", "android_version"], &|v| format!(", Android {v}"));+ with(&mut device, &["device", "api_level"], &|v| format!(" (API {v})"));+ with(&mut device, &["device", "abi"], &|v| format!(", {v}"));+ md.push_str(&format!("- measured on {}: {device}\n", t(&["measured_on"]).unwrap_or_default()));+ let thermal = match get(&e.doc, &["thermal", "status"]).and_then(Value::as_u64) {+ Some(s) => format!("{s} ({})", thermal_name(s)),+ None => "not recorded".into(),+ };+ md.push_str(&format!("- thermal status at capture: {thermal}\n- {}\n", samples_line(&e.doc)));+ if let Some(s) = t(&["symbolization"]) {+ let why = t(&["symbolization_reason"]).map(|r| format!(": {r}")).unwrap_or_default();+ md.push_str(&format!("- symbolization: {s}{why}\n"));+ }+ let mut prov = format!(+ "- raw input: `{}`, sha256 `{}`",+ t(&["provenance", "input"]).unwrap_or_else(|| "?".into()),+ t(&["provenance", "input_sha256"]).unwrap_or_default()+ );+ with(&mut prov, &["provenance", "captured_by"], &|v| format!(", captured by {v}"));+ with(&mut prov, &["captured_at"], &|v| format!(" at {v}"));+ md.push_str(&prov);+ md.push('\n');+ }+ md+}++#[cfg(test)]+mod tests {+ use super::*;++ const GFXINFO: &str = "Applications Graphics Acceleration Info:+Uptime: 5251932 Realtime: 5251932++** Graphics info for pid 4321 [com.example.app] **++Stats since: 5241383187417ns+Total frames rendered: 412+Janky frames: 91 (22.09%)+Janky frames (legacy): 80 (19.42%)+50th percentile: 7ms+90th percentile: 22ms+95th percentile: 26ms+99th percentile: 34ms+Number Missed Vsync: 10+HISTOGRAM: 5ms=120 6ms=80 7ms=50 8ms=0 22ms=40 34ms=10 101ms=2 150ms=0+50th gpu percentile: 3ms+99th gpu percentile: 12ms+GPU HISTOGRAM: 1ms=300 2ms=100 13ms=12+";++ const ONE_FRAME: &str = "** Graphics info for pid 99 [com.example.app] **+Total frames rendered: 1+Janky frames: 1 (100.00%)+50th percentile: 150ms+90th percentile: 150ms+95th percentile: 150ms+99th percentile: 150ms+";++ fn meta() -> Meta {+ Meta {+ package: Some("com.example.app".into()),+ device: Some("DM9X8".into()),+ android_version: Some("14".into()),+ api: Some(34),+ abi: Some("arm64-v8a".into()),+ build_variant: Some("debug".into()),+ thermal_status: Some(0),+ runs: Some(5),+ warmup: Some(1),+ ..Meta::default()+ }+ }++ /// A project folder with a scan and a bench in it.+ fn project(name: &str) -> PathBuf {+ let dir = std::env::temp_dir().join(format!("fa-android-{name}-{}", std::process::id()));+ let _ = fs::remove_dir_all(&dir);+ fs::create_dir_all(dir.join(".fast")).unwrap();+ fs::write(dir.join(".fast/state.json"), r#"{"version": 1, "wall": 2.5, "bench": {"speedup": 8.0}}"#).unwrap();+ fs::write(dir.join(".fast/profile.json"), "{\"functions\": []}").unwrap();+ dir+ }++ fn input(dir: &Path, name: &str, text: &str) -> PathBuf {+ let path = dir.join(name);+ fs::write(&path, text).unwrap();+ path+ }++ fn import_of(format: Format, input: PathBuf, meta: Meta) -> Import {+ Import { format, input, meta, out: None, symbols: None }+ }++ #[test]+ fn gfxinfo_text_is_read() {+ let g = parse_gfxinfo(GFXINFO).unwrap();+ assert_eq!(+ g,+ Gfxinfo {+ package: Some("com.example.app".into()),+ frames: 412,+ janky: Some(91),+ janky_percent: Some(22.09),+ p50: Some(7.0),+ p90: Some(22.0),+ p95: Some(26.0),+ p99: Some(34.0),+ min: Some(5.0),+ max: Some(101.0),+ },+ "legacy jank, gpu percentiles and the gpu histogram are other measures"+ );+ assert!(parse_gfxinfo("Uptime: 1\n").unwrap_err().to_string().contains("dumpsys gfxinfo"));+ let two = format!("{ONE_FRAME}{ONE_FRAME}");+ assert!(parse_gfxinfo(&two).unwrap_err().to_string().contains("2 processes"));+ let utf16: Vec<u8> =+ [0xff, 0xfe].into_iter().chain(ONE_FRAME.encode_utf16().flat_map(|u| u.to_le_bytes())).collect();+ assert_eq!(parse_gfxinfo(&decode(&utf16)).unwrap().frames, 1, "PowerShell 5 writes UTF-16");+ }++ #[test]+ fn gfxinfo_import_keeps_scan_and_bench_untouched() {+ let dir = project("gfxinfo");+ let (state, profile) =+ (fs::read(dir.join(".fast/state.json")).unwrap(), fs::read(dir.join(".fast/profile.json")).unwrap());+ let raw = input(&dir, "gfxinfo.txt", GFXINFO);+ let out = import(&dir, &import_of(Format::Gfxinfo, raw.clone(), meta())).unwrap();+ assert_eq!(out.report, dir.join(".fast/android/gfxinfo/report.json"));+ assert_eq!(fs::read(&out.raw).unwrap(), GFXINFO.as_bytes(), "the raw input is kept next to the report");+ assert_eq!(fs::read(dir.join(".fast/state.json")).unwrap(), state);+ assert_eq!(fs::read(dir.join(".fast/profile.json")).unwrap(), profile);++ let doc: Value = serde_json::from_str(&fs::read_to_string(&out.report).unwrap()).unwrap();+ assert_eq!(+ (&doc["schema_version"], &doc["measured_on"], &doc["source"]),+ (&json!(1), &json!("ART"), &json!("android-gfxinfo"))+ );+ assert_eq!(+ doc["device"],+ json!({ "serial": "DM9X8", "android_version": "14", "api_level": 34, "abi": "arm64-v8a" })+ );+ assert_eq!(doc["build"], json!({ "package": "com.example.app", "variant": "debug" }));+ assert_eq!(doc["thermal"], json!({ "status": 0 }));+ assert_eq!(+ doc["samples"],+ json!({ "runs": 5, "warmup_runs": 1, "frames": 412, "janky_frames": 91, "janky_percent": 22.09,+ "p50_ms": 7, "p90_ms": 22, "p95_ms": 26, "p99_ms": 34, "min_ms": 5, "max_ms": 101 })+ );+ assert_eq!(+ doc["provenance"],+ json!({ "input": "gfxinfo.txt", "input_sha256": sha256::hex(GFXINFO.as_bytes()) })+ );+ assert_eq!(+ doc["comparability"],+ json!({ "host_scan": false, "reason": NOT_COMPARABLE, "warmup": true, "thermal_controlled": true, "sample_status": "ok" })+ );+ let loaded = load(&dir.join(".fast")).unwrap();+ assert_eq!((loaded.len(), &loaded[0].doc), (1, &doc));+ let _ = fs::remove_dir_all(&dir);+ }++ #[test]+ fn missing_metadata_fails_and_writes_nothing() {+ let dir = std::env::temp_dir().join(format!("fa-android-missing-{}", std::process::id()));+ let _ = fs::remove_dir_all(&dir);+ fs::create_dir_all(&dir).unwrap();+ let raw = input(&dir, "gfxinfo.txt", GFXINFO);+ for (flag, strip) in [+ ("--device", (|m: &mut Meta| m.device = None) as fn(&mut Meta)),+ ("--package", |m| m.package = None),+ ("--android-version", |m| m.android_version = None),+ ("--abi", |m| m.abi = None),+ ("--build-variant", |m| m.build_variant = None),+ ] {+ let mut m = meta();+ strip(&mut m);+ let err = import(&dir, &import_of(Format::Gfxinfo, raw.clone(), m)).unwrap_err().to_string();+ assert!(err.starts_with("E_ANDROID_METADATA_REQUIRED: missing ") && err.contains(flag), "{err}");+ assert!(!dir.join(".fast").exists(), "nothing written");+ }+ let wrong = Meta { package: Some("com.other".into()), ..meta() };+ let err = import(&dir, &import_of(Format::Gfxinfo, raw.clone(), wrong)).unwrap_err().to_string();+ assert!(err.contains("the capture is of com.example.app"), "{err}");+ let outside = Import { out: Some(dir.join(".fast")), ..import_of(Format::Gfxinfo, raw, meta()) };+ assert!(import(&dir, &outside).unwrap_err().to_string().contains("--out has to be a folder inside"));+ assert!(!dir.join(".fast").exists());+ let _ = fs::remove_dir_all(&dir);+ }++ #[test]+ fn one_frame_is_insufficient_not_a_headline() {+ let dir = project("one-frame");+ let out = import(&dir, &import_of(Format::Gfxinfo, input(&dir, "one.txt", ONE_FRAME), meta())).unwrap();+ let c = &out.evidence["comparability"];+ assert_eq!(+ (&c["sample_status"], &c["sample_reason"]),+ (&json!("insufficient"), &json!("1 frame, 30 needed for a jank percentage"))+ );+ let md = markdown(&load(&dir.join(".fast")).unwrap(), true);+ assert!(md.contains("1 frame over 5 runs after 1 warm-up: insufficient for a jank percentage"), "{md}");+ assert!(!md.contains("100") && !md.contains("150ms"), "no percentage or percentile headline: {md}");+ let _ = fs::remove_dir_all(&dir);+ }++ #[test]+ fn traces_are_kept_but_not_parsed() {+ let dir = project("perfetto");+ let trace = input(&dir, "trace.perfetto-trace", "\x0a\x05trace bytes");+ let mapping = input(&dir, "mapping.txt", "com.example.A -> a:\n");+ let imp = Import { symbols: Some(mapping), ..import_of(Format::Perfetto, trace, meta()) };+ let out = import(&dir, &imp).unwrap();+ assert_eq!(out.report, dir.join(".fast/android/perfetto/report.json"));+ assert_eq!(fs::read(&out.raw).unwrap(), b"\x0a\x05trace bytes");+ let doc = &out.evidence;+ assert_eq!((&doc["source"], &doc["symbolization"]), (&json!("android-perfetto"), &json!("not attempted")));+ assert_eq!(doc["provenance"]["input_sha256"], json!(sha256::hex(b"\x0a\x05trace bytes")));+ assert_eq!(doc["provenance"]["mapping"]["sha256"], json!(sha256::hex(b"com.example.A -> a:\n")));+ assert_eq!(doc["comparability"]["sample_status"], json!("insufficient"), "a trace has no frame count here");+ assert!(markdown(&load(&dir.join(".fast")).unwrap(), true).contains("- symbolization: not attempted: "));++ let perf = input(&dir, "perf.data", "PERFILE2");+ let out = import(&dir, &import_of(Format::Simpleperf, perf, meta())).unwrap();+ assert_eq!(out.report, dir.join(".fast/android/simpleperf/report.json"));+ assert_eq!(load(&dir.join(".fast")).unwrap().len(), 2);+ let empty = input(&dir, "empty.data", "");+ assert!(+ import(&dir, &import_of(Format::Simpleperf, empty, meta())).unwrap_err().to_string().contains("is empty")+ );+ let _ = fs::remove_dir_all(&dir);+ }++ fn summary() -> Value {+ json!({+ "schema_version": 1,+ "source": "android-macrobenchmark",+ "measured_on": "ART",+ "device": { "serial": "DM9X8", "android_version": 14, "abi": "arm64-v8a" },+ "build": { "package": "com.example.app", "variant": "benchmark" },+ "thermal": { "status": 1 },+ "samples": { "frames": 600, "janky_percent": 3.5 },+ "provenance": { "input": "run.json", "input_sha256": "ab".repeat(32) },+ "comparability": { "host_scan": false, "sample_status": "ok" }+ })+ }++ #[test]+ fn json_reports_are_checked() {+ let ok = validate(summary()).unwrap();+ assert_eq!(ok["comparability"]["reason"], json!(NOT_COMPARABLE));+ assert_eq!(ok["comparability"]["thermal_controlled"], json!(false), "status 1 is throttling");++ let broken = |change: fn(&mut Value)| {+ let mut doc = summary();+ change(&mut doc);+ validate(doc).unwrap_err().to_string()+ };+ assert!(broken(|d| d["schema_version"] = json!(2)).contains("reads version 1"));+ assert!(+ broken(|d| {+ d.as_object_mut().unwrap().remove("measured_on");+ })+ .contains("measured_on is required")+ );+ assert!(broken(|d| d["comparability"]["host_scan"] = json!(true)).contains("can't be compared"));+ assert!(broken(|d| d["provenance"]["input_sha256"] = json!("abc")).contains("input_sha256 is required"));+ assert!(broken(|d| d["thermal"]["status"] = json!(7)).contains("0 (none) to 6"));+ assert!(+ broken(|d| {+ d.as_object_mut().unwrap().remove("thermal");+ })+ .contains("thermal is required")+ );+ assert!(broken(|d| d["build"] = json!({})).contains(+ "E_ANDROID_METADATA_REQUIRED: missing --package (build.package), --build-variant (build.variant)"+ ));++ let mut few = summary();+ few["samples"]["frames"] = json!(12);+ assert_eq!(+ validate(few).unwrap()["comparability"]["sample_status"],+ json!("insufficient"),+ "12 frames aren't ok"+ );+ }++ #[test]+ fn json_import_fills_in_flags_and_refuses_conflicts() {+ let dir = project("json");+ let mut doc = summary();+ doc["build"].as_object_mut().unwrap().remove("variant");+ doc["provenance"].as_object_mut().unwrap().remove("input_sha256");+ let text = doc.to_string();+ let file = input(&dir, "report.json", &text);+ let missing = import(&dir, &import_of(Format::Json, file.clone(), Meta::default())).unwrap_err();+ assert!(missing.to_string().contains("--build-variant (build.variant)"), "{missing}");+ let flags =+ Meta { build_variant: Some("benchmark".into()), android_version: Some("14".into()), ..Meta::default() };+ let out = import(&dir, &import_of(Format::Json, file.clone(), flags)).unwrap();+ assert_eq!(out.report, dir.join(".fast/android/json/report.json"));+ assert_eq!(+ out.raw,+ dir.join(".fast/android/json/input-report.json"),+ "the raw input doesn't overwrite the report"+ );+ assert_eq!(out.evidence["build"]["variant"], json!("benchmark"));+ assert_eq!(out.evidence["provenance"]["input_sha256"], json!(sha256::hex(text.as_bytes())));+ let conflict = Meta { device: Some("OTHER".into()), ..Meta::default() };+ let err = import(&dir, &import_of(Format::Json, file, conflict)).unwrap_err().to_string();+ assert!(err.contains("--device is OTHER but the report says device.serial is \"DM9X8\""), "{err}");+ let _ = fs::remove_dir_all(&dir);+ }+}diff --git a/src/main.rs b/src/main.rsindex 99cad4b..4e8471b 100644--- a/src/main.rs+++ b/src/main.rs@@ -1,6 +1,7 @@//! fast-anything: find the slow 5% of your app, rewrite just that in Rust, prove it behaves the same.mod agent;+mod android;mod backend;mod bench;mod cfamily;@@ -19,13 +20,14 @@ mod python;mod r;mod report;mod ruby;+mod sha256;mod show;mod state;mod ui;mod util;use anyhow::{Result, bail, ensure};-use clap::{Parser, Subcommand};+use clap::{Args, Parser, Subcommand};use serde_json::{Value, json};use std::path::{Path, PathBuf};use std::process::ExitCode;@@ -124,8 +126,17 @@ enum Cmd {#[arg(long, default_value_t = 5)]runs: usize,},+ /// Attach evidence measured on an Android device; the report keeps it apart from the host numbers+ Import {+ #[command(subcommand)]+ what: Box<ImportCmd>,+ },/// Write a shareable report (markdown + SVG card)- Report,+ Report {+ /// Require on-device (ART) evidence from fast-anything import (it's included whenever there is some)+ #[arg(long)]+ include_android: bool,+ },/// Show the last scan and every portStatus,/// Check that Rust, Python/Node and your agents are installed@@ -137,6 +148,111 @@ enum Cmd {Replay { job: PathBuf },}+#[derive(Subcommand)]+enum ImportCmd {+ /// Frame timing: the text of adb shell dumpsys gfxinfo <package>+ Gfxinfo(ImportArgs),+ /// A report in fast-anything's on-device schema (version 1), e.g. made in CI+ Json(ImportArgs),+ /// A Perfetto trace, kept with its hash (not parsed yet)+ Perfetto {+ #[command(flatten)]+ args: ImportArgs,+ /// The R8/ProGuard mapping.txt of the build, recorded with the trace+ #[arg(long)]+ mapping: Option<PathBuf>,+ },+ /// A simpleperf perf.data, kept with its hash (not parsed yet)+ Simpleperf {+ #[command(flatten)]+ args: ImportArgs,+ /// The folder with the build's unstripped libraries, recorded with the profile+ #[arg(long)]+ symfs: Option<PathBuf>,+ },+}++/// What was measured: --device, --package, --android-version, --abi and --build-variant are required,+/// as flags or (for json) in the file.+#[derive(Args)]+struct ImportArgs {+ /// The file to import+ #[arg(long)]+ input: PathBuf,+ /// The app's package, e.g. com.example.app+ #[arg(long)]+ package: Option<String>,+ /// The device's serial, as adb devices lists it+ #[arg(long)]+ device: Option<String>,+ /// The device's model, e.g. 22041216G+ #[arg(long)]+ model: Option<String>,+ /// e.g. 14+ #[arg(long)]+ android_version: Option<String>,+ /// API level, e.g. 34+ #[arg(long)]+ api: Option<u32>,+ /// e.g. arm64-v8a+ #[arg(long)]+ abi: Option<String>,+ /// e.g. debug, release, benchmark+ #[arg(long)]+ build_variant: Option<String>,+ #[arg(long)]+ version_name: Option<String>,+ #[arg(long)]+ version_code: Option<u64>,+ /// Android's thermal status at capture, 0 (none) to 6 (shutdown)+ #[arg(long, value_parser = clap::value_parser!(u8).range(0..=6))]+ thermal_status: Option<u8>,+ /// Measured runs in the capture+ #[arg(long)]+ runs: Option<u32>,+ /// Warm-up runs before them+ #[arg(long)]+ warmup: Option<u32>,+ /// When it was captured, e.g. 2026-11-24T18:40:00Z+ #[arg(long)]+ captured_at: Option<String>,+ /// What captured it, e.g. "androidforge 1.0.0"+ #[arg(long)]+ captured_by: Option<String>,+ /// Where to keep it, inside .fast/android (default: .fast/android/<format>)+ #[arg(long)]+ out: Option<PathBuf>,+}++impl ImportCmd {+ fn into_import(self) -> android::Import {+ use android::Format;+ let (format, a, symbols) = match self {+ ImportCmd::Gfxinfo(a) => (Format::Gfxinfo, a, None),+ ImportCmd::Json(a) => (Format::Json, a, None),+ ImportCmd::Perfetto { args, mapping } => (Format::Perfetto, args, mapping),+ ImportCmd::Simpleperf { args, symfs } => (Format::Simpleperf, args, symfs),+ };+ let meta = android::Meta {+ package: a.package,+ device: a.device,+ model: a.model,+ android_version: a.android_version,+ api: a.api,+ abi: a.abi,+ build_variant: a.build_variant,+ version_name: a.version_name,+ version_code: a.version_code,+ thermal_status: a.thermal_status,+ runs: a.runs,+ warmup: a.warmup,+ captured_at: a.captured_at,+ captured_by: a.captured_by,+ };+ android::Import { format, input: a.input, meta, out: a.out, symbols }+ }+}+fn main() -> ExitCode {let cli = Cli::parse();let root = util::absolute(&cli.dir);@@ -157,7 +273,7 @@ fn run(cmd: Cmd, root: &Path, json: bool) -> Result<()> {match &cmd {Cmd::Go { .. } | Cmd::Build | Cmd::Clean { .. } | Cmd::Mcp => {bail!(- "--json isn't supported for this command (supported: scan, port, verify, enable, disable, bench, report, status, doctor)"+ "--json isn't supported for this command (supported: scan, port, verify, enable, disable, bench, import, report, status, doctor)");}_ => ui::set_quiet(true),@@ -279,8 +395,17 @@ fn run(cmd: Cmd, root: &Path, json: bool) -> Result<()> {Ok(())}}- Cmd::Report => {- let out = pipeline::report(root)?;+ Cmd::Import { what } => {+ let out = android::import(root, &(*what).into_import())?;+ if json {+ show::json(&out)+ } else {+ show::import(&out);+ Ok(())+ }+ }+ Cmd::Report { include_android } => {+ let out = pipeline::report(root, include_android)?;if json {show::json(&out)} else {@@ -371,6 +496,43 @@ mod tests {assert!(matches!(app_flag.cmd, Cmd::Scan { command, .. } if command == ["python", "app.py", "--json"]));}+ #[test]+ fn import_lists_its_formats() {+ let err = Cli::try_parse_from(["fast-anything", "import", "--help"]).err().unwrap();+ assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp, "exits 0");+ let help = err.to_string();+ for format in ["gfxinfo", "perfetto", "simpleperf", "json"] {+ assert!(help.contains(format), "{help}");+ }+ let cli = Cli::try_parse_from([+ "fast-anything",+ "import",+ "perfetto",+ "--input",+ "t.pftrace",+ "--mapping",+ "mapping.txt",+ "--thermal-status",+ "6",+ "--json",+ ])+ .unwrap();+ assert!(cli.json);+ let Cmd::Import { what } = cli.cmd else { panic!("import") };+ let imp = (*what).into_import();+ assert_eq!((imp.format, imp.meta.thermal_status), (android::Format::Perfetto, Some(6)));+ assert_eq!(imp.symbols, Some(PathBuf::from("mapping.txt")));+ assert!(+ Cli::try_parse_from(["fast-anything", "import", "gfxinfo", "--input", "g.txt", "--thermal-status", "7"])+ .is_err()+ );+ assert!(Cli::try_parse_from(["fast-anything", "import", "gfxinfo"]).is_err(), "--input is required");+ assert!(matches!(+ Cli::try_parse_from(["fast-anything", "report", "--include-android"]).unwrap().cmd,+ Cmd::Report { include_android: true }+ ));+ }+#[test]fn json_rejects_unsupported_subcommands() {let root = Path::new(".");diff --git a/src/mcp.rs b/src/mcp.rsindex 74d64ce..0c3e1ec 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -105,7 +105,7 @@ fn call(params: &Value) -> Result<Value> {"fast_enable" => json!({ "enabled": pipeline::enable(&root, id, args["force"].as_bool().unwrap_or(false))? }),"fast_disable" => json!({ "disabled": pipeline::disable(&root, id)? }),"fast_bench" => serde_json::to_value(pipeline::bench(&root, args["runs"].as_u64().unwrap_or(5) as usize)?)?,- "fast_report" => serde_json::to_value(pipeline::report(&root)?)?,+ "fast_report" => serde_json::to_value(pipeline::report(&root, false)?)?,"fast_status" => {let ctx = state::Ctx::open(&root)?;json!({ "command": ctx.state.command, "ports": ctx.state.ports, "bench": ctx.state.bench })diff --git a/src/pipeline.rs b/src/pipeline.rsindex d1f3796..dd67e0c 100644--- a/src/pipeline.rs+++ b/src/pipeline.rs@@ -403,8 +403,8 @@ pub fn bench(root: &Path, runs: usize) -> Result<BenchResult> {Ok(result)}-pub fn report(root: &Path) -> Result<ReportOut> {- report::write(&Ctx::open(root)?)+pub fn report(root: &Path, include_android: bool) -> Result<ReportOut> {+ report::write(&Ctx::open(root)?, include_android)}/// What the agent gets told after a failed attempt, or a right one that's slower than the original.@@ -598,7 +598,7 @@ pub fn go(}ensure!(enabled > 0, "no port both passed verification and ran faster");show::bench(&bench(root, runs)?);- show::report(&report(root)?);+ show::report(&report(root, false)?);Ok(())}diff --git a/src/report.rs b/src/report.rsindex f0f64cf..d3b0597 100644--- a/src/report.rs+++ b/src/report.rs@@ -1,10 +1,11 @@//! A report worth sharing: markdown for the PR, an SVG card for the README / socials.+use crate::android::{self, Evidence};use crate::bench::{BenchResult, Scope, Verdict};use crate::hotspot::Hotspot;use crate::state::{Ctx, Port, State};use crate::util::{nanos, secs, thousands, times};-use anyhow::{Context, Result};+use anyhow::{Result, bail, ensure};use serde::Serialize;use std::fs;use std::path::PathBuf;@@ -12,7 +13,12 @@ use std::path::PathBuf;#[derive(Serialize)]pub struct ReportOut {pub markdown: PathBuf,- pub svg: PathBuf,+ /// None when there's no bench, only on-device evidence.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub svg: Option<PathBuf>,+ /// The imported on-device reports the markdown includes.+ #[serde(skip_serializing_if = "Vec::is_empty")]+ pub android: Vec<PathBuf>,pub text: String,}@@ -159,8 +165,11 @@ fn enabled(s: &State) -> Vec<&Port> {s.ports.values().filter(|p| p.enabled).collect()}-/// The report for the last bench `b`; `s` holds the ports and the scan that chose them.-pub fn markdown(project: &str, b: &BenchResult, s: &State) -> String {+const MADE_WITH: &str = "\n<sub>Made with [fast-anything](https://github.com/EL4CTEO/fast-anything)</sub>\n";++/// The report for the last bench `b`; `s` holds the ports and the scan that chose them. On-device+/// evidence gets its own section, and the host part is labelled as such.+pub fn markdown(project: &str, b: &BenchResult, s: &State, on_device: &[Evidence]) -> String {// results saved before rounds and the A/A control have no verdict and no rangelet how = if b.verdict.is_some() {format!(@@ -174,8 +183,9 @@ pub fn markdown(project: &str, b: &BenchResult, s: &State) -> String {};let (head, word) = headline(b.speedup);let scope = b.scope.as_ref().and_then(scope_line).map(|l| format!("{l}\n\n")).unwrap_or_default();+ let host = if on_device.is_empty() { "" } else { "## Host scan and bench\n\n" };let mut md = format!(- "# ⚡ {project}: {head} {word}\n\n{scope}`{}` went from **{}** to **{}** ({how}).\n\n",+ "# ⚡ {project}: {head} {word}\n\n{host}{scope}`{}` went from **{}** to **{}** ({how}).\n\n",b.command.join(" "),secs(b.before.median),secs(b.after.median),@@ -209,19 +219,43 @@ pub fn markdown(project: &str, b: &BenchResult, s: &State) -> String {md.push_str(&format!("| `{}` | {per_call} | {}/{} recorded real calls |\n", p.id, v.passed, v.total));}md.push_str(&scan_section(s, b));- md.push_str("\nThe original code stays in place: set `FAST_ANYTHING_DISABLE=1` to run it, or `fast-anything disable <id>` to remove the switch.\n\n<sub>Made with [fast-anything](https://github.com/EL4CTEO/fast-anything)</sub>\n");+ md.push_str("\nThe original code stays in place: set `FAST_ANYTHING_DISABLE=1` to run it, or `fast-anything disable <id>` to remove the switch.\n");+ if !on_device.is_empty() {+ md.push_str(&android::markdown(on_device, true));+ }+ md.push_str(MADE_WITH);md}-pub fn write(ctx: &Ctx) -> Result<ReportOut> {- let b = ctx.state.bench.as_ref().context("no benchmark yet: run fast-anything bench")?;+/// The report when there's on-device evidence but no host bench.+fn on_device_only(project: &str, on_device: &[Evidence]) -> String {+ format!("# ⚡ {project}: on-device evidence\n{}{MADE_WITH}", android::markdown(on_device, false))+}++/// `include_android`: fail when there's no on-device evidence (it's included whenever there is).+pub fn write(ctx: &Ctx, include_android: bool) -> Result<ReportOut> {+ let on_device = android::load(&ctx.fast)?;+ ensure!(+ !include_android || !on_device.is_empty(),+ "no on-device evidence in .fast/android yet: run fast-anything import gfxinfo --input gfxinfo.txt ... first"+ );let project = ctx.root.file_name().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "your app".into());+ let md_path = ctx.fast.join("report.md");+ let android: Vec<PathBuf> = on_device.iter().map(|e| e.path.clone()).collect();+ let Some(b) = ctx.state.bench.as_ref() else {+ if on_device.is_empty() {+ bail!("no benchmark yet: run fast-anything bench");+ }+ let text = on_device_only(&project, &on_device);+ fs::write(&md_path, &text)?;+ return Ok(ReportOut { markdown: md_path, svg: None, android, text });+ };let ports = enabled(&ctx.state);- let text = markdown(&project, b, &ctx.state);- let (md_path, svg_path) = (ctx.fast.join("report.md"), ctx.fast.join("report.svg"));+ let text = markdown(&project, b, &ctx.state, &on_device);+ let svg_path = ctx.fast.join("report.svg");fs::write(&md_path, &text)?;fs::write(&svg_path, svg(&project, b, &ports))?;- Ok(ReportOut { markdown: md_path, svg: svg_path, text })+ Ok(ReportOut { markdown: md_path, svg: Some(svg_path), android, text })}#[cfg(test)]@@ -230,6 +264,47 @@ mod tests {use crate::bench::{Spread, Stats};use crate::state::Verify;+ #[test]+ fn on_device_evidence_gets_its_own_section() {+ let doc = crate::android::validate(serde_json::json!({+ "schema_version": 1, "source": "android-gfxinfo", "measured_on": "ART",+ "device": { "serial": "DM9X8", "android_version": "14", "abi": "arm64-v8a" },+ "build": { "package": "com.example.app", "variant": "debug" },+ "thermal": { "status": 0 },+ "samples": { "frames": 412, "janky_frames": 91, "janky_percent": 22.09, "p90_ms": 22 },+ "provenance": { "input": "gfxinfo.txt", "input_sha256": "ab".repeat(32) }+ }))+ .unwrap();+ let evidence = [Evidence { path: ".fast/android/gfxinfo/report.json".into(), doc }];+ let b = BenchResult {+ before: Stats { median: 4.0, ..Stats::default() },+ after: Stats { median: 0.5, ..Stats::default() },+ speedup: 8.0,+ ..BenchResult::default()+ };+ let md = markdown("app", &b, &State::default(), &evidence);+ assert!(md.starts_with("# ⚡ app: 8.0× faster\n\n## Host scan and bench\n\n"), "{md}");+ let device = &md[md.find("## On-device (ART)").expect("an on-device section")..];+ assert!(device.contains("no speed-up is computed across the two"), "{device}");+ assert!(!device.contains('×') && !device.contains("faster"), "no speed-up across host and ART: {device}");+ for part in [+ "measured on ART: DM9X8, Android 14, arm64-v8a",+ "thermal status at capture: 0 (none)",+ "**22.09%** janky (91)",+ "p90 22ms",+ "sha256 `abab",+ ] {+ assert!(device.contains(part), "{part}\n{device}");+ }+ assert!(md.ends_with(MADE_WITH));+ assert!(!markdown("app", &b, &State::default(), &[]).contains("On-device"));+ let alone = on_device_only("app", &evidence);+ assert!(+ alone.starts_with("# ⚡ app: on-device evidence\n\n## On-device (ART)") && !alone.contains("Host"),+ "{alone}"+ );+ }+#[test]fn svg_is_escaped_and_scaled() {let b = BenchResult {@@ -252,7 +327,7 @@ mod tests {speedup: 8.42 / 9.27,..BenchResult::default()};- let (md, card) = (markdown("app", &b, &State::default()), svg("app", &b, &[]));+ let (md, card) = (markdown("app", &b, &State::default(), &[]), svg("app", &b, &[]));assert!(md.starts_with("# ⚡ app: 1.1× slower"), "{md}");assert!(card.contains(">slower</tspan>") && !card.contains("faster"), "{card}");}@@ -274,7 +349,7 @@ mod tests {cpu_speedup: Some(95.2),..BenchResult::default()};- let md = markdown("demo", &b, &State::default());+ let md = markdown("demo", &b, &State::default(), &[]);assert!(md.contains("median of 5 rounds") && md.contains("228–240×"), "{md}");assert!(md.contains("- CPU time 95×: **11.90s** → **125ms**"), "{md}");assert!(@@ -285,7 +360,7 @@ mod tests {assert!(card.contains("234×") && card.contains("CPU time 95×"));let old = BenchResult { speedup: 8.0, ..BenchResult::default() };- let md = markdown("demo", &old, &State::default());+ let md = markdown("demo", &old, &State::default(), &[]);assert!(!md.contains("CPU time") && !md.contains("A/A") && md.contains("interleaved"), "{md}");}@@ -299,7 +374,7 @@ mod tests {cpu: Some("AMD Ryzen 5 PRO 4650GE".into()),};let b = BenchResult { speedup: 8.0, scope: Some(scope.clone()), ..BenchResult::default() };- let md = markdown("app", &b, &State::default());+ let md = markdown("app", &b, &State::default(), &[]);assert!(md.starts_with("# ⚡ app: 8.0× faster\n\nMeasured 2026-11-26 on AMD Ryzen 5 PRO 4650GE (12 logical CPUs), Linux x86_64.\n\n`"@@ -314,7 +389,7 @@ mod tests {let old: BenchResult =serde_json::from_str(r#"{"runs": 5, "before": {"median": 4.0}, "speedup": 8.0}"#).unwrap();assert!(old.scope.is_none());- let md = markdown("app", &old, &State::default());+ let md = markdown("app", &old, &State::default(), &[]);assert!(md.starts_with("# ⚡ app: 8.0× faster\n\n`") && !md.contains("Measured"), "{md}");}@@ -373,7 +448,7 @@ mod tests {speedup: 8.0,..BenchResult::default()};- let md = markdown("app", &b, &s);+ let md = markdown("app", &b, &s, &[]);let (table, section, stays) = (md.find("| `fuzzy.lev` | **30×**").unwrap(),md.find("## How the ports were chosen").unwrap(),@@ -399,11 +474,11 @@ mod tests {assert!(md.contains("2 more functions from the scan ranked lower."), "{md}");let stopped_early = State { wall: 2.0, ..s.clone() };- let md = markdown("app", &b, &stopped_early);+ let md = markdown("app", &b, &stopped_early, &[]);assert!(md.contains("less than the original's **4.00s** median above") && !md.contains("as long"), "{md}");let no_scan = State { hotspots: vec![], ..s };- let md = markdown("app", &b, &no_scan);+ let md = markdown("app", &b, &no_scan, &[]);assert!(!md.contains("How the ports were chosen") && md.contains("| `fuzzy.lev` |"), "{md}");}}diff --git a/src/show.rs b/src/show.rsindex 52eeae7..cb73fc8 100644--- a/src/show.rs+++ b/src/show.rs@@ -1,5 +1,6 @@//! How results look in the terminal.+use crate::android::ImportOut;use crate::bench::{BenchResult, OUTPUT_VARIES, Verdict};use crate::pipeline::{PortOut, ScanOut};use crate::report::ReportOut;@@ -220,7 +221,33 @@ pub fn bench(b: &BenchResult) {pub fn report(r: &ReportOut) {ui::ok(format!("report: {}", r.markdown.display()));- ui::ok(format!("share card: {}", r.svg.display()));+ if let Some(svg) = &r.svg {+ ui::ok(format!("share card: {}", svg.display()));+ }+ if !r.android.is_empty() {+ ui::ok(format!(+ "with on-device (ART) evidence from {} import{}",+ r.android.len(),+ if r.android.len() == 1 { "" } else { "s" }+ ));+ }+}++pub fn import(out: &ImportOut) {+ ui::ok(format!("imported {}: {}", bold(out.format), crate::android::samples_line(&out.evidence).replace("**", "")));+ if out.evidence.pointer("/comparability/sample_status").and_then(|s| s.as_str()) != Some("ok")+ && out.format == "gfxinfo"+ {+ ui::warn(format!(+ "too few frames for a jank percentage: capture at least {} (scroll or animate while it records)",+ crate::android::MIN_FRAMES+ ));+ }+ if let Some(why) = out.evidence.get("symbolization_reason").and_then(|s| s.as_str()) {+ ui::line(format!(" {}", dim(format!("symbolization not attempted: {why}"))));+ }+ ui::ok(format!("saved {} (raw input: {})", out.report.display(), out.raw.display()));+ next("fast-anything report");}pub fn status(ctx: &Ctx) {diff --git a/src/util.rs b/src/util.rsindex bd37802..e7e06a4 100644--- a/src/util.rs+++ b/src/util.rs@@ -186,9 +186,19 @@ pub fn civil_date(days: i64) -> (i64, u32, u32) {/// Today's date in UTC, e.g. "2026-11-26"; None if the clock is before 1970.pub fn today() -> Option<String> {+ now_utc().map(|t| t[..10].to_string())+}++/// The time in UTC, e.g. "2026-11-26T18:40:00Z"; None if the clock is before 1970.+pub fn now_utc() -> Option<String> {let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).ok()?.as_secs();+ Some(utc(secs))+}++fn utc(secs: u64) -> String {let (y, m, d) = civil_date((secs / 86_400) as i64);- Some(format!("{y:04}-{m:02}-{d:02}"))+ let t = secs % 86_400;+ format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z", t / 3600, t / 60 % 60, t % 60)}#[cfg(test)]@@ -225,5 +235,8 @@ mod tests {assert_eq!(civil_date(-1), (1969, 12, 31));let today = today().unwrap();assert!(today.len() == 10 && today.starts_with("20"), "{today}");+ assert_eq!(utc(0), "1970-01-01T00:00:00Z");+ assert_eq!(utc(20_783 * 86_400 + 18 * 3600 + 40 * 60 + 5), "2026-11-26T18:40:05Z");+ assert!(now_utc().unwrap().starts_with(&today));}}