1//! Perf profiler for Zed tests. Outputs timings of tests marked with the `#[perf]`
  2//! attribute to stdout in Markdown. See the documentation of `util_macros::perf`
  3//! for usage details on the actual attribute.
  4//!
  5//! # Setup
  6//! Make sure `hyperfine` is installed and in the shell path.
  7//!
  8//! # Usage
  9//! Calling this tool rebuilds the targeted crate(s) with some cfg flags set for the
 10//! perf proc macro *and* enables optimisations (`release-fast` profile), so expect
 11//! it to take a little while.
 12//!
 13//! To test an individual crate, run:
 14//! ```sh
 15//! cargo perf-test -p $CRATE
 16//! ```
 17//!
 18//! To test everything (which will be **VERY SLOW**), run:
 19//! ```sh
 20//! cargo perf-test --workspace
 21//! ```
 22//!
 23//! Some command-line parameters are also recognised by this profiler. To filter
 24//! out all tests below a certain importance (e.g. `important`), run:
 25//! ```sh
 26//! cargo perf-test $WHATEVER -- --important
 27//! ```
 28//!
 29//! Similarly, to skip outputting progress to the command line, pass `-- --quiet`.
 30//! These flags can be combined.
 31//!
 32//! ## Comparing runs
 33//! Passing `--json=ident` will save per-crate run files in `.perf-runs`, e.g.
 34//! `cargo perf-test -p gpui -- --json=blah` will result in `.perf-runs/blah.gpui.json`
 35//! being created (unless no tests were run). These results can be automatically
 36//! compared. To do so, run `cargo perf-compare new-ident old-ident`.
 37//!
 38//! To save the markdown output to a file instead, run `cargo perf-compare --save=$FILE
 39//! new-ident old-ident`.
 40//!
 41//! NB: All files matching `.perf-runs/ident.*.json` will be considered when
 42//! doing this comparison, so ensure there aren't leftover files in your `.perf-runs`
 43//! directory that might match that!
 44//!
 45//! # Notes
 46//! This should probably not be called manually unless you're working on the profiler
 47//! itself; use the `cargo perf-test` alias (after building this crate) instead.
 48
 49use zed_perf::{FailKind, Importance, Output, TestMdata, Timings, consts};
 50
 51use std::{
 52    fs::OpenOptions,
 53    io::{Read, Write},
 54    num::NonZero,
 55    path::{Path, PathBuf},
 56    process::{Command, Stdio},
 57    sync::atomic::{AtomicBool, Ordering},
 58    time::{Duration, Instant},
 59};
 60
 61/// How many iterations to attempt the first time a test is run.
 62const DEFAULT_ITER_COUNT: NonZero<usize> = NonZero::new(3).unwrap();
 63/// Multiplier for the iteration count when a test doesn't pass the noise cutoff.
 64const ITER_COUNT_MUL: NonZero<usize> = NonZero::new(4).unwrap();
 65
 66/// Do we keep stderr empty while running the tests?
 67static QUIET: AtomicBool = AtomicBool::new(false);
 68
 69/// Report a failure into the output and skip an iteration.
 70macro_rules! fail {
 71    ($output:ident, $name:expr, $kind:expr) => {{
 72        $output.failure($name, None, None, $kind);
 73        continue;
 74    }};
 75    ($output:ident, $name:expr, $mdata:expr, $kind:expr) => {{
 76        $output.failure($name, Some($mdata), None, $kind);
 77        continue;
 78    }};
 79    ($output:ident, $name:expr, $mdata:expr, $count:expr, $kind:expr) => {{
 80        $output.failure($name, Some($mdata), Some($count), $kind);
 81        continue;
 82    }};
 83}
 84
 85/// How does this perf run return its output?
 86enum OutputKind<'a> {
 87    /// Print markdown to the terminal.
 88    Markdown,
 89    /// Save JSON to a file.
 90    Json(&'a Path),
 91}
 92
 93impl OutputKind<'_> {
 94    /// Logs the output of a run as per the `OutputKind`.
 95    fn log(&self, output: &Output, t_bin: &str) {
 96        match self {
 97            OutputKind::Markdown => println!("{output}"),
 98            OutputKind::Json(ident) => {
 99                // We're going to be in tooling/perf/$whatever.
100                let wspace_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
101                    .join("..")
102                    .join("..");
103                let runs_dir = PathBuf::from(&wspace_dir).join(consts::RUNS_DIR);
104                std::fs::create_dir_all(&runs_dir).unwrap();
105                assert!(
106                    !ident.to_string_lossy().is_empty(),
107                    "FATAL: Empty filename specified!"
108                );
109                // Get the test binary's crate's name; a path like
110                // target/release-fast/deps/gpui-061ff76c9b7af5d7
111                // would be reduced to just "gpui".
112                let test_bin_stripped = Path::new(t_bin)
113                    .file_name()
114                    .unwrap()
115                    .to_str()
116                    .unwrap()
117                    .rsplit_once('-')
118                    .unwrap()
119                    .0;
120                let mut file_path = runs_dir.join(ident);
121                file_path
122                    .as_mut_os_string()
123                    .push(format!(".{test_bin_stripped}.json"));
124                let mut out_file = OpenOptions::new()
125                    .write(true)
126                    .create(true)
127                    .truncate(true)
128                    .open(&file_path)
129                    .unwrap();
130                out_file
131                    .write_all(&serde_json::to_vec(&output).unwrap())
132                    .unwrap();
133                if !QUIET.load(Ordering::Relaxed) {
134                    eprintln!("JSON output written to {}", file_path.display());
135                }
136            }
137        }
138    }
139}
140
141/// Runs a given metadata-returning function from a test handler, parsing its
142/// output into a `TestMdata`.
143fn parse_mdata(t_bin: &str, mdata_fn: &str) -> Result<TestMdata, FailKind> {
144    let mut cmd = Command::new(t_bin);
145    cmd.args([mdata_fn, "--exact", "--nocapture"]);
146    let out = cmd
147        .output()
148        .expect("FATAL: Could not run test binary {t_bin}");
149    assert!(out.status.success());
150    let stdout = String::from_utf8_lossy(&out.stdout);
151    let mut version = None;
152    let mut iterations = None;
153    let mut importance = Importance::default();
154    let mut weight = consts::WEIGHT_DEFAULT;
155    for line in stdout
156        .lines()
157        .filter_map(|l| l.strip_prefix(consts::MDATA_LINE_PREF))
158    {
159        let mut items = line.split_whitespace();
160        // For v0, we know the ident always comes first, then one field.
161        match items.next().ok_or(FailKind::BadMetadata)? {
162            consts::VERSION_LINE_NAME => {
163                let v = items
164                    .next()
165                    .ok_or(FailKind::BadMetadata)?
166                    .parse::<u32>()
167                    .map_err(|_| FailKind::BadMetadata)?;
168                if v > consts::MDATA_VER {
169                    return Err(FailKind::VersionMismatch);
170                }
171                version = Some(v);
172            }
173            consts::ITER_COUNT_LINE_NAME => {
174                // This should never be zero!
175                iterations = Some(
176                    items
177                        .next()
178                        .ok_or(FailKind::BadMetadata)?
179                        .parse::<usize>()
180                        .map_err(|_| FailKind::BadMetadata)?
181                        .try_into()
182                        .map_err(|_| FailKind::BadMetadata)?,
183                );
184            }
185            consts::IMPORTANCE_LINE_NAME => {
186                importance = match items.next().ok_or(FailKind::BadMetadata)? {
187                    "critical" => Importance::Critical,
188                    "important" => Importance::Important,
189                    "average" => Importance::Average,
190                    "iffy" => Importance::Iffy,
191                    "fluff" => Importance::Fluff,
192                    _ => return Err(FailKind::BadMetadata),
193                };
194            }
195            consts::WEIGHT_LINE_NAME => {
196                weight = items
197                    .next()
198                    .ok_or(FailKind::BadMetadata)?
199                    .parse::<u8>()
200                    .map_err(|_| FailKind::BadMetadata)?;
201            }
202            _ => unreachable!(),
203        }
204    }
205
206    Ok(TestMdata {
207        version: version.ok_or(FailKind::BadMetadata)?,
208        // Iterations may be determined by us and thus left unspecified.
209        iterations,
210        // In principle this should always be set, but just for the sake of
211        // stability allow the potentially-breaking change of not reporting the
212        // importance without erroring. Maybe we want to change this.
213        importance,
214        // Same with weight.
215        weight,
216    })
217}
218
219/// Compares the perf results of two profiles as per the arguments passed in.
220fn compare_profiles(args: &[String]) {
221    let mut save_to = None;
222    let mut ident_idx = 0;
223    args.first().inspect(|a| {
224        if a.starts_with("--save") {
225            save_to = Some(
226                a.strip_prefix("--save=")
227                    .expect("FATAL: save param formatted incorrectly"),
228            );
229        }
230        ident_idx = 1;
231    });
232    let ident_new = args
233        .get(ident_idx)
234        .expect("FATAL: missing identifier for new run");
235    let ident_old = args
236        .get(ident_idx + 1)
237        .expect("FATAL: missing identifier for old run");
238    let wspace_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
239    let runs_dir = PathBuf::from(&wspace_dir)
240        .join("..")
241        .join("..")
242        .join(consts::RUNS_DIR);
243
244    // Use the blank outputs initially, so we can merge into these with prefixes.
245    let mut outputs_new = Output::blank();
246    let mut outputs_old = Output::blank();
247
248    for e in runs_dir.read_dir().unwrap() {
249        let Ok(entry) = e else {
250            continue;
251        };
252        let Ok(metadata) = entry.metadata() else {
253            continue;
254        };
255        if metadata.is_file() {
256            let Ok(name) = entry.file_name().into_string() else {
257                continue;
258            };
259
260            // A little helper to avoid code duplication. Reads the `output` from
261            // a json file, then merges it into what we have so far.
262            let read_into = |output: &mut Output| {
263                let mut elems = name.split('.').skip(1);
264                let prefix = elems.next().unwrap();
265                assert_eq!("json", elems.next().unwrap());
266                assert!(elems.next().is_none());
267                let mut buffer = Vec::new();
268                let _ = OpenOptions::new()
269                    .read(true)
270                    .open(entry.path())
271                    .unwrap()
272                    .read_to_end(&mut buffer)
273                    .unwrap();
274                let o_other: Output = serde_json::from_slice(&buffer).unwrap();
275                output.merge(o_other, prefix);
276            };
277
278            if name.starts_with(ident_old) {
279                read_into(&mut outputs_old);
280            } else if name.starts_with(ident_new) {
281                read_into(&mut outputs_new);
282            }
283        }
284    }
285
286    let res = outputs_new.compare_perf(outputs_old);
287    if let Some(filename) = save_to {
288        let mut file = std::fs::OpenOptions::new()
289            .create(true)
290            .write(true)
291            .truncate(true)
292            .open(filename)
293            .expect("FATAL: couldn't save run results to file");
294        file.write_all(format!("{res}").as_bytes()).unwrap();
295    } else {
296        println!("{res}");
297    }
298}
299
300/// Runs a test binary, filtering out tests which aren't marked for perf triage
301/// and giving back the list of tests we care about.
302///
303/// The output of this is an iterator over `test_fn_name, test_mdata_name`.
304fn get_tests(t_bin: &str) -> impl ExactSizeIterator<Item = (String, String)> {
305    let mut cmd = Command::new(t_bin);
306    // --format=json is nightly-only :(
307    cmd.args(["--list", "--format=terse"]);
308    let out = cmd
309        .output()
310        .expect("FATAL: Could not run test binary {t_bin}");
311    assert!(
312        out.status.success(),
313        "FATAL: Cannot do perf check - test binary {t_bin} returned an error"
314    );
315    if !QUIET.load(Ordering::Relaxed) {
316        eprintln!("Test binary ran successfully; starting profile...");
317    }
318    // Parse the test harness output to look for tests we care about.
319    let stdout = String::from_utf8_lossy(&out.stdout);
320    let mut test_list: Vec<_> = stdout
321        .lines()
322        .filter_map(|line| {
323            // This should split only in two; e.g.,
324            // "app::test::test_arena: test" => "app::test::test_arena:", "test"
325            let line: Vec<_> = line.split_whitespace().collect();
326            match line[..] {
327                // Final byte of t_name is ":", which we need to ignore.
328                [t_name, kind] => (kind == "test").then(|| &t_name[..t_name.len() - 1]),
329                _ => None,
330            }
331        })
332        // Exclude tests that aren't marked for perf triage based on suffix.
333        .filter(|t_name| {
334            t_name.ends_with(consts::SUF_NORMAL) || t_name.ends_with(consts::SUF_MDATA)
335        })
336        .collect();
337
338    // Pulling itertools just for .dedup() would be quite a big dependency that's
339    // not used elsewhere, so do this on a vec instead.
340    test_list.sort_unstable();
341    test_list.dedup();
342
343    // Tests should come in pairs with their mdata fn!
344    assert!(
345        test_list.len().is_multiple_of(2),
346        "Malformed tests in test binary {t_bin}"
347    );
348
349    let out = test_list
350        .chunks_exact_mut(2)
351        .map(|pair| {
352            // Be resilient against changes to these constants.
353            if consts::SUF_NORMAL < consts::SUF_MDATA {
354                (pair[0].to_owned(), pair[1].to_owned())
355            } else {
356                (pair[1].to_owned(), pair[0].to_owned())
357            }
358        })
359        .collect::<Vec<_>>();
360    out.into_iter()
361}
362
363/// Runs the specified test `count` times, returning the time taken if the test
364/// succeeded.
365#[inline]
366fn spawn_and_iterate(t_bin: &str, t_name: &str, count: NonZero<usize>) -> Option<Duration> {
367    let mut cmd = Command::new(t_bin);
368    cmd.args([t_name, "--exact"]);
369    cmd.env(consts::ITER_ENV_VAR, format!("{count}"));
370    // Don't let the child muck up our stdin/out/err.
371    cmd.stdin(Stdio::null());
372    cmd.stdout(Stdio::null());
373    cmd.stderr(Stdio::null());
374    let pre = Instant::now();
375    // Discard the output beyond ensuring success.
376    let out = cmd.spawn().unwrap().wait();
377    let post = Instant::now();
378    out.iter().find_map(|s| s.success().then_some(post - pre))
379}
380
381/// Triage a test to determine the correct number of iterations that it should run.
382/// Specifically, repeatedly runs the given test until its execution time exceeds
383/// `thresh`, calling `step(iterations)` after every failed run to determine the new
384/// iteration count. Returns `None` if the test errored or `step` returned `None`,
385/// else `Some(iterations)`.
386///
387/// # Panics
388/// This will panic if `step(usize)` is not monotonically increasing, or if the test
389/// binary is invalid.
390fn triage_test(
391    t_bin: &str,
392    t_name: &str,
393    thresh: Duration,
394    mut step: impl FnMut(NonZero<usize>) -> Option<NonZero<usize>>,
395) -> Option<NonZero<usize>> {
396    let mut iter_count = DEFAULT_ITER_COUNT;
397    // It's possible that the first loop of a test might be an outlier (e.g. it's
398    // doing some caching), in which case we want to skip it.
399    let duration_once = spawn_and_iterate(t_bin, t_name, NonZero::new(1).unwrap())?;
400    loop {
401        let duration = spawn_and_iterate(t_bin, t_name, iter_count)?;
402        if duration.saturating_sub(duration_once) > thresh {
403            break Some(iter_count);
404        }
405        let new = step(iter_count)?;
406        assert!(
407            new > iter_count,
408            "FATAL: step must be monotonically increasing"
409        );
410        iter_count = new;
411    }
412}
413
414/// Profiles a given test with hyperfine, returning the mean and standard deviation
415/// for its runtime. If the test errors, returns `None` instead.
416fn hyp_profile(t_bin: &str, t_name: &str, iterations: NonZero<usize>) -> Option<Timings> {
417    let mut perf_cmd = Command::new("hyperfine");
418    // Warm up the cache and print markdown output to stdout, which we parse.
419    perf_cmd.args([
420        "--style",
421        "none",
422        "--warmup",
423        "1",
424        "--export-markdown",
425        "-",
426        // Parse json instead...
427        "--time-unit",
428        "millisecond",
429        &format!("{t_bin} --exact {t_name}"),
430    ]);
431    perf_cmd.env(consts::ITER_ENV_VAR, format!("{iterations}"));
432    let p_out = perf_cmd.output().unwrap();
433    if !p_out.status.success() {
434        return None;
435    }
436
437    let cmd_output = String::from_utf8_lossy(&p_out.stdout);
438    // Can't use .last() since we have a trailing newline. Sigh.
439    let results_line = cmd_output.lines().nth(3).unwrap();
440    // Grab the values out of the pretty-print.
441    // TODO: Parse json instead.
442    let mut res_iter = results_line.split_whitespace();
443    // Durations are given in milliseconds, so account for that.
444    let mean = Duration::from_secs_f64(res_iter.nth(5).unwrap().parse::<f64>().unwrap() / 1000.);
445    let stddev = Duration::from_secs_f64(res_iter.nth(1).unwrap().parse::<f64>().unwrap() / 1000.);
446
447    Some(Timings { mean, stddev })
448}
449
450fn main() {
451    let args = std::env::args().collect::<Vec<_>>();
452    // We get passed the test we need to run as the 1st argument after our own name.
453    let t_bin = args
454        .get(1)
455        .expect("FATAL: No test binary or command; this shouldn't be manually invoked!");
456
457    // We're being asked to compare two results, not run the profiler.
458    if t_bin == "compare" {
459        compare_profiles(&args[2..]);
460        return;
461    }
462
463    // Minimum test importance we care about this run.
464    let mut thresh = Importance::Iffy;
465    // Where to print the output of this run.
466    let mut out_kind = OutputKind::Markdown;
467
468    for arg in args.iter().skip(2) {
469        match arg.as_str() {
470            "--critical" => thresh = Importance::Critical,
471            "--important" => thresh = Importance::Important,
472            "--average" => thresh = Importance::Average,
473            "--iffy" => thresh = Importance::Iffy,
474            "--fluff" => thresh = Importance::Fluff,
475            "--quiet" => QUIET.store(true, Ordering::Relaxed),
476            s if s.starts_with("--json") => {
477                out_kind = OutputKind::Json(Path::new(
478                    s.strip_prefix("--json=")
479                        .expect("FATAL: Invalid json parameter; pass --json=ident"),
480                ));
481            }
482            _ => (),
483        }
484    }
485    if !QUIET.load(Ordering::Relaxed) {
486        eprintln!("Starting perf check");
487    }
488
489    let mut output = Output::default();
490
491    // Spawn and profile an instance of each perf-sensitive test, via hyperfine.
492    // Each test is a pair of (test, metadata-returning-fn), so grab both. We also
493    // know the list is sorted.
494    let i = get_tests(t_bin);
495    let len = i.len();
496    for (idx, (ref t_name, ref t_mdata)) in i.enumerate() {
497        if !QUIET.load(Ordering::Relaxed) {
498            eprint!("\rProfiling test {}/{}", idx + 1, len);
499        }
500        // Pretty-printable stripped name for the test.
501        let t_name_pretty = t_name.replace(consts::SUF_NORMAL, "");
502
503        // Get the metadata this test reports for us.
504        let t_mdata = match parse_mdata(t_bin, t_mdata) {
505            Ok(mdata) => mdata,
506            Err(err) => fail!(output, t_name_pretty, err),
507        };
508
509        if t_mdata.importance < thresh {
510            fail!(output, t_name_pretty, t_mdata, FailKind::Skipped);
511        }
512
513        // Time test execution to see how many iterations we need to do in order
514        // to account for random noise. This is skipped for tests with fixed
515        // iteration counts.
516        let final_iter_count = t_mdata.iterations.or_else(|| {
517            triage_test(t_bin, t_name, consts::NOISE_CUTOFF, |c| {
518                if let Some(c) = c.checked_mul(ITER_COUNT_MUL) {
519                    Some(c)
520                } else {
521                    // This should almost never happen, but maybe..?
522                    eprintln!(
523                        "WARNING: Ran nearly usize::MAX iterations of test {t_name_pretty}; skipping"
524                    );
525                    None
526                }
527            })
528        });
529
530        // Don't profile failing tests.
531        let Some(final_iter_count) = final_iter_count else {
532            fail!(output, t_name_pretty, t_mdata, FailKind::Triage);
533        };
534
535        // Now profile!
536        if let Some(timings) = hyp_profile(t_bin, t_name, final_iter_count) {
537            output.success(t_name_pretty, t_mdata, final_iter_count, timings);
538        } else {
539            fail!(
540                output,
541                t_name_pretty,
542                t_mdata,
543                final_iter_count,
544                FailKind::Profile
545            );
546        }
547    }
548    if !QUIET.load(Ordering::Relaxed) {
549        if output.is_empty() {
550            eprintln!("Nothing to do.");
551        } else {
552            // If stdout and stderr are on the same terminal, move us after the
553            // output from above.
554            eprintln!();
555        }
556    }
557
558    // No need making an empty json file on every empty test bin.
559    if output.is_empty() {
560        return;
561    }
562
563    out_kind.log(&output, t_bin);
564}