in midst of refactor

This commit is contained in:
2024-04-29 21:46:42 +02:00
parent ed0d64d52e
commit aa3f7e99ab
221 changed files with 5431 additions and 685 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "stdio-perftest"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@@ -0,0 +1,44 @@
use std::env::{self, args};
use std::io::{stdin, BufRead, BufReader, Read, Write};
use std::process::{self, Child};
use std::time::SystemTime;
fn main() {
let is_child = env::args().any(|arg| arg == "child");
if is_child {
loop {
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
if input == "ping\n" {
println!("pong");
} else if input.is_empty() {
process::exit(0);
} else {
panic!("Unrecognized input {input:?}");
}
}
} else {
let steps = 10_000;
let mut child = process::Command::new(args().next().unwrap())
.arg("child")
.stdin(process::Stdio::piped())
.stdout(process::Stdio::piped())
.spawn()
.unwrap();
let mut bufr = BufReader::new(child.stdout.take().unwrap());
let mut child_stdin = child.stdin.take().unwrap();
let time = SystemTime::now();
for _ in 0..steps {
writeln!(child_stdin, "ping");
let mut buf = String::new();
bufr.read_line(&mut buf).unwrap();
if buf != "pong\n" {
panic!("Unrecognized output {buf:?}")
}
}
child.kill();
let elapsed = time.elapsed().unwrap();
let avg = elapsed / steps;
println!("A roundtrip takes {avg:?}, {}ms on average", (avg.as_nanos() as f64) / 1_000_000f64);
}
}