Debian Codegraph @

cli/src/commands/bench/mod.rs

raw
1 // Copyright 2023 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 mod common_ancestors;
16 mod is_ancestor;
17 mod resolve_prefix;
18 mod revset;
19
20 use std::fmt::Debug;
21 use std::io;
22 use std::time::Instant;
23
24 use clap::Subcommand;
25 use criterion::Criterion;
26
27 use self::common_ancestors::BenchCommonAncestorsArgs;
28 use self::common_ancestors::cmd_bench_common_ancestors;
29 use self::is_ancestor::BenchIsAncestorArgs;
30 use self::is_ancestor::cmd_bench_is_ancestor;
31 use self::resolve_prefix::BenchResolvePrefixArgs;
32 use self::resolve_prefix::cmd_bench_resolve_prefix;
33 use self::revset::BenchRevsetArgs;
34 use self::revset::cmd_bench_revset;
35 use crate::cli_util::CommandHelper;
36 use crate::command_error::CommandError;
37 use crate::ui::Ui;
38
39 /// Commands for benchmarking internal operations
40 #[derive(Subcommand, Clone, Debug)]
41 #[command(hide = true)]
42 pub enum BenchCommand {
43 CommonAncestors(BenchCommonAncestorsArgs),
44 IsAncestor(BenchIsAncestorArgs),
45 ResolvePrefix(BenchResolvePrefixArgs),
46 Revset(BenchRevsetArgs),
47 }
48
49 pub(crate) async fn cmd_bench(
50 ui: &mut Ui,
51 command: &CommandHelper,
52 subcommand: &BenchCommand,
53 ) -> Result<(), CommandError> {
54 match subcommand {
55 BenchCommand::CommonAncestors(args) => cmd_bench_common_ancestors(ui, command, args).await,
56 BenchCommand::IsAncestor(args) => cmd_bench_is_ancestor(ui, command, args).await,
57 BenchCommand::ResolvePrefix(args) => cmd_bench_resolve_prefix(ui, command, args).await,
58 BenchCommand::Revset(args) => cmd_bench_revset(ui, command, args).await,
59 }
60 }
61
62 #[derive(clap::Args, Clone, Debug)]
63 struct CriterionArgs {
64 /// Name of baseline to save results
65 #[arg(long, short = 's', group = "baseline_mode", default_value = "base")]
66 save_baseline: String,
67
68 /// Name of baseline to compare with
69 #[arg(long, short = 'b', group = "baseline_mode")]
70 baseline: Option<String>,
71
72 /// Sample size for the benchmarks, which must be at least 10
73 #[arg(long, default_value_t = 100, value_parser = clap::value_parser!(u32).range(10..))]
74 sample_size: u32, // not usize because https://github.com/clap-rs/clap/issues/4253
75 }
76
77 fn new_criterion(ui: &Ui, args: &CriterionArgs) -> Criterion {
78 let criterion = Criterion::default().with_output_color(ui.color());
79 let criterion = if let Some(name) = &args.baseline {
80 let strict = false; // Do not panic if previous baseline doesn't exist.
81 criterion.retain_baseline(name.clone(), strict)
82 } else {
83 criterion.save_baseline(args.save_baseline.clone())
84 };
85 criterion.sample_size(args.sample_size as usize)
86 }
87
88 fn run_bench<R, O>(ui: &mut Ui, id: &str, args: &CriterionArgs, mut routine: R) -> io::Result<()>
89 where
90 R: (FnMut() -> O) + Copy,
91 O: Debug,
92 {
93 let mut criterion = new_criterion(ui, args);
94 let before = Instant::now();
95 let result = routine();
96 let after = Instant::now();
97 writeln!(
98 ui.status(),
99 "First run took {:?} and produced: {:?}",
100 after.duration_since(before),
101 result
102 )?;
103 criterion.bench_function(id, |bencher: &mut criterion::Bencher| {
104 bencher.iter(routine);
105 });
106 Ok(())
107 }