-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmod.rs
More file actions
245 lines (209 loc) · 7.57 KB
/
mod.rs
File metadata and controls
245 lines (209 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::api_client::CodSpeedAPIClient;
use crate::config::CodSpeedConfig;
use crate::prelude::*;
use crate::run::{config::Config, logger::Logger};
use crate::VERSION;
use check_system::SystemInfo;
use clap::{Args, ValueEnum};
use instruments::mongo_tracer::{install_mongodb_tracer, MongoTracer};
use run_environment::interfaces::{RepositoryProvider, RunEnvironment};
use runner::get_run_data;
use serde::Serialize;
use std::path::PathBuf;
pub mod check_system;
pub mod helpers;
mod instruments;
mod poll_results;
pub mod run_environment;
pub mod runner;
mod uploader;
pub mod config;
pub mod logger;
fn show_banner() {
let banner = format!(
r#"
______ __ _____ __
/ ____/____ ____/ // ___/ ____ ___ ___ ____/ /
/ / / __ \ / __ / \__ \ / __ \ / _ \ / _ \ / __ /
/ /___ / /_/ // /_/ / ___/ // /_/ // __// __// /_/ /
\____/ \____/ \__,_/ /____// .___/ \___/ \___/ \__,_/
https://codspeed.io /_/ runner v{}
"#,
VERSION
);
println!("{}", banner);
debug!("codspeed v{}", VERSION);
}
#[derive(Args, Debug)]
pub struct RunArgs {
/// The upload URL to use for uploading the results, useful for on-premises installations
#[arg(long)]
pub upload_url: Option<String>,
/// The token to use for uploading the results,
#[arg(long, env = "CODSPEED_TOKEN")]
pub token: Option<String>,
/// The repository the benchmark is associated with, under the format `owner/repo`.
#[arg(short, long, env = "CODSPEED_REPOSITORY")]
pub repository: Option<String>,
/// The repository provider to use in case --repository is used. Defaults to github
#[arg(
long,
env = "CODSPEED_PROVIDER",
requires = "repository",
ignore_case = true
)]
pub provider: Option<RepositoryProvider>,
/// The directory where the command will be executed.
#[arg(long)]
pub working_directory: Option<String>,
/// The mode to run the benchmarks in.
#[arg(long, default_value_t, value_enum, env = "CODSPEED_RUNNER_MODE")]
pub mode: RunnerMode,
/// Comma-separated list of instruments to enable. Possible values: mongodb.
#[arg(long, value_delimiter = ',')]
pub instruments: Vec<String>,
/// The name of the environment variable that contains the MongoDB URI to patch.
/// If not provided, user will have to provide it dynamically through a CodSpeed integration.
///
/// Only used if the `mongodb` instrument is enabled.
#[arg(long)]
pub mongo_uri_env_name: Option<String>,
/// Profile folder to use for the run.
#[arg(long)]
pub profile_folder: Option<PathBuf>,
#[arg(long, hide = true)]
pub message_format: Option<MessageFormat>,
/// Only for debugging purposes, skips the upload of the results
#[arg(
long,
default_value = "false",
hide = true,
env = "CODSPEED_SKIP_UPLOAD"
)]
pub skip_upload: bool,
/// Used internally to upload the results after running the benchmarks in a sandbox environment
/// with no internet access
#[arg(long, default_value = "false", hide = true)]
pub skip_run: bool,
/// Only for debugging purposes, skips the setup of the runner
#[arg(long, default_value = "false", hide = true)]
pub skip_setup: bool,
/// The bench command to run
pub command: Vec<String>,
}
#[derive(ValueEnum, Clone, Default, Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RunnerMode {
#[default]
Instrumentation,
Walltime,
}
#[derive(ValueEnum, Clone, Debug, PartialEq)]
pub enum MessageFormat {
Json,
}
#[cfg(test)]
impl RunArgs {
/// Constructs a new `RunArgs` with default values for testing purposes
pub fn test() -> Self {
Self {
upload_url: None,
token: None,
repository: None,
provider: None,
working_directory: None,
mode: RunnerMode::Instrumentation,
instruments: vec![],
mongo_uri_env_name: None,
message_format: None,
profile_folder: None,
skip_upload: false,
skip_run: false,
skip_setup: false,
command: vec![],
}
}
}
pub async fn run(
args: RunArgs,
api_client: &CodSpeedAPIClient,
codspeed_config: &CodSpeedConfig,
) -> Result<()> {
let output_json = args.message_format == Some(MessageFormat::Json);
let mut config = Config::try_from(args)?;
let provider = run_environment::get_provider(&config)?;
let logger = Logger::new(&provider)?;
if provider.get_run_environment() != RunEnvironment::Local {
show_banner();
}
debug!("config: {:#?}", config);
if provider.get_run_environment() == RunEnvironment::Local {
if codspeed_config.auth.token.is_none() {
bail!("You have to authenticate the CLI first. Run `codspeed auth login`.");
}
debug!("Using the token from the CodSpeed configuration file");
config.set_token(codspeed_config.auth.token.clone());
}
let system_info = SystemInfo::new()?;
check_system::check_system(&system_info)?;
let executor = runner::get_executor_from_mode(&config.mode);
if !config.skip_setup {
start_group!("Preparing the environment");
executor.setup(&system_info).await?;
// TODO: refactor and move directly in the Instruments struct as a `setup` method
if config.instruments.is_mongodb_enabled() {
install_mongodb_tracer().await?;
}
info!("Environment ready");
end_group!();
}
let run_data = get_run_data(&config)?;
if !config.skip_run {
start_opened_group!("Running the benchmarks");
// TODO: refactor and move directly in the Instruments struct as a `start` method
let mongo_tracer = if let Some(mongodb_config) = &config.instruments.mongodb {
let mut mongo_tracer = MongoTracer::try_from(&run_data.profile_folder, mongodb_config)?;
mongo_tracer.start().await?;
Some(mongo_tracer)
} else {
None
};
executor
.run(&config, &system_info, &run_data, &mongo_tracer)
.await?;
// TODO: refactor and move directly in the Instruments struct as a `stop` method
if let Some(mut mongo_tracer) = mongo_tracer {
mongo_tracer.stop().await?;
}
executor.teardown(&config, &system_info, &run_data).await?;
logger.persist_log_to_profile_folder(&run_data)?;
end_group!();
} else {
debug!("Skipping the run of the benchmarks");
};
if !config.skip_upload {
start_group!("Uploading performance data");
let upload_result =
uploader::upload(&config, &system_info, &provider, &run_data, executor.name()).await?;
end_group!();
if provider.get_run_environment() == RunEnvironment::Local {
poll_results::poll_results(api_client, &provider, upload_result.run_id, output_json)
.await?;
end_group!();
}
}
Ok(())
}
// We have to implement this manually, because deriving the trait makes the CLI values `git-hub`
// and `git-lab`
impl clap::ValueEnum for RepositoryProvider {
fn value_variants<'a>() -> &'a [Self] {
&[Self::GitLab, Self::GitHub]
}
fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
match self {
Self::GitLab => Some(clap::builder::PossibleValue::new("gitlab").aliases(["gl"])),
Self::GitHub => Some(clap::builder::PossibleValue::new("github").aliases(["gh"])),
}
}
}