-
Notifications
You must be signed in to change notification settings - Fork 6
/
pewpew.rs
313 lines (305 loc) · 11.4 KB
/
pewpew.rs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::{convert::TryInto, ffi::OsStr, fs::create_dir_all, io, path::PathBuf, time::UNIX_EPOCH};
use clap::{builder::ValueParser, crate_version, Arg, Command};
use config::duration_from_string;
use futures::channel::mpsc as futures_channel;
use log::{debug, info};
use pewpew::{
create_run, ExecConfig, RunConfig, RunOutputFormat, StatsFileFormat, TryConfig, TryFilter,
TryRunFormat,
};
use regex::Regex;
use tokio::runtime;
use yansi::Paint;
fn main() {
#[cfg(target_os = "windows")]
{
if !Paint::enable_windows_ascii() {
Paint::disable();
}
}
if atty::isnt(atty::Stream::Stdout) {
Paint::disable();
}
let filter_reg = Regex::new("^(.*?)(!=|=)(.*)").expect("is a valid regex");
let filter_reg2 = filter_reg.clone();
let matches = Command::new("pewpew")
.about("The HTTP load test tool https://familysearch.github.io/pewpew")
.version(crate_version!())
.disable_help_subcommand(true)
.infer_subcommands(true)
.subcommand_required(true)
.arg_required_else_help(true)
// .setting(AppSettings::VersionlessSubcommands)
.subcommand(Command::new("run")
.about("Runs a full load test")
// .setting(AppSettings::UnifiedHelpMessage) // Now the default
.arg(
Arg::new("output-format")
.short('f')
.long("output-format")
.help("Formatting for stats printed to stderr")
.value_name("FORMAT")
.possible_value("human")
.possible_value("json")
.default_value("human")
)
.arg(
Arg::new("stats-file")
.short('o')
.long("stats-file")
.help("Specify the filename for the stats file")
.value_name("STATS_FILE")
.value_parser(ValueParser::os_string())
)
.arg(
Arg::new("start-at")
.short('t')
.long("start-at")
.help("Specify the time the test should start at")
.value_name("START_AT")
.validator(|s| {
match duration_from_string(s.into()) {
Ok(_) => Ok(()),
Err(_) => Err("".to_string()),
}
})
)
.arg(
Arg::new("results-directory")
.short('d')
.long("results-directory")
.number_of_values(1)
.help("Directory to store results and logs")
.value_name("DIRECTORY")
.value_parser(ValueParser::os_string())
)
.arg(
Arg::new("stats-file-format")
.short('s')
.long("stats-file-format")
.help("Format for the stats file")
.value_name("FORMAT")
.possible_value("json")
// .possible_value("html")
// .possible_value("none")
.default_value("json")
)
.arg(
Arg::new("watch")
.short('w')
.long("watch")
.help("Watch the config file for changes and update the test accordingly")
)
.arg(
Arg::new("CONFIG")
.help("Load test config file to use")
.required(true),
)
)
.subcommand(Command::new("try")
.about("Runs the specified endpoint(s) a single time for testing purposes")
// .setting(AppSettings::UnifiedHelpMessage) // Now the default
.arg(
Arg::new("loggers")
.short('l')
.long("loggers")
.help("Enable loggers defined in the config file")
)
.arg(
Arg::new("file")
.short('o')
.long("file")
.help("Send results to the specified file instead of stdout")
.value_name("FILE")
)
.arg(
Arg::new("format")
.short('f')
.long("format")
.help("Specify the format for the try run output")
.value_name("FORMAT")
.possible_value("human")
.possible_value("json")
.default_value("human")
)
.arg(
Arg::new("include")
.short('i')
.long("include")
.long_help(r#"Filter which endpoints are included in the try run. Filters work based on an endpoint's tags. Filters are specified in the format "key=value" where "*" is a wildcard. Any endpoint matching the filter is included in the test"#)
.multiple_occurrences(true)
.number_of_values(1)
.validator(move |s| {
if filter_reg2.is_match(s) {
Ok(())
} else {
Err("include filters must be in the format `tag=value` or `tag!=value`".to_string())
}
})
.value_name("INCLUDE")
)
.arg(
Arg::new("results-directory")
.short('d')
.long("results-directory")
.number_of_values(1)
.help("Directory to store logs (if enabled with --loggers)")
.value_name("DIRECTORY")
)
.arg(
Arg::new("CONFIG")
.help("Load test config file to use")
.required(true),
)
)
.get_matches();
let (ctrl_c_tx, ctrlc_channel) = futures_channel::unbounded();
let _ = ctrlc::set_handler(move || {
let _ = ctrl_c_tx.unbounded_send(());
});
let cli_config = if let Some(matches) = matches.subcommand_matches("run") {
let config_file: PathBuf = matches
.value_of("CONFIG")
.expect("should have CONFIG param")
.into();
let results_dir = matches.value_of_os("results-directory").map(|d: &OsStr| {
create_dir_all(d).unwrap();
PathBuf::from(d)
});
let output_format = TryInto::try_into(
matches
.value_of("output-format")
.expect("should have output_format cli arg"),
)
.expect("output_format cli arg unrecognized");
match output_format {
RunOutputFormat::Json => {
json_env_logger::init();
json_env_logger::panic_hook();
}
_ => env_logger::init(),
}
info!("log::max_level() = {}", log::max_level());
let stats_file = matches
.value_of_os("stats-file")
.map(PathBuf::from)
.unwrap_or_else(|| {
let start_sec = UNIX_EPOCH
.elapsed()
.map(|d| d.as_secs())
.unwrap_or_default();
let test_name = config_file.file_stem().and_then(std::ffi::OsStr::to_str);
let file = if let Some(test_name) = test_name {
format!("stats-{}-{}.json", test_name, start_sec)
} else {
format!("stats-{}.json", start_sec)
};
PathBuf::from(file)
});
let stats_file = if let Some(results_dir) = &results_dir {
let mut file = results_dir.clone();
file.push(stats_file);
file
} else {
stats_file
};
let stats_file_format = StatsFileFormat::Json;
let watch_config_file = matches.is_present("watch");
let start_at = matches
.value_of("start-at")
.map(|s| duration_from_string(s.to_string()).expect("start_at should match pattern"));
let run_config = RunConfig {
config_file,
output_format,
results_dir,
start_at,
stats_file,
stats_file_format,
watch_config_file,
};
debug!("{{\"run_config\":{}}}", run_config);
ExecConfig::Run(run_config)
} else if let Some(matches) = matches.subcommand_matches("try") {
let config_file: PathBuf = matches
.value_of("CONFIG")
.expect("should have CONFIG param")
.into();
let results_dir = matches.value_of("results-directory");
let loggers_on = matches.is_present("loggers");
let results_dir = match (results_dir, loggers_on) {
(Some(d), true) => {
create_dir_all(d).unwrap();
Some(d.into())
}
_ => None,
};
let filters = matches.values_of("include").map(|v| {
v.map(|s| {
let captures = filter_reg
.captures(s)
.expect("include cli arg should match regex");
let left = captures
.get(1)
.expect("include arg should match regex")
.as_str()
.to_string();
let right = captures
.get(3)
.expect("include arg should match regex")
.as_str()
.to_string();
let comparator = captures
.get(2)
.expect("include arg should match regex")
.as_str();
match comparator {
"=" => TryFilter::Eq(left, right),
"!=" => TryFilter::Ne(left, right),
_ => unreachable!(),
}
})
.collect()
});
let format: TryRunFormat = matches
.value_of("format")
.and_then(|f| f.try_into().ok())
.unwrap_or_default();
match format {
TryRunFormat::Json => {
json_env_logger::init();
json_env_logger::panic_hook();
}
_ => env_logger::init(),
}
info!("log::max_level()={}", log::max_level());
let file = matches.value_of("file").map(Into::into);
let try_config = TryConfig {
config_file,
file,
filters,
format,
loggers_on,
results_dir,
};
debug!("{{\"try_config\":{}}}", try_config);
ExecConfig::Try(try_config)
} else {
unreachable!();
};
let f = create_run(cli_config, ctrlc_channel, io::stdout(), io::stderr());
let rt = runtime::Builder::new_multi_thread()
.enable_time()
.enable_io()
.thread_name("pewpew-worker")
.build()
.unwrap();
debug!("rt.block_on start");
let result = rt.block_on(f);
debug!("rt.block_on finished. result: {:?}", result);
// shutdown the runtime in case there are any hanging threads/tasks
rt.shutdown_timeout(Default::default());
debug!("rt.shutdown_timeout finished");
if result.is_err() {
std::process::exit(1)
}
}