Debian Codegraph @

debian/missing-sources/criterion/benches/benchmarks/external_process.rs

raw
1 use criterion::{criterion_group, Criterion};
2 use std::{
3 io::{BufRead, BufReader, Write},
4 process::{Command, Stdio},
5 str::FromStr,
6 time::Duration,
7 };
8
9 fn create_command() -> Command {
10 let mut command = Command::new("python3");
11 command
12 .arg("benches/benchmarks/external_process.py")
13 .arg("10");
14 command
15 }
16
17 fn python_fibonacci(c: &mut Criterion) {
18 let has_python3 = Command::new("python3")
19 .arg("--version")
20 .stdout(Stdio::null())
21 .stderr(Stdio::null())
22 .output()
23 .is_ok();
24
25 if has_python3 {
26 let mut process = create_command()
27 .stdin(Stdio::piped())
28 .stdout(Stdio::piped())
29 .spawn()
30 .expect("Unable to start python process");
31
32 let mut stdin = process
33 .stdin
34 .take()
35 .expect("Unable to get stdin for child process");
36 let stdout = process
37 .stdout
38 .take()
39 .expect("Unable to get stdout for child process");
40 let mut stdout = BufReader::new(stdout);
41 c.bench_function("fibonacci-python", |b| {
42 b.iter_custom(|iters| {
43 writeln!(stdin, "{}", iters)
44 .expect("Unable to send iteration count to child process");
45 let mut line = String::new();
46 stdout
47 .read_line(&mut line)
48 .expect("Unable to read time from child process");
49 let nanoseconds: u64 =
50 u64::from_str(line.trim()).expect("Unable to parse time from child process");
51 Duration::from_nanos(nanoseconds)
52 });
53 });
54
55 // Ensure that your child process terminates itself gracefully!
56 process.kill().expect("Unable to kill child process");
57 }
58 }
59
60 criterion_group!(benches, python_fibonacci);