| 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 |
use std::sync::Arc; |
| 16 |
use std::time::Instant; |
| 17 |
|
| 18 |
use criterion::BatchSize; |
| 19 |
use criterion::BenchmarkGroup; |
| 20 |
use criterion::BenchmarkId; |
| 21 |
use criterion::measurement::Measurement; |
| 22 |
use futures::StreamExt as _; |
| 23 |
use jj_lib::revset::SymbolResolver; |
| 24 |
use jj_lib::revset::SymbolResolverExtension; |
| 25 |
use jj_lib::revset::UserRevsetExpression; |
| 26 |
use pollster::FutureExt as _; |
| 27 |
|
| 28 |
use super::CriterionArgs; |
| 29 |
use super::new_criterion; |
| 30 |
use crate::cli_util::CommandHelper; |
| 31 |
use crate::cli_util::RevisionArg; |
| 32 |
use crate::cli_util::WorkspaceCommandHelper; |
| 33 |
use crate::command_error::CommandError; |
| 34 |
use crate::ui::Ui; |
| 35 |
|
| 36 |
/// Walk the revisions in the revset |
| 37 |
#[derive(clap::Args, Clone, Debug)] |
| 38 |
#[command(group(clap::ArgGroup::new("revset_source").required(true)))] |
| 39 |
pub struct BenchRevsetArgs { |
| 40 |
#[arg(group = "revset_source")] |
| 41 |
revisions: Vec<RevisionArg>, |
| 42 |
|
| 43 |
/// Read revsets from file |
| 44 |
#[arg(long, short = 'f', group = "revset_source", value_hint = clap::ValueHint::FilePath)] |
| 45 |
file: Option<String>, |
| 46 |
|
| 47 |
#[command(flatten)] |
| 48 |
criterion: CriterionArgs, |
| 49 |
} |
| 50 |
|
| 51 |
pub async fn cmd_bench_revset( |
| 52 |
ui: &mut Ui, |
| 53 |
command: &CommandHelper, |
| 54 |
args: &BenchRevsetArgs, |
| 55 |
) -> Result<(), CommandError> { |
| 56 |
let workspace_command = command.workspace_helper(ui).await?; |
| 57 |
let revsets = if let Some(file_path) = &args.file { |
| 58 |
std::fs::read_to_string(command.cwd().join(file_path))? |
| 59 |
.lines() |
| 60 |
.map(|line| line.trim().to_owned()) |
| 61 |
.filter(|line| !line.is_empty() && !line.starts_with('#')) |
| 62 |
.map(RevisionArg::from) |
| 63 |
.collect() |
| 64 |
} else { |
| 65 |
args.revisions.clone() |
| 66 |
}; |
| 67 |
let mut criterion = new_criterion(ui, &args.criterion); |
| 68 |
let mut group = criterion.benchmark_group("revsets"); |
| 69 |
for revset in &revsets { |
| 70 |
bench_revset(ui, command, &workspace_command, &mut group, revset)?; |
| 71 |
} |
| 72 |
// Neither of these seem to report anything... |
| 73 |
group.finish(); |
| 74 |
criterion.final_summary(); |
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
|
| 78 |
fn bench_revset<M: Measurement>( |
| 79 |
ui: &mut Ui, |
| 80 |
command: &CommandHelper, |
| 81 |
workspace_command: &WorkspaceCommandHelper, |
| 82 |
group: &mut BenchmarkGroup<M>, |
| 83 |
revset: &RevisionArg, |
| 84 |
) -> Result<(), CommandError> { |
| 85 |
writeln!(ui.status(), "----------Testing revset: {revset}----------")?; |
| 86 |
let expression = workspace_command |
| 87 |
.parse_revset(ui, revset)? |
| 88 |
.expression() |
| 89 |
.clone(); |
| 90 |
// Time both evaluation and iteration. |
| 91 |
let routine = |workspace_command: &WorkspaceCommandHelper, |
| 92 |
expression: Arc<UserRevsetExpression>| { |
| 93 |
// Evaluate the expression without parsing/evaluating short-prefixes. |
| 94 |
let repo = workspace_command.repo().as_ref(); |
| 95 |
let symbol_resolver = |
| 96 |
SymbolResolver::new(repo, &([] as [Box<dyn SymbolResolverExtension>; 0])); |
| 97 |
let resolved = expression |
| 98 |
.resolve_user_expression(repo, &symbol_resolver) |
| 99 |
.unwrap(); |
| 100 |
let revset = resolved.evaluate(repo).unwrap(); |
| 101 |
revset.stream().count().block_on() |
| 102 |
}; |
| 103 |
let before = Instant::now(); |
| 104 |
let result = routine(workspace_command, expression.clone()); |
| 105 |
let after = Instant::now(); |
| 106 |
writeln!( |
| 107 |
ui.status(), |
| 108 |
"First run took {:?} and produced {result} commits.", |
| 109 |
after.duration_since(before), |
| 110 |
)?; |
| 111 |
|
| 112 |
group.bench_with_input( |
| 113 |
BenchmarkId::from_parameter(revset), |
| 114 |
&expression, |
| 115 |
|bencher, expression| { |
| 116 |
bencher.iter_batched( |
| 117 |
// Reload repo and backend store to clear caches (such as commit objects |
| 118 |
// in `Store`), but preload index since it's more likely to be loaded |
| 119 |
// by preceding operation. `repo.reload_at()` isn't enough to clear |
| 120 |
// store cache. |
| 121 |
|| { |
| 122 |
let workspace_command = |
| 123 |
command.workspace_helper_no_snapshot(ui).block_on().unwrap(); |
| 124 |
workspace_command.repo().readonly_index(); |
| 125 |
workspace_command |
| 126 |
}, |
| 127 |
|workspace_command| routine(&workspace_command, expression.clone()), |
| 128 |
// Index-preloaded repo may consume a fair amount of memory |
| 129 |
BatchSize::LargeInput, |
| 130 |
); |
| 131 |
}, |
| 132 |
); |
| 133 |
Ok(()) |
| 134 |
} |