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
create_command() -> Command9 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 #[allow(deprecated)]
python_fibonacci(c: &mut Criterion)18 fn python_fibonacci(c: &mut Criterion) {
19 let has_python3 = Command::new("python3")
20 .arg("--version")
21 .stdout(Stdio::null())
22 .stderr(Stdio::null())
23 .output()
24 .is_ok();
25
26 if has_python3 {
27 let process = create_command()
28 .stdin(Stdio::piped())
29 .stdout(Stdio::piped())
30 .spawn()
31 .expect("Unable to start python process");
32
33 let mut stdin = process
34 .stdin
35 .expect("Unable to get stdin for child process");
36 let stdout = process
37 .stdout
38 .expect("Unable to get stdout for child process");
39 let mut stdout = BufReader::new(stdout);
40 c.bench_function("fibonacci-python", |b| {
41 b.iter_custom(|iters| {
42 writeln!(stdin, "{}", iters)
43 .expect("Unable to send iteration count to child process");
44 let mut line = String::new();
45 stdout
46 .read_line(&mut line)
47 .expect("Unable to read time from child process");
48 let nanoseconds: u64 =
49 u64::from_str(line.trim()).expect("Unable to parse time from child process");
50 Duration::from_nanos(nanoseconds)
51 })
52 });
53
54 // Ensure that your child process terminates itself gracefully!
55 }
56 }
57
58 criterion_group!(benches, python_fibonacci);
59