Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

43.7 KB
Raw Blame
//! The speed test in Settings: rustyfiles against the other file managers on this machine, and
//! its own engines against Windows'. Blocking: run [`run`] on a background thread.
//!
//! Test folders live in `<app dir>/bench` and are kept between runs (making 100 000 files takes a
//! while); copies and extractions go to a scratch folder that is emptied after each run.
//! rustyfiles is measured as a fresh process with its own settings folder, the way the others
//! start: the user's settings are neither read nor changed.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use rf_win::bench::{App, AppKind, Machine, OpenResult};

/// Each app opens each folder this many times (after one warm-up run that isn't counted).
pub const RUNS: usize = 3;
const OPEN_TIMEOUT: Duration = Duration::from_secs(45);

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum OpenTest {
    /// An empty folder: how long the app takes to start.
    Start,
    Files1k,
    Files10k,
    Files100k,
    /// 2 000 pictures: thumbnails.
    Images,
}

impl OpenTest {
    pub const ALL: [OpenTest; 5] = [OpenTest::Start, OpenTest::Files1k, OpenTest::Files10k, OpenTest::Files100k, OpenTest::Images];

    /// The test folder's name (also what the window titles are matched by).
    pub fn folder(self) -> &'static str {
        match self {
            OpenTest::Start => "rf-bench empty",
            OpenTest::Files1k => "rf-bench 1000",
            OpenTest::Files10k => "rf-bench 10000",
            OpenTest::Files100k => "rf-bench 100000",
            OpenTest::Images => "rf-bench images",
        }
    }
}

/// What to run.
#[derive(Debug, Clone)]
pub struct Plan {
    /// rustyfiles first, then the apps to compare with.
    pub apps: Vec<App>,
    pub open: Vec<OpenTest>,
    pub copy: bool,
    pub engines: bool,
}

/// One app's runs of one open test.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AppRuns {
    pub app: AppKind,
    pub runs: Vec<OpenResult>,
    /// Why it couldn't be measured, if it couldn't.
    pub error: Option<String>,
}

impl AppRuns {
    /// The run with the median settle time.
    pub fn median(&self) -> Option<&OpenResult> {
        let mut v: Vec<&OpenResult> = self.runs.iter().collect();
        v.sort_by(|a, b| a.settled_ms.total_cmp(&b.settled_ms));
        v.get(v.len() / 2).copied()
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OpenResults {
    pub test: OpenTest,
    pub apps: Vec<AppRuns>,
}

/// Ours against Windows' engine, in ms (median of the runs); `None` when it failed.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Versus {
    pub ours_ms: Option<f64>,
    pub windows_ms: Option<f64>,
    pub bytes: u64,
    pub files: u64,
    /// CPU time of each side (medians, ms) and the private memory it took on top (MB). Results
    /// saved before these were measured have none.
    #[serde(default)]
    pub ours_cpu_ms: Option<f64>,
    #[serde(default)]
    pub windows_cpu_ms: Option<f64>,
    #[serde(default)]
    pub ours_mb: Option<f64>,
    #[serde(default)]
    pub windows_mb: Option<f64>,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Engines {
    /// 10 000 small files in 100 folders.
    pub copy_small: Option<Versus>,
    /// One 1 GB file.
    pub copy_big: Option<Versus>,
    /// Extracting a ZIP of 10 000 files: rarchive against Windows' compressed folders.
    pub unzip: Option<Versus>,
    /// Searching the test folders by name (ms, entries looked at).
    pub search_ms: Option<f64>,
    pub search_entries: u64,
    /// The search's CPU time (ms) and extra memory (MB).
    #[serde(default)]
    pub search_cpu_ms: Option<f64>,
    #[serde(default)]
    pub search_mb: Option<f64>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Results {
    /// Unix ms.
    pub when: i64,
    pub version: String,
    pub machine: Machine,
    pub open: Vec<OpenResults>,
    pub engines: Engines,
}

/// Progress for the UI.
#[derive(Debug, Clone)]
pub enum Progress {
    /// Making the test folders (what, done 0..1).
    Preparing(String, f32),
    /// Step `i` of `n`.
    Step { i: usize, n: usize, label: String },
}

/// `%LOCALAPPDATA%/rustyfiles/bench`: local, never the roaming settings folder (a roaming
/// profile would carry 100 000 test files around).
pub fn bench_dir() -> PathBuf {
    std::env::var_os("LOCALAPPDATA").map_or_else(std::env::temp_dir, PathBuf::from).join("rustyfiles").join("bench")
}

/// rustyfiles itself, started fresh with its own settings folder.
pub fn rustyfiles_app() -> App {
    let exe = std::env::current_exe().map(|p| p.with_file_name("rustyfiles.exe")).unwrap_or_default();
    let cfg = bench_dir().join("rustyfiles-settings");
    App {
        kind: AppKind::Rustyfiles,
        exe,
        env: vec![
            ("RUSTYFILES_MULTI_INSTANCE".into(), "1".into()),
            ("RUSTYFILES_CONFIG_DIR".into(), cfg.to_string_lossy().into_owned()),
            ("RUSTYFILES_MUTE".into(), "1".into()),
        ],
    }
}

// ------------------------------------------------------------------ test folders

const EXTS: [&str; 10] = ["txt", "pdf", "docx", "zip", "mp3", "rs", "md", "json", "csv", "log"];

fn make_files(dir: &Path, n: usize, report: &dyn Fn(f32)) -> std::io::Result<()> {
    let done = dir.join(".rf-bench-done");
    if done.exists() {
        return Ok(());
    }
    let _ = std::fs::remove_dir_all(dir);
    std::fs::create_dir_all(dir)?;
    let body = vec![b'x'; 600];
    for i in 0..n {
        // small enough to stay inside the file record: no clusters used
        let len = (i * 37) % 600;
        std::fs::write(dir.join(format!("file {i:06}.{}", EXTS[i % EXTS.len()])), &body[..len])?;
        if i % 2000 == 0 {
            report(i as f32 / n as f32);
        }
    }
    hide(&done)?;
    Ok(())
}

/// The "done" marker, hidden so it doesn't show in the listings.
fn hide(p: &Path) -> std::io::Result<()> {
    std::fs::write(p, b"")?;
    use std::os::windows::ffi::OsStrExt;
    let w: Vec<u16> = p.as_os_str().encode_wide().chain([0]).collect();
    unsafe {
        let _ = windows::Win32::Storage::FileSystem::SetFileAttributesW(
            windows::core::PCWSTR(w.as_ptr()),
            windows::Win32::Storage::FileSystem::FILE_ATTRIBUTE_HIDDEN,
        );
    }
    Ok(())
}

fn make_images(dir: &Path, n: usize, report: &dyn Fn(f32)) -> anyhow::Result<()> {
    let done = dir.join(".rf-bench-done");
    if done.exists() {
        return Ok(());
    }
    let _ = std::fs::remove_dir_all(dir);
    std::fs::create_dir_all(dir)?;
    for i in 0..n {
        let (w, h) = (320u32, 240u32);
        let hue = (i * 47 % 360) as f32;
        let img = image::RgbImage::from_fn(w, h, |x, y| {
            let t = (x + y) as f32 / (w + h) as f32;
            let c = |o: f32| (((hue + o) / 360.0 * std::f32::consts::TAU).sin() * 0.5 + 0.5) * 200.0 * (0.5 + t * 0.5) + 30.0;
            image::Rgb([c(0.0) as u8, c(120.0) as u8, c(240.0) as u8])
        });
        img.save(dir.join(format!("picture {i:04}.png")))?;
        if i % 50 == 0 {
            report(i as f32 / n as f32);
        }
    }
    hide(&done)?;
    Ok(())
}

/// 10 000 files of 4 KB in 100 folders, and one 1 GB file.
fn make_copy_sources(root: &Path, report: &dyn Fn(f32)) -> std::io::Result<(PathBuf, PathBuf)> {
    let small = root.join("copy small");
    let big = root.join("copy big.bin");
    let done = root.join(".rf-bench-copy-done");
    if done.exists() && big.exists() {
        return Ok((small, big));
    }
    let _ = std::fs::remove_dir_all(&small);
    for d in 0..100 {
        let dir = small.join(format!("folder {d:03}"));
        std::fs::create_dir_all(&dir)?;
        for f in 0..100u32 {
            let body: Vec<u8> = (0..4096u32).map(|i| ((i + f * 7 + d) % 251) as u8).collect();
            std::fs::write(dir.join(format!("file {f:03}.dat")), body)?;
        }
        report(d as f32 / 200.0);
    }
    use std::io::Write;
    let chunk: Vec<u8> = (0..(4u32 << 20)).map(|i| (i.wrapping_mul(2654435761) >> 24) as u8).collect();
    let mut f = std::io::BufWriter::new(std::fs::File::create(&big)?);
    for k in 0..256 {
        f.write_all(&chunk)?;
        if k % 16 == 0 {
            report(0.5 + k as f32 / 512.0);
        }
    }
    f.flush()?;
    hide(&done)?;
    Ok((small, big))
}

/// Writes the benchmark instance's settings: nothing that would stay running or reach out.
/// The folder an open test lists (empty, files, pictures), made afresh.
fn make_open_folder(dir: &Path, t: OpenTest, report: &dyn Fn(f32)) -> anyhow::Result<()> {
    match t {
        OpenTest::Start => std::fs::create_dir_all(dir)?,
        OpenTest::Files1k => make_files(dir, 1_000, report)?,
        OpenTest::Files10k => make_files(dir, 10_000, report)?,
        OpenTest::Files100k => make_files(dir, 100_000, report)?,
        OpenTest::Images => make_images(dir, 2_000, report)?,
    }
    Ok(())
}

fn prepare_rustyfiles_settings() -> std::io::Result<()> {
    let cfg = bench_dir().join("rustyfiles-settings");
    std::fs::create_dir_all(&cfg)?;
    std::fs::write(
        cfg.join("settings.toml"),
        "restore_session = false\nrun_in_background = false\nauto_update = false\nstar_asked = true\nstarts = 1\n",
    )
}

/// Removes the test folders.
pub fn delete_test_files() -> std::io::Result<()> {
    let d = bench_dir();
    if d.exists() { std::fs::remove_dir_all(d) } else { Ok(()) }
}

fn free_space(p: &Path) -> u64 {
    use std::os::windows::ffi::OsStrExt;
    let w: Vec<u16> = p.as_os_str().encode_wide().chain([0]).collect();
    let mut free = 0u64;
    unsafe {
        let _ = windows::Win32::Storage::FileSystem::GetDiskFreeSpaceExW(windows::core::PCWSTR(w.as_ptr()), Some(&mut free), None, None);
    }
    free
}

// ------------------------------------------------------------------ running

fn median(mut v: Vec<f64>) -> Option<f64> {
    if v.is_empty() {
        return None;
    }
    v.sort_by(f64::total_cmp);
    let n = v.len();
    // an even count averages the middle two (taking v[n / 2] of two runs took the slower one)
    Some(if n % 2 == 0 { (v[n / 2 - 1] + v[n / 2]) / 2.0 } else { v[n / 2] })
}

/// Our engine exactly as a paste uses it (thread count chosen for the disks).
fn our_copy(sources: &[PathBuf], dest: &Path) -> anyhow::Result<()> {
    let tracker = Arc::new(rf_win::fileops::Tracker::new(Box::new(|_, _, _| {}), Arc::new(AtomicBool::new(false))));
    anyhow::ensure!(rf_win::fastcopy::transfer(rf_win::fileops::Transfer::Copy, sources, dest, tracker)?, "the copy stopped");
    Ok(())
}

fn versus(runs: usize, scratch: &Path, ours: &dyn Fn(&Path) -> anyhow::Result<()>, windows: &dyn Fn(&Path) -> anyhow::Result<()>, cancel: &AtomicBool) -> Versus {
    let (mut a, mut b) = (Vec::new(), Vec::new());
    let (mut ac, mut bc, mut am, mut bm) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
    for r in 0..runs {
        if cancel.load(Ordering::Relaxed) {
            break;
        }
        for (which, f) in [(0, ours), (1, windows)] {
            let dest = scratch.join(format!("run {r} {which}"));
            let _ = std::fs::remove_dir_all(&dest);
            if std::fs::create_dir_all(&dest).is_err() {
                continue;
            }
            match rf_win::bench::measure_in_process(|| f(&dest)) {
                (Ok(()), cost) => {
                    [&mut a, &mut b][which].push(cost.ms);
                    [&mut ac, &mut bc][which].push(cost.cpu_ms);
                    [&mut am, &mut bm][which].push(cost.extra_mb);
                }
                (Err(e), _) => log::warn!("bench: {} failed: {e:#}", if which == 0 { "ours" } else { "windows" }),
            }
            let _ = std::fs::remove_dir_all(&dest);
        }
        // a side that takes this long is measured once (Windows' ZIP and small-file copy run
        // for half a minute each)
        if a.iter().chain(&b).any(|&ms| ms > 8000.0) {
            break;
        }
    }
    Versus { ours_ms: median(a), windows_ms: median(b), ours_cpu_ms: median(ac), windows_cpu_ms: median(bc), ours_mb: median(am), windows_mb: median(bm), ..Default::default() }
}

/// Runs `plan`. `progress` is called from this thread; `cancel` stops between steps.
// ------------------------------------------------------------------ Files' session

/// Files remembers the tabs open when its last window closed and opens them again on start
/// ("continue where you left off"). Every window the test opens and closes would become the
/// user's "last session" (their next Files start showing an rf-bench folder), and each test start
/// would also restore the previous test's tab. For the test, Files starts without a session;
/// afterwards the user's two settings are written back as they were (also when the test fails
/// or is cancelled: on drop), and again a few seconds later in case Files saved late.
struct FilesSession {
    path: PathBuf,
    saved: Vec<(&'static str, Option<serde_json::Value>)>,
}

const FILES_SESSION_KEYS: [&str; 3] = ["ContinueLastSessionOnStartUp", "LastSessionTabList", "LastSessionSelectedTabIndex"];

impl FilesSession {
    fn settings_path() -> Option<PathBuf> {
        let base = PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join("Packages");
        // the Store package (Files_1y0xx7n9077q4) or a sideloaded one (FilesStable_…, FilesPreview_…)
        std::fs::read_dir(&base)
            .ok()?
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("Files")))
            .map(|p| p.join("LocalState").join("settings").join("user_settings.json"))
            .find(|p| p.is_file())
    }

    fn read(path: &Path) -> Option<serde_json::Map<String, serde_json::Value>> {
        let text = std::fs::read_to_string(path).ok()?;
        match serde_json::from_str(text.trim_start_matches('\u{feff}')).ok()? {
            serde_json::Value::Object(m) => Some(m),
            _ => None,
        }
    }

    /// Files' own layout: two-space indent, CRLF.
    fn write(path: &Path, m: &serde_json::Map<String, serde_json::Value>) -> std::io::Result<()> {
        let text = serde_json::to_string_pretty(m).map_err(std::io::Error::other)?.replace('\n', "\r\n");
        let tmp = path.with_extension("json.rf-bench");
        std::fs::write(&tmp, text)?;
        std::fs::rename(&tmp, path)
    }

    fn begin() -> Option<Self> {
        let path = Self::settings_path()?;
        let mut m = Self::read(&path)?;
        let saved = FILES_SESSION_KEYS.iter().map(|&k| (k, m.get(k).cloned())).collect();
        m.insert("ContinueLastSessionOnStartUp".into(), serde_json::Value::Bool(false));
        if let Err(e) = Self::write(&path, &m) {
            log::warn!("bench: Files settings not changed: {e}");
            return None;
        }
        log::info!("bench: Files starts without its last session during the test");
        Some(Self { path, saved })
    }

    fn restore(path: &Path, saved: &[(&'static str, Option<serde_json::Value>)]) -> bool {
        let Some(mut m) = Self::read(path) else { return false };
        if saved.iter().all(|(k, v)| m.get(*k) == v.as_ref()) {
            return true;
        }
        for (k, v) in saved {
            match v {
                Some(v) => m.insert((*k).into(), v.clone()),
                None => m.remove(*k),
            };
        }
        Self::write(path, &m).is_ok()
    }
}

impl Drop for FilesSession {
    fn drop(&mut self) {
        if !Self::restore(&self.path, &self.saved) {
            log::warn!("bench: Files settings could not be put back");
        }
        let (path, saved) = (self.path.clone(), std::mem::take(&mut self.saved));
        std::thread::spawn(move || {
            for _ in 0..3 {
                std::thread::sleep(Duration::from_secs(5));
                Self::restore(&path, &saved);
            }
        });
    }
}

pub fn run(plan: &Plan, progress: &dyn Fn(Progress), cancel: &AtomicBool) -> anyhow::Result<Results> {
    let _files_session = if plan.apps.iter().any(|a| a.kind == AppKind::Files) { FilesSession::begin() } else { None };
    let root = bench_dir();
    std::fs::create_dir_all(&root)?;
    prepare_rustyfiles_settings()?;
    let machine = rf_win::bench::machine(&root);

    // test folders
    for t in &plan.open {
        let dir = root.join(t.folder());
        let label = t.folder().to_string();
        let report = |f: f32| progress(Progress::Preparing(label.clone(), f));
        make_open_folder(&dir, *t, &report)?;
        if cancel.load(Ordering::Relaxed) {
            anyhow::bail!("cancelled");
        }
    }
    let copy_ok = plan.copy && free_space(&root) > 4 << 30;
    let copy_src = if copy_ok || plan.engines {
        Some(make_copy_sources(&root, &|f| progress(Progress::Preparing("copy".into(), f)))?)
    } else {
        None
    };

    let n_open = plan.open.len() * plan.apps.len() * (RUNS + 1);
    let n = n_open + if copy_ok { 2 } else { 0 } + if plan.engines { 2 } else { 0 };
    let mut i = 0usize;
    let mut step = |label: String| {
        i += 1;
        progress(Progress::Step { i, n, label });
    };

    let mut open = Vec::new();
    for &t in &plan.open {
        let dir = root.join(t.folder());
        let mut per_app: Vec<AppRuns> = plan.apps.iter().map(|a| AppRuns { app: a.kind, runs: Vec::new(), error: None }).collect();
        // run 0 warms up (caches, the app's own first start); apps take turns each round
        for r in 0..=RUNS {
            for (k, app) in plan.apps.iter().enumerate() {
                if cancel.load(Ordering::Relaxed) {
                    anyhow::bail!("cancelled");
                }
                step(format!("{} · {}", app.kind.name(), t.folder()));
                if per_app[k].error.is_some() {
                    continue;
                }
                // an app that takes no folder from the command line (Sigma) is measured only on its start
                if !app.kind.opens_a_folder() && t != OpenTest::Start {
                    per_app[k].error = Some("opens no folder given on the command line".into());
                    continue;
                }
                match rf_win::bench::measure_open(app, &dir, OPEN_TIMEOUT, cancel) {
                    Ok(res) if r > 0 => per_app[k].runs.push(res),
                    Ok(_) => {}
                    Err(e) => {
                        log::warn!("bench: {} on {}: {e:#}", app.kind.name(), t.folder());
                        per_app[k].error = Some(format!("{e:#}"));
                    }
                }
                // let the closed window's process settle before the next start
                std::thread::sleep(Duration::from_millis(400));
            }
        }
        open.push(OpenResults { test: t, apps: per_app });
    }
    // a window an app opened after its measurement gave up on it is asked to close too
    if !plan.open.is_empty() {
        let left = rf_win::bench::close_test_windows();
        if left > 0 {
            log::info!("bench: closed {left} test window(s) left open");
        }
    }

    let mut engines = Engines::default();
    let scratch = root.join("scratch");
    let _ = std::fs::remove_dir_all(&scratch);
    std::fs::create_dir_all(&scratch)?;
    if let (true, Some((small, big))) = (copy_ok, &copy_src) {
        step("copy: 10 000 small files".into());
        let s = [small.clone()];
        let mut v = versus(2, &scratch, &|d| our_copy(&s, d), &|d| rf_win::bench::shell_copy_quiet(&s, d), cancel);
        v.files = 10_000;
        v.bytes = 10_000 * 4096;
        engines.copy_small = Some(v);
        step("copy: one 1 GB file".into());
        let b = [big.clone()];
        let mut v = versus(2, &scratch, &|d| our_copy(&b, d), &|d| rf_win::bench::shell_copy_quiet(&b, d), cancel);
        v.files = 1;
        v.bytes = 1 << 30;
        engines.copy_big = Some(v);
    }
    if plan.engines && !cancel.load(Ordering::Relaxed) {
        step("ZIP".into());
        let src = root.join(OpenTest::Files10k.folder());
        let zip = root.join("rf-bench 10000.zip");
        if !zip.exists() && src.exists() {
            let _ = rf_archive::compress(&[src.clone()], &zip, rf_archive::Format::Zip, 5, &mut |_| {}, &|| false);
        }
        if zip.exists() {
            // Windows: copying out of the compressed folder (what Explorer's Extract does)
            let inner = zip.join(OpenTest::Files10k.folder());
            let mut v = versus(
                2,
                &scratch,
                &|d| rf_archive::extract(&zip, d, &mut |_| {}, &|| false).map(|_| ()).map_err(|e| anyhow::anyhow!("{e}")),
                &|d| rf_win::bench::shell_copy_quiet(std::slice::from_ref(&inner), d),
                cancel,
            );
            v.files = 10_000;
            engines.unzip = Some(v);
        }
        step("search".into());
        let q = rf_win::search::SearchQuery { root: root.clone(), pattern: "file 0999".into(), include_hidden: false, max_results: 50_000 };
        if let (Ok(s), cost) = rf_win::bench::measure_in_process(|| rf_win::search::search(&q, &|| false, &mut |_| {})) {
            engines.search_ms = Some(cost.ms);
            engines.search_cpu_ms = Some(cost.cpu_ms);
            engines.search_mb = Some(cost.extra_mb);
            engines.search_entries = s.entries_scanned as u64;
        }
    }
    let _ = std::fs::remove_dir_all(&scratch);

    let when = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_millis() as i64);
    let r = Results { when, version: crate::installer::version(), machine, open, engines };
    let _ = std::fs::write(root.join("results.json"), serde_json::to_vec_pretty(&r).unwrap_or_default());
    // the test folders (over a gigabyte) are not left behind: only the results stay; the next test makes them again
    if let Ok(rd) = std::fs::read_dir(&root) {
        for e in rd.flatten().filter(|e| e.file_name() != "results.json") {
            let p = e.path();
            let _ = if p.is_dir() { std::fs::remove_dir_all(&p) } else { std::fs::remove_file(&p) };
        }
    }
    Ok(r)
}

/// The last results, if a test was run before.
pub fn last_results() -> Option<Results> {
    serde_json::from_slice(&std::fs::read(bench_dir().join("results.json")).ok()?).ok()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The engine comparison reports what each side cost: a real copy of 300 small files with
    /// our engine and with Windows' (in this process), both measured.
    #[test]
    fn engine_comparison_reports_cpu_and_memory() {
        // (Windows' engine is one of the two compared)
        if rf_win::engine_guard::not_run_without_shell("engine_comparison_reports_cpu_and_memory") {
            return;
        }
        let root = std::env::temp_dir().join(format!("rf-bench-cost-{}", std::process::id()));
        let src = root.join("src");
        std::fs::create_dir_all(&src).unwrap();
        for i in 0..300 {
            std::fs::write(src.join(format!("f{i}.bin")), vec![7u8; 8192]).unwrap();
        }
        let scratch = root.join("scratch");
        std::fs::create_dir_all(&scratch).unwrap();
        let s = [src.clone()];
        let v = versus(1, &scratch, &|d| our_copy(&s, d), &|d| rf_win::bench::shell_copy_quiet(&s, d), &AtomicBool::new(false));
        assert!(v.ours_ms.is_some() && v.windows_ms.is_some(), "{v:?}");
        assert!(v.ours_cpu_ms.is_some_and(|c| c >= 0.0) && v.windows_cpu_ms.is_some_and(|c| c > 0.0), "{v:?}");
        assert!(v.ours_mb.is_some_and(|m| m >= 0.0) && v.windows_mb.is_some_and(|m| m >= 0.0), "{v:?}");
        eprintln!("{v:?}");
        let _ = std::fs::remove_dir_all(&root);
    }

    /// Files' session settings are put back exactly, other settings Files changed meanwhile kept.
    #[test]
    fn files_session_is_put_back() {
        let dir = std::env::temp_dir().join(format!("rf-files-session-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("user_settings.json");
        std::fs::write(&path, "{\r\n  \"ShowShelfPane\": false,\r\n  \"ContinueLastSessionOnStartUp\": true,\r\n  \"LastSessionTabList\": [\r\n    \"C:\\\\Users\"\r\n  ]\r\n}").unwrap();
        let mut m = FilesSession::read(&path).unwrap();
        let saved: Vec<_> = FILES_SESSION_KEYS.iter().map(|&k| (k, m.get(k).cloned())).collect();
        // what the test leaves: Files' own writes of the bench tabs, plus an unrelated change
        m.insert("ContinueLastSessionOnStartUp".into(), false.into());
        m.insert("LastSessionTabList".into(), serde_json::json!(["C:\\rf-bench 1000"]));
        m.insert("LastSessionSelectedTabIndex".into(), 0.into());
        m.insert("ShowShelfPane".into(), true.into());
        FilesSession::write(&path, &m).unwrap();
        assert!(FilesSession::restore(&path, &saved));
        let back = FilesSession::read(&path).unwrap();
        assert_eq!(back["ContinueLastSessionOnStartUp"], true);
        assert_eq!(back["LastSessionTabList"], serde_json::json!(["C:\\Users"]));
        assert!(!back.contains_key("LastSessionSelectedTabIndex"));
        assert_eq!(back["ShowShelfPane"], true);
        assert!(std::fs::read_to_string(&path).unwrap().contains("\r\n  \"ShowShelfPane\""));
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Results saved by 0.5.0 (no cost fields) still load.
    #[test]
    fn older_results_still_load() {
        let old = r#"{"ours_ms":1.0,"windows_ms":2.0,"bytes":0,"files":0}"#;
        let v: Versus = serde_json::from_str(old).unwrap();
        assert_eq!(v.ours_cpu_ms, None);
    }
}

#[cfg(test)]
mod files_live {
    /// Opens the 1 000-file folder in Files (when installed) through the real test and checks that
    /// Files' session settings are the user's again afterwards. Opens windows: run by hand
    /// (`cargo test -p rf-app --lib files_live -- --ignored`).
    #[test]
    #[ignore]
    fn files_session_survives_a_real_test() {
        let Some(files) = rf_win::bench::detect().into_iter().find(|a| a.kind == rf_win::bench::AppKind::Files) else { return };
        let path = super::FilesSession::settings_path().unwrap();
        let before = super::FilesSession::read(&path).unwrap();
        let plan = super::Plan { apps: vec![files], open: vec![super::OpenTest::Files1k], copy: false, engines: false };
        let r = super::run(&plan, &|_| {}, &std::sync::atomic::AtomicBool::new(false)).unwrap();
        eprintln!("{:?}", r.open[0].apps[0].median().map(|m| (m.settled_ms, m.shared)));
        std::thread::sleep(std::time::Duration::from_secs(17));
        let after = super::FilesSession::read(&path).unwrap();
        for k in super::FILES_SESSION_KEYS {
            assert_eq!(before.get(k), after.get(k), "{k}");
        }
    }
}

#[cfg(test)]
mod live_100k {
    /// The 100 000-file folder: rustyfiles from scratch, rustyfiles already running (a copy with
    /// the test's own settings, kept running in the background: its first start is the warm-up
    /// run, then each start hands the folder to it, as README measures), Explorer and Files.
    /// Never touches the user's copy: skipped while any other rustyfiles runs. Opens windows: run
    /// by hand (`cargo test -p rf-app --lib live_100k -- --ignored --nocapture`), then end the
    /// test's copy (it stays in the background).
    #[test]
    #[ignore]
    fn open_100k() {
        use rf_win::bench::{App, AppKind};
        let fresh = super::rustyfiles_app();
        let others = std::process::Command::new("tasklist").args(["/FI", "IMAGENAME eq rustyfiles.exe", "/NH"]).output().map(|o| String::from_utf8_lossy(&o.stdout).contains("rustyfiles.exe")).unwrap_or(true);
        assert!(!others, "a rustyfiles is running: the warm runs would go to it");
        let cfg = super::bench_dir().join("rustyfiles-warm");
        std::fs::create_dir_all(&cfg).unwrap();
        std::fs::write(cfg.join("settings.toml"), "restore_session = false
run_in_background = true
auto_update = false
star_asked = true
starts = 1
search_index = false
name_index = false
directlook_enabled = false
").unwrap();
        let warm = App {
            kind: AppKind::Rustyfiles,
            exe: fresh.exe.clone(),
            env: vec![("RUSTYFILES_CONFIG_DIR".into(), cfg.to_string_lossy().into_owned()), ("RUSTYFILES_MUTE".into(), "1".into())],
        };
        let mut apps = vec![fresh, warm];
        apps.extend(rf_win::bench::detect().into_iter().filter(|a| matches!(a.kind, AppKind::Explorer | AppKind::Files)));
        let names = ["rustyfiles, from scratch", "rustyfiles, already running", "File Explorer", "Files"];
        let plan = super::Plan { apps, open: vec![super::OpenTest::Files100k], copy: false, engines: false };
        let r = super::run(&plan, &|_| {}, &std::sync::atomic::AtomicBool::new(false)).unwrap();
        for (name, a) in names.iter().zip(&r.open[0].apps) {
            let runs: Vec<String> = a.runs.iter().map(|x| format!("{:.0}{}{}", x.settled_ms, if x.shared { "*" } else { "" }, if x.timed_out { "!" } else { "" })).collect();
            eprintln!("{name}: median {:?} ms; window {:?} ms; runs [{}] {:?}", a.median().map(|m| m.settled_ms.round()), a.median().map(|m| m.window_ms.round()), runs.join(", "), a.error);
        }
    }
}

#[cfg(test)]
mod live_arke {
    /// rustyfiles against Arke (and File Explorer) on every folder of the speed test, the way README measures: rustyfiles started
    /// from scratch with the test's settings; Arke from scratch (no Arke process left before the start) and Arke already running
    /// (it keeps running in the background by default, so its next start hands the folder to it). Ending Arke between its cold runs
    /// is why this is a test run by hand and not part of Settings › Speed test, which never ends another app's process:
    /// `RF_BENCH_RUSTYFILES=<rustyfiles.exe> RF_BENCH_ARKE=<arke.exe> cargo test -p rf-app --lib live_arke -- --ignored --nocapture`.
    /// Opens windows on top for a few minutes; the test folders must exist (one run of the speed test makes them).
    #[test]
    #[ignore]
    fn against_arke() {
        use rf_win::bench::{App, AppKind, measure_open};
        use std::path::PathBuf;
        use std::time::Duration;
        let cancel = std::sync::atomic::AtomicBool::new(false);
        let mut ours = super::rustyfiles_app();
        if let Some(exe) = std::env::var_os("RF_BENCH_RUSTYFILES") {
            ours.exe = PathBuf::from(exe);
        }
        let arke_exe = std::env::var_os("RF_BENCH_ARKE")
            .map(PathBuf::from)
            .or_else(|| rf_win::bench::detect().into_iter().find(|a| a.kind == AppKind::Arke).map(|a| a.exe))
            .expect("no Arke: set RF_BENCH_ARKE");
        let arke = App { kind: AppKind::Arke, exe: arke_exe.clone(), env: Vec::new() };
        let explorer = rf_win::bench::detect().into_iter().find(|a| a.kind == AppKind::Explorer).unwrap();
        super::prepare_rustyfiles_settings().unwrap();
        // rustyfiles kept running in the background with settings of its own (its pipe is named after its settings folder, so a
        // start hands the folder to this copy, never to the user's), as `live_100k`
        let cfg = super::bench_dir().join("rustyfiles-warm");
        std::fs::create_dir_all(&cfg).unwrap();
        std::fs::write(cfg.join("settings.toml"), "restore_session = false
run_in_background = true
auto_update = false
star_asked = true
starts = 1
search_index = false
name_index = false
directlook_enabled = false
").unwrap();
        let warm = App {
            kind: AppKind::Rustyfiles,
            exe: ours.exe.clone(),
            env: vec![("RUSTYFILES_CONFIG_DIR".into(), cfg.to_string_lossy().into_owned()), ("RUSTYFILES_MUTE".into(), "1".into())],
        };
        // every process of one program (Arke's window and its shell host; the warm rustyfiles copy)
        let end_all = |exe: &std::path::Path| {
            let image = exe.to_string_lossy().replace('/', "\\").to_lowercase();
            let list = std::process::Command::new("powershell")
                .args(["-NoProfile", "-Command", "Get-CimInstance Win32_Process | ForEach-Object { \"$($_.ProcessId)|$($_.ExecutablePath)\" }"])
                .output()
                .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
                .unwrap_or_default();
            for line in list.lines() {
                if let Some((pid, path)) = line.trim().split_once('|')
                    && path.replace('/', "\\").to_lowercase() == image
                {
                    let _ = std::process::Command::new("taskkill").args(["/PID", pid, "/F"]).output();
                }
            }
            std::thread::sleep(Duration::from_millis(500));
        };
        let folders = [super::OpenTest::Start, super::OpenTest::Files1k, super::OpenTest::Files10k, super::OpenTest::Files100k, super::OpenTest::Images];
        let end_arke = || end_all(&arke_exe);
        let names = ["rustyfiles, from scratch", "rustyfiles, already running", "File Explorer", "Arke, from scratch", "Arke, already running"];
        for t in folders {
            let dir = super::bench_dir().join(t.folder());
            // (Settings › Speed test removes its folders when it is done: they are made again here)
            if !dir.is_dir() {
                super::make_open_folder(&dir, t, &|_| {}).unwrap();
            }
            let mut runs: Vec<Vec<rf_win::bench::OpenResult>> = vec![Vec::new(); names.len()];
            for round in 0..=super::RUNS {
                for (k, app) in [&ours, &warm, &explorer, &arke, &arke].into_iter().enumerate() {
                    if k == 3 {
                        end_arke();
                    }
                    match measure_open(app, &dir, super::OPEN_TIMEOUT, &cancel) {
                        Ok(r) if round > 0 => runs[k].push(r),
                        Ok(_) => {}
                        Err(e) => eprintln!("{}: {e:#}", names[k]),
                    }
                }
            }
            eprintln!("\n{}", t.folder());
            for (name, rs) in names.iter().zip(&runs) {
                let mut ms: Vec<f64> = rs.iter().map(|r| r.settled_ms).collect();
                ms.sort_by(f64::total_cmp);
                let mid = rs.iter().min_by(|a, b| (a.settled_ms - ms[ms.len() / 2]).abs().total_cmp(&(b.settled_ms - ms[ms.len() / 2]).abs()));
                let shown: Vec<String> = rs.iter().map(|r| format!("{:.0}{}{}", r.settled_ms, if r.shared { "*" } else { "" }, if r.timed_out { "!" } else { "" })).collect();
                eprintln!(
                    "  {name}: median {:?} ms, memory {:?} MB; runs [{}] in {:?}",
                    mid.map(|m| m.settled_ms.round()),
                    mid.map(|m| (m.private_ws as f64 / 1048576.0).round()),
                    shown.join(", "),
                    mid.map(|m| m.process.clone())
                );
            }
        }
        end_arke();
        end_all(&ours.exe);
    }
}

#[cfg(test)]
mod live_all {
    /// The README's comparison, by hand: every file manager found opening every folder of the speed test, each two ways where it
    /// can be: FROM SCRATCH (no process of it left before the start: every one is ended first) and ALREADY RUNNING (its process
    /// kept in the background, as rustyfiles, Arke, Files and Sigma do in everyday use, so a start hands the folder to it). Then
    /// the copy, ZIP and search engines against Windows'. Portable copies are named by `RF_BENCH_RUSTYFILES`, `RF_BENCH_ARKE`,
    /// `RF_BENCH_EXPLORERPP`, `RF_BENCH_DOUBLECMD`, `RF_BENCH_SIGMA`:
    /// `cargo test --release -p rf-app --lib live_all -- --ignored --nocapture`. Ends those apps' processes, so it is never part of
    /// Settings › Speed test. Opens windows on top for a while; writes `%TEMP%\rf-bench-everyone.json`.
    #[test]
    #[ignore]
    fn against_everyone() {
        use rf_win::bench::{App, AppKind, OpenResult, measure_open};
        use std::path::{Path, PathBuf};
        use std::time::Duration;
        let cancel = std::sync::atomic::AtomicBool::new(false);
        let mut ours = super::rustyfiles_app();
        if let Some(exe) = std::env::var_os("RF_BENCH_RUSTYFILES") {
            ours.exe = PathBuf::from(exe);
        }
        super::prepare_rustyfiles_settings().unwrap();
        // rustyfiles kept running in the background with settings of its own (its pipe is named after its settings folder, so a
        // start hands the folder to this copy, never to the user's), as `live_arke`
        let cfg = super::bench_dir().join("rustyfiles-warm");
        std::fs::create_dir_all(&cfg).unwrap();
        std::fs::write(
            cfg.join("settings.toml"),
            "restore_session = false\nrun_in_background = true\nauto_update = false\nstar_asked = true\nstarts = 1\nsearch_index = false\nname_index = false\ndirectlook_enabled = false\n",
        )
        .unwrap();
        let warm = App {
            kind: AppKind::Rustyfiles,
            exe: ours.exe.clone(),
            env: vec![("RUSTYFILES_CONFIG_DIR".into(), cfg.to_string_lossy().into_owned()), ("RUSTYFILES_MUTE".into(), "1".into())],
        };
        let mut found = rf_win::bench::detect();
        if let Some(exe) = std::env::var_os("RF_BENCH_ARKE").map(PathBuf::from)
            && !found.iter().any(|a| a.kind == AppKind::Arke)
        {
            found.push(App { kind: AppKind::Arke, exe, env: Vec::new() });
        }
        let get = |k: AppKind| found.iter().find(|a| a.kind == k).cloned();

        // every process of one program
        let end_all = |exe: &Path| {
            let image = exe.to_string_lossy().replace('/', "\\").to_lowercase();
            let list = std::process::Command::new("powershell")
                .args(["-NoProfile", "-Command", "Get-CimInstance Win32_Process | ForEach-Object { \"$($_.ProcessId)|$($_.ExecutablePath)\" }"])
                .output()
                .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
                .unwrap_or_default();
            for line in list.lines() {
                if let Some((pid, path)) = line.trim().split_once('|')
                    && path.replace('/', "\\").to_lowercase() == image
                {
                    let _ = std::process::Command::new("taskkill").args(["/PID", pid, "/F"]).output();
                }
            }
            std::thread::sleep(Duration::from_millis(700));
        };

        // (name, app, ended before each run, only the start)
        let mut entries: Vec<(&str, App, bool, bool)> = vec![("rustyfiles, from scratch", ours.clone(), false, false), ("rustyfiles, already running", warm.clone(), false, false)];
        if let Some(a) = get(AppKind::Explorer) {
            entries.push(("File Explorer", a, false, false));
        }
        if let Some(a) = get(AppKind::Files) {
            entries.push(("Files, already running", a, false, false));
        }
        if let Some(a) = get(AppKind::FilePilot) {
            entries.push(("File Pilot, from scratch", a, true, false));
        }
        for (k, cold, running) in [
            (AppKind::Arke, "Arke, from scratch", Some("Arke, already running")),
            (AppKind::ExplorerPlusPlus, "Explorer++, from scratch", None),
            (AppKind::DoubleCommander, "Double Commander, from scratch", None),
            (AppKind::Sigma, "Sigma, from scratch", Some("Sigma, already running")),
        ] {
            if let Some(a) = get(k) {
                let start_only = !k.opens_a_folder();
                entries.push((cold, a.clone(), true, start_only));
                if let Some(name) = running {
                    entries.push((name, a, false, start_only));
                }
            }
        }
        eprintln!("apps: {}", entries.iter().map(|e| e.0).collect::<Vec<_>>().join(", "));
        let _files = if entries.iter().any(|e| e.1.kind == AppKind::Files) { super::FilesSession::begin() } else { None };

        let mut out = serde_json::Map::new();
        for t in super::OpenTest::ALL {
            let dir = super::bench_dir().join(t.folder());
            // (Settings › Speed test removes its folders when it is done: they are made again here)
            if !dir.is_dir() {
                super::make_open_folder(&dir, t, &|_| {}).unwrap();
            }
            let mut runs: Vec<Vec<OpenResult>> = vec![Vec::new(); entries.len()];
            let mut errors: Vec<Option<String>> = vec![None; entries.len()];
            for round in 0..=super::RUNS {
                for (k, (name, app, cold, start_only)) in entries.iter().enumerate() {
                    if *start_only && t != super::OpenTest::Start {
                        continue;
                    }
                    if *cold {
                        end_all(&app.exe);
                    }
                    match measure_open(app, &dir, super::OPEN_TIMEOUT, &cancel) {
                        Ok(r) if round > 0 => runs[k].push(r),
                        Ok(_) => {}
                        Err(e) => {
                            eprintln!("{name}: {e:#}");
                            errors[k] = Some(format!("{e:#}"));
                        }
                    }
                }
            }
            eprintln!("\n== {}", t.folder());
            let mut row = serde_json::Map::new();
            for (k, (name, ..)) in entries.iter().enumerate() {
                let mut ms: Vec<f64> = runs[k].iter().map(|r| r.settled_ms).collect();
                ms.sort_by(f64::total_cmp);
                let Some(&mid) = ms.get(ms.len() / 2) else {
                    if let Some(e) = &errors[k] {
                        eprintln!("  {name:32} -  ({e})");
                    }
                    continue;
                };
                let m = runs[k].iter().min_by(|a, b| (a.settled_ms - mid).abs().total_cmp(&(b.settled_ms - mid).abs())).unwrap();
                eprintln!(
                    "  {name:32} {:>7.0} ms  window {:>6.0} ms  cpu {:>6.0} ms  memory {:>4} MB  runs {:?}{}",
                    m.settled_ms,
                    m.window_ms,
                    m.cpu_ms,
                    m.private_ws >> 20,
                    ms.iter().map(|v| v.round()).collect::<Vec<_>>(),
                    if runs[k].iter().any(|r| r.timed_out) { "  (timed out)" } else { "" }
                );
                row.insert(name.to_string(), serde_json::json!({ "ms": m.settled_ms, "window_ms": m.window_ms, "cpu_ms": m.cpu_ms, "memory_mb": m.private_ws >> 20, "runs": ms, "shared": m.shared }));
            }
            out.insert(t.folder().to_string(), serde_json::Value::Object(row));
        }
        // and any window still showing a test folder is asked to close (Files kept one open after a run)
        let left = rf_win::bench::close_test_windows();
        if left > 0 {
            eprintln!("closed {left} test window(s) left open");
        }
        // the running copies the test made are ended: Arke's, Sigma's and the warm rustyfiles
        for (_, app, ..) in &entries {
            if app.kind != AppKind::Explorer && app.kind != AppKind::Files {
                end_all(&app.exe);
            }
        }

        // the engines, as Settings › Speed test measures them
        // (`run` saves its results as the speed test's last ones: those of Settings › Speed test are kept and put back)
        let last = super::bench_dir().join("results.json");
        let kept = std::fs::read(&last).ok();
        let plan = super::Plan { apps: vec![ours], open: Vec::new(), copy: true, engines: true };
        let r = super::run(&plan, &|_| {}, &cancel).unwrap();
        match kept {
            Some(b) => {
                let _ = std::fs::write(&last, b);
            }
            None => {
                let _ = std::fs::remove_file(&last);
            }
        }
        eprintln!("\nengines: {}", serde_json::to_string(&r.engines).unwrap_or_default());
        out.insert("engines".into(), serde_json::to_value(&r.engines).unwrap_or_default());
        out.insert("machine".into(), serde_json::to_value(&r.machine).unwrap_or_default());
        let path = std::env::temp_dir().join("rf-bench-everyone.json");
        std::fs::write(&path, serde_json::to_vec_pretty(&serde_json::Value::Object(out)).unwrap()).unwrap();
        eprintln!("written: {}", path.display());
    }
}