//! Flat-image open benchmark (#10259): File › Open of an ordinary JPEG or PNG, phase by phase, //! with the peak resident memory of each case. //! //! ```sh //! cargo run --release -p photocraft-ui-egui --example flat_open_bench -- [--quick] [--reps 5] [--only jpeg-36] [--cpu] [--cache DIR] [--json out.json] //! cargo run --release -p photocraft-ui-egui --example flat_open_bench -- --case png16-36 # one case, one process //! ``` //! //! Cases (`--only` matches a substring of the key): `jpeg-{24,36}` (RGB, quality 90, 4:2:0), //! `png8-{24,36}` (RGB 8-bit), `png16-{24,36}` (RGB 16-bit), `rgba8-{24,36}` (RGB 8-bit with //! partial alpha) and `jpeg-24-orient6` (EXIF orientation 6). 24 MP is 6000x4000, 36 MP is //! 7360x4912; `--quick` uses 1200x800 for every case (the row names keep the nominal size). //! //! The inputs are generated in-process (smooth gradients, soft blobs and seeded noise, so the //! compression is realistic) with the workspace's own encoders and cached in `--cache` (default: //! `$PHOTOCRAFT_FIXTURE_DIR`, else `fixtures/` in the target directory), never committed. //! //! Rows per case, in ms over `--reps` repetitions: //! - `import`: `import_for_open_with`, the real File › Open path (decode, orientation, tiles) //! - `admission`: `Session::open_document` of that document //! - `decode`: the codec alone, orientation kept as stored //! - `orient`: `Image::oriented` with the file's EXIF orientation (a no-op for an upright file) //! - `tiles`: `image_to_document` on the decoded image (layout conversion, tile construction, prune) //! - `first composite`: the canvas's first full refresh (GPU, or the CPU compositor with `--cpu` //! or without an adapter), as `perf_scenarios` times it //! - `editable`: import + admission + first composite of one open, the "decode + first composite" rows //! //! Peak memory: without `--case` the bench starts itself once per case (`current_exe`), so each //! case's `VmHWM` is its own process-lifetime high-water mark. `peak_process_after_import_bytes` //! is read after real import and admission; `peak_process_after_all_passes_bytes` is read after //! standalone decode/orient/tiles, GPU setup and warm-up, and editable-open passes. The latter //! is not an isolated composite or editable-open peak. Row `peak_rss_provenance` identifies //! which reading its `peak_rss_bytes` carries. Off Linux the peak is `n/a`. //! A case run directly with `--case` generates a missing fixture in its own process, which //! raises its peak: run the bench once without `--case` first. use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::time::Instant; use eframe::egui_wgpu::RenderState; use photocraft_codecs::{ChannelLayout, DecodeOptions, EncodeOptions, Format, Image}; use photocraft_engine::Session; use photocraft_ui_egui::gpu_canvas::GpuCanvas; use rayon::prelude::*; use serde_json::{Value, json}; /// Prefix of the line a `--case` process prints for its parent. const RESULT_PREFIX: &str = "FLAT_OPEN_RESULT "; /// The phases, in report order. const PHASES: [&str; 7] = ["import", "admission", "decode", "orient", "tiles", "first composite", "editable"]; #[derive(Clone, Copy, PartialEq)] enum Kind { Jpeg, Png8, Png16, Rgba8, } struct Case { key: &'static str, label: &'static str, kind: Kind, /// 36 MP (7360x4912) instead of 24 MP (6000x4000). big: bool, /// EXIF orientation 6 (the stored image is turned upright on open). orient6: bool, } const CASES: [Case; 9] = [ Case { key: "jpeg-24", label: "open 24 MP JPEG RGB8", kind: Kind::Jpeg, big: false, orient6: false }, Case { key: "jpeg-36", label: "open 36 MP JPEG RGB8", kind: Kind::Jpeg, big: true, orient6: false }, Case { key: "png8-24", label: "open 24 MP PNG RGB8", kind: Kind::Png8, big: false, orient6: false }, Case { key: "png8-36", label: "open 36 MP PNG RGB8", kind: Kind::Png8, big: true, orient6: false }, Case { key: "png16-24", label: "open 24 MP PNG RGB16", kind: Kind::Png16, big: false, orient6: false }, Case { key: "png16-36", label: "open 36 MP PNG RGB16", kind: Kind::Png16, big: true, orient6: false }, Case { key: "rgba8-24", label: "open 24 MP PNG RGBA8", kind: Kind::Rgba8, big: false, orient6: false }, Case { key: "rgba8-36", label: "open 36 MP PNG RGBA8", kind: Kind::Rgba8, big: true, orient6: false }, Case { key: "jpeg-24-orient6", label: "open 24 MP JPEG RGB8 (EXIF orientation 6)", kind: Kind::Jpeg, big: false, orient6: true }, ]; impl Case { fn size(&self, quick: bool) -> (u32, u32) { match (quick, self.big) { (true, _) => (1200, 800), (false, false) => (6000, 4000), (false, true) => (7360, 4912), } } fn ext(&self) -> &'static str { if self.kind == Kind::Jpeg { "jpg" } else { "png" } } fn file_name(&self, w: u32, h: u32) -> String { format!("flat_open_{}_{w}x{h}.{}", self.key, self.ext()) } } /// Return the value following a named command-line option. fn arg(args: &[String], name: &str) -> Option { args.iter().position(|a| a == name).and_then(|i| args.get(i + 1).cloned()) } fn ms(t: Instant) -> f64 { t.elapsed().as_secs_f64() * 1000.0 } /// `/fixtures`: the binary lives in `/release/examples/`. fn default_cache() -> PathBuf { if let Some(d) = std::env::var_os("PHOTOCRAFT_FIXTURE_DIR") { return PathBuf::from(d); } std::env::current_exe().ok().and_then(|e| e.ancestors().nth(3).map(|t| t.join("fixtures"))).unwrap_or_else(std::env::temp_dir) } /// Deterministic noise in [-0.5, 0.5) from the pixel position and a channel/seed. fn noise(x: usize, y: usize, seed: usize) -> f32 { let mut h = (x.wrapping_mul(73_856_093) ^ y.wrapping_mul(19_349_663) ^ seed.wrapping_mul(83_492_791)) as u64; h = (h ^ (h >> 15)).wrapping_mul(0x2c1b_3c6d); h = (h ^ (h >> 12)).wrapping_mul(0x297a_2d39); ((h ^ (h >> 15)) % 4096) as f32 / 4096.0 - 0.5 } /// A photo-like sample in [0, 1] for channel `c` (3 is alpha: opaque at the centre, down to /// about 15 % at the corners, with fine noise, so most pixels are partly transparent). fn sample(x: usize, y: usize, w: usize, h: usize, c: usize) -> f32 { let (fx, fy) = (x as f32 / w as f32, y as f32 / h as f32); let n = noise(x, y, c); let blob = (-(((fx - 0.4) * 3.0).powi(2) + ((fy - 0.55) * 4.0).powi(2))).exp(); let v = match c { 0 => 0.55 + 0.17 * (fx * 6.0).sin() + 0.3 * blob + n * 0.06, 1 => 0.45 + 0.12 * (fy * 9.0 + fx * 2.0).cos() + 0.2 * blob + n * 0.06, 2 => 0.35 + 0.4 * fy - 0.2 * blob + n * 0.06, _ => 1.0 - 0.85 * (((fx - 0.5).powi(2) + (fy - 0.5).powi(2)) * 2.0).min(1.0) + n * 0.04, }; v.clamp(0.0, 1.0) } /// A TIFF-structured EXIF block holding only the orientation tag. fn exif_orientation_block(o: u16) -> Vec { let mut b = b"II*\0\x08\0\0\0\x01\0".to_vec(); b.extend_from_slice(&[0x12, 0x01, 3, 0, 1, 0, 0, 0]); b.extend_from_slice(&o.to_le_bytes()); b.extend_from_slice(&[0, 0, 0, 0, 0, 0]); b } /// The encoded file for `case` at `w`x`h`. fn generate(case: &Case, w: u32, h: u32) -> Result, String> { let (wu, hu) = (w as usize, h as usize); let (layout, ch) = if case.kind == Kind::Rgba8 { (ChannelLayout::Rgba, 4) } else { (ChannelLayout::Rgb, 3) }; let img = if case.kind == Kind::Png16 { let mut px = vec![0u16; wu * hu * ch]; px.par_chunks_mut(wu * ch).enumerate().for_each(|(y, row)| { for (i, v) in row.iter_mut().enumerate() { *v = (sample(i / ch, y, wu, hu, i % ch) * 65535.0 + 0.5) as u16; } }); Image::from_u16(w, h, layout, &px).map_err(|e| e.to_string())? } else { let mut px = vec![0u8; wu * hu * ch]; px.par_chunks_mut(wu * ch).enumerate().for_each(|(y, row)| { for (i, v) in row.iter_mut().enumerate() { *v = (sample(i / ch, y, wu, hu, i % ch) * 255.0 + 0.5) as u8; } }); Image::from_u8(w, h, layout, px).map_err(|e| e.to_string())? }; let format = if case.kind == Kind::Jpeg { Format::Jpeg } else { Format::Png }; let opts = EncodeOptions { jpeg_quality: 90, jpeg_chroma_subsampling: true, ..Default::default() }; let mut bytes = photocraft_codecs::encode(&img, format, &opts).map_err(|e| e.to_string())?; if case.orient6 { // The encoder writes the pixels as given and resets the orientation, so the tag goes in // as an Exif APP1 segment right after the JPEG's SOI marker. let exif = exif_orientation_block(6); let len = u16::try_from(exif.len() + 8).map_err(|e| e.to_string())?; let mut seg = vec![0xFF, 0xE1]; seg.extend_from_slice(&len.to_be_bytes()); seg.extend_from_slice(b"Exif\0\0"); seg.extend_from_slice(&exif); bytes.splice(2..2, seg); } Ok(bytes) } /// The cached fixture for `case`, generated (and written atomically) when missing. fn ensure_fixture(case: &Case, dir: &Path, quick: bool) -> Result { let (w, h) = case.size(quick); let path = dir.join(case.file_name(w, h)); if path.metadata().is_ok_and(|m| m.len() > 0) { return Ok(path); } std::fs::create_dir_all(dir).map_err(|e| format!("create {}: {e}", dir.display()))?; let t = Instant::now(); let bytes = generate(case, w, h).map_err(|e| format!("generate {}: {e}", case.key))?; let tmp = path.with_extension(format!("{}.tmp{}", case.ext(), std::process::id())); std::fs::write(&tmp, &bytes).map_err(|e| format!("write {}: {e}", tmp.display()))?; std::fs::rename(&tmp, &path).map_err(|e| format!("rename {}: {e}", path.display()))?; eprintln!("generated {} ({:.1} MB) in {:.1} s", path.display(), bytes.len() as f64 / 1e6, t.elapsed().as_secs_f64()); Ok(path) } /// A headless GPU canvas on a device created like the app's. fn canvas() -> Option<(GpuCanvas, RenderState)> { let setup = photocraft_ui_egui::gpu_canvas::wgpu_setup(); let rs = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| egui_kittest::wgpu::create_render_state(setup, Default::default()))).ok()?; Some((GpuCanvas::new(&rs), rs)) } /// The canvas's first full refresh; waits for the GPU like presenting the next frame would. fn refresh(gpu: Option<&(GpuCanvas, RenderState)>, s: &Session) -> Result { let st = s.active().ok_or("no document")?; let t = Instant::now(); match gpu { Some((g, rs)) => { g.refresh(st.doc.id.0, &st.doc, None, None); let _ = rs.device.poll(eframe::wgpu::PollType::Wait { submission_index: None, timeout: None }); } None => { std::hint::black_box(photocraft_compose::render(&st.doc, st.doc.bounds())); } } Ok(ms(t)) } /// `import_for_open_with`, File › Open's real path. fn import_once(name: &str, bytes: &[u8]) -> Result { let opts = photocraft_io::ImportOptions::default(); photocraft_io::import_for_open_with(name, bytes, &opts, &Default::default()).map(|(r, _)| r.document).map_err(|e| e.to_string()) } /// Runs every measurement for one case in this process; the result is the JSON line's payload. fn run_case(case: &Case, dir: &Path, quick: bool, reps: usize, cpu: bool) -> Result { let (w, h) = case.size(quick); let path = ensure_fixture(case, dir, quick)?; let bytes = std::fs::read(&path).map_err(|e| format!("read {}: {e}", path.display()))?; let name = format!("photo.{}", case.ext()); let want = if case.orient6 { (h, w) } else { (w, h) }; let mut ph: Vec> = vec![Vec::new(); PHASES.len()]; let mut put = |i: usize, v: f64| { if let Some(p) = ph.get_mut(i) { p.push(v); } }; // Pass 1: the real path and the admission, before anything else raises the process's peak. for _ in 0..reps { let t = Instant::now(); let doc = import_once(&name, &bytes)?; put(0, ms(t)); if doc.size.width != want.0 || doc.size.height != want.1 { return Err(format!("{}: opened {}x{}, expected {}x{}", case.key, doc.size.width, doc.size.height, want.0, want.1)); } let t = Instant::now(); let mut s = Session::new(); s.open_document(doc, Some(name.clone())); put(1, ms(t)); } let peak_process_after_import = photocraft_testkit::perf::process_peak_rss_bytes(); // Pass 2: the phases of the import on their own. let format = photocraft_codecs::detect(&bytes).ok_or("unknown format")?; let keep = DecodeOptions { keep_orientation: true, ..Default::default() }; for _ in 0..reps { let t = Instant::now(); let img = photocraft_codecs::decode_as_with(format, &bytes, &keep).map_err(|e| e.to_string())?; put(2, ms(t)); let o = photocraft_codecs::exif_orientation(img.meta.exif.as_deref().unwrap_or_default()); let t = Instant::now(); let upright = img.oriented(o).map_err(|e| e.to_string())?; put(3, ms(t)); let t = Instant::now(); let r = photocraft_io::image_to_document(&name, &upright).map_err(|e| e.to_string())?; put(4, ms(t)); drop(r); } // Pass 3: one whole open as perf_scenarios times it: import, admission, first composite. let gpu = if cpu { None } else { canvas() }; let adapter = gpu.as_ref().map(|(_, rs)| rs.adapter.get_info().name); // One untimed open first, so the GPU pipeline and adapter warm-up is not in the first sample. { let mut s = Session::new(); s.open_document(import_once(&name, &bytes)?, Some(name.clone())); refresh(gpu.as_ref(), &s)?; } for _ in 0..reps { let t = Instant::now(); let doc = import_once(&name, &bytes)?; let mut s = Session::new(); s.open_document(doc, Some(name.clone())); let open = ms(t); let c = refresh(gpu.as_ref(), &s)?; put(5, c); put(6, open + c); } let peak_process_after_all_passes = photocraft_testkit::perf::process_peak_rss_bytes(); let phases: serde_json::Map = PHASES.iter().zip(ph).map(|(n, v)| ((*n).to_string(), json!(v))).collect(); Ok(json!({ "key": case.key, "label": case.label, "width": w, "height": h, "file_bytes": bytes.len(), "peak_process_after_import_bytes": peak_process_after_import, "peak_process_after_all_passes_bytes": peak_process_after_all_passes, "adapter": adapter, "phases": phases, })) } /// The report rows of one case's result: `