Skip to content

Commit 44b6a46

Browse files
committed
feat(run): add mode command arg
1 parent a35d4f7 commit 44b6a46

5 files changed

Lines changed: 47 additions & 36 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ Use the `CODSPEED_LOG` environment variable to set the logging level:
7878
```bash
7979
CODSPEED_LOG=debug codspeed run ...
8080
```
81+
82+
### Changing the mode of the runner
83+
84+
By default, the runner will run the benchmark in the `instrumentation` mode. You can specify the mode with the `--mode` flag of the `run` command:
85+
86+
```bash
87+
# Run in the `instrumentation` mode
88+
codspeed run --mode instrumentation <my-benchmark-command>
89+
90+
# Run in the `walltime` mode
91+
codspeed run --mode walltime <my-benchmark-command>
92+
```
93+
94+
> [!WARNING]
95+
> We strongly recommend not changing this mode unless you know what you are doing.
96+
> Using the `walltime` mode on machines not provided and managed by CodSpeed will lead to inconsistent results, as the machine will not have the necessary configurations and resources for accurate measurements.
97+
> Check out the [Walltime Instrument Documentation](https://docs.codspeed.io/instruments/walltime/) for more details.

src/run/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ use url::Url;
44

55
use crate::run::RunArgs;
66

7+
use super::RunnerMode;
8+
79
#[derive(Debug)]
810
pub struct Config {
911
pub upload_url: Url,
1012
pub token: Option<String>,
1113
pub working_directory: Option<String>,
1214
pub command: String,
1315

16+
pub mode: RunnerMode,
1417
pub instruments: Instruments,
1518

1619
pub skip_upload: bool,
@@ -32,6 +35,7 @@ impl Config {
3235
token: None,
3336
working_directory: None,
3437
command: "".into(),
38+
mode: RunnerMode::Instrumentation,
3539
instruments: Instruments::test(),
3640
skip_upload: false,
3741
skip_setup: false,
@@ -52,6 +56,7 @@ impl TryFrom<RunArgs> for Config {
5256
upload_url,
5357
token: args.token,
5458
working_directory: args.working_directory,
59+
mode: args.mode,
5560
instruments,
5661
command: args.command.join(" "),
5762
skip_upload: args.skip_upload,
@@ -72,6 +77,7 @@ mod tests {
7277
upload_url: None,
7378
token: None,
7479
working_directory: None,
80+
mode: RunnerMode::Instrumentation,
7581
instruments: vec![],
7682
mongo_uri_env_name: None,
7783
skip_upload: false,
@@ -94,6 +100,7 @@ mod tests {
94100
upload_url: Some("https://example.com/upload".into()),
95101
token: Some("token".into()),
96102
working_directory: Some("/tmp".into()),
103+
mode: RunnerMode::Instrumentation,
97104
instruments: vec!["mongodb".into()],
98105
mongo_uri_env_name: Some("MONGODB_URI".into()),
99106
skip_upload: true,

src/run/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::prelude::*;
44
use crate::run::{config::Config, logger::Logger};
55
use crate::VERSION;
66
use check_system::SystemInfo;
7-
use clap::Args;
7+
use clap::{Args, ValueEnum};
88
use instruments::mongo_tracer::{install_mongodb_tracer, MongoTracer};
99
use run_environment::interfaces::RunEnvironment;
1010
use runner::get_run_data;
11+
use serde::Serialize;
1112

1213
pub mod check_system;
1314
pub mod helpers;
@@ -36,6 +37,14 @@ fn show_banner() {
3637
debug!("codspeed v{}", VERSION);
3738
}
3839

40+
#[derive(ValueEnum, Clone, Default, Debug, Serialize)]
41+
#[serde(rename_all = "lowercase")]
42+
pub enum RunnerMode {
43+
#[default]
44+
Instrumentation,
45+
Walltime,
46+
}
47+
3948
#[derive(Args, Debug)]
4049
pub struct RunArgs {
4150
/// The upload URL to use for uploading the results, useful for on-premises installations
@@ -50,6 +59,10 @@ pub struct RunArgs {
5059
#[arg(long)]
5160
pub working_directory: Option<String>,
5261

62+
/// The mode to run the benchmarks in.
63+
#[arg(long, default_value_t, value_enum, env = "CODSPEED_RUNNER_MODE")]
64+
pub mode: RunnerMode,
65+
5366
/// Comma-separated list of instruments to enable. Possible values: mongodb.
5467
#[arg(long, value_delimiter = ',')]
5568
pub instruments: Vec<String>,
@@ -86,6 +99,7 @@ impl RunArgs {
8699
upload_url: None,
87100
token: None,
88101
working_directory: None,
102+
mode: RunnerMode::Instrumentation,
89103
instruments: vec![],
90104
mongo_uri_env_name: None,
91105
skip_upload: false,
@@ -117,8 +131,7 @@ pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
117131
let system_info = SystemInfo::new()?;
118132
check_system::check_system(&system_info)?;
119133

120-
let mode = runner::get_mode()?;
121-
let executor = runner::get_executor_from_mode(mode);
134+
let executor = runner::get_executor_from_mode(&config.mode);
122135

123136
if !config.skip_setup {
124137
start_group!("Preparing the environment");

src/run/runner/mod.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,36 @@
1-
use std::{env, fmt::Display};
1+
use std::fmt::Display;
22

33
use crate::prelude::*;
44

5+
use super::RunnerMode;
6+
57
mod executor;
68
mod helpers;
79
mod interfaces;
810
mod valgrind;
911
mod wall_time;
1012

11-
use anyhow::bail;
1213
use executor::Executor;
1314
use helpers::profile_folder::create_profile_folder;
1415
pub use interfaces::{ExecutorName, RunData};
1516
use valgrind::executor::ValgrindExecutor;
1617
use wall_time::executor::WallTimeExecutor;
1718

18-
pub enum RunnerMode {
19-
Instrumentation,
20-
WallTime,
21-
}
22-
2319
impl Display for RunnerMode {
2420
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2521
match self {
2622
RunnerMode::Instrumentation => write!(f, "instrumentation"),
27-
RunnerMode::WallTime => write!(f, "walltime"),
28-
}
29-
}
30-
}
31-
32-
impl TryFrom<&str> for RunnerMode {
33-
type Error = anyhow::Error;
34-
35-
fn try_from(value: &str) -> Result<Self> {
36-
match value {
37-
"instrumentation" => Ok(RunnerMode::Instrumentation),
38-
"walltime" => Ok(RunnerMode::WallTime),
39-
_ => bail!("Unknown runner mode: {}", value),
23+
RunnerMode::Walltime => write!(f, "walltime"),
4024
}
4125
}
4226
}
4327

4428
pub const EXECUTOR_TARGET: &str = "executor";
4529

46-
pub fn get_mode() -> Result<RunnerMode> {
47-
if let Ok(runner_mode) = env::var("CODSPEED_RUNNER_MODE") {
48-
debug!("CODSPEED_RUNNER_MODE is set to {}", runner_mode);
49-
RunnerMode::try_from(runner_mode.as_str())
50-
} else {
51-
debug!("CODSPEED_RUNNER_MODE is not set, using instrumentation");
52-
Ok(RunnerMode::Instrumentation)
53-
}
54-
}
55-
56-
pub fn get_executor_from_mode(mode: RunnerMode) -> Box<dyn Executor> {
30+
pub fn get_executor_from_mode(mode: &RunnerMode) -> Box<dyn Executor> {
5731
match mode {
5832
RunnerMode::Instrumentation => Box::new(ValgrindExecutor),
59-
RunnerMode::WallTime => Box::new(WallTimeExecutor),
33+
RunnerMode::Walltime => Box::new(WallTimeExecutor),
6034
}
6135
}
6236

src/run/runner/wall_time/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Executor for WallTimeExecutor {
2929
let mut cmd = Command::new("sh");
3030

3131
cmd.envs(get_base_injected_env(
32-
RunnerMode::WallTime,
32+
RunnerMode::Walltime,
3333
&run_data.profile_folder,
3434
));
3535

0 commit comments

Comments
 (0)