Skip to content

Commit

Permalink
Use Measurement over Result
Browse files Browse the repository at this point in the history
Signed-off-by: Tej Chajed <[email protected]>
  • Loading branch information
tchajed committed Jul 29, 2023
1 parent 414c86b commit 5a87f62
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions benchmarking/src/bin/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::time::Duration;

use benchmarking::{
run::{get_examples, BenchmarkResult},
run::{get_examples, BenchmarkMeasurement},
time_bench::{compile_flyvy_bin, compile_time_bin, REPO_ROOT_PATH},
};
use clap::Parser;
Expand All @@ -27,15 +27,15 @@ struct App {
command: Command,
}

fn benchmark_verify(time_limit: Duration) -> Vec<BenchmarkResult> {
fn benchmark_verify(time_limit: Duration) -> Vec<BenchmarkMeasurement> {
get_examples()
.into_iter()
.map(|file| {
println!(
"verify {}",
file.strip_prefix(REPO_ROOT_PATH()).unwrap().display()
);
BenchmarkResult::run(vec![String::from("verify")], vec![], file, time_limit)
BenchmarkMeasurement::run(vec![String::from("verify")], vec![], file, time_limit)
})
.collect()
}
Expand All @@ -49,7 +49,7 @@ impl App {
match &self.command {
Command::Verify { time_limit } => {
let results = benchmark_verify((*time_limit).into());
BenchmarkResult::print_table(results);
BenchmarkMeasurement::print_table(results);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions benchmarking/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::{error::Error, time::Duration};

use serde::{Deserialize, Serialize};

/// RunResult holds the statistics captured from a single run. This includes
/// `RunMeasurement` holds the statistics captured from a single run. This includes
/// resource usage through `getrusage` (the same system call that powers
/// `time`), as well as basic info about whether the process exited with an
/// error or not.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunResult {
pub struct RunMeasurement {
/// Elapsed time of run.
pub real_time: Duration,
/// User time (including all child processes like solvers).
Expand All @@ -29,7 +29,7 @@ pub struct RunResult {
pub success: bool,
}

impl RunResult {
impl RunMeasurement {
/// Maximum memory usage in MB.
pub fn max_mem_mb(&self) -> usize {
self.max_mem_bytes / 1024 / 1024
Expand Down
24 changes: 12 additions & 12 deletions benchmarking/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::{collections::HashMap, ffi::OsStr, path::PathBuf, time::Duration};

use crate::{
result::RunResult,
result::RunMeasurement,
time_bench::{Time, REPO_ROOT_PATH},
};

Expand All @@ -19,14 +19,14 @@ use walkdir::WalkDir;

/// A benchmark configuration and its resulting measurement.
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
pub struct BenchmarkMeasurement {
command: Vec<String>,
params: String,
file: PathBuf,
result: RunResult,
measurement: RunMeasurement,
}

impl BenchmarkResult {
impl BenchmarkMeasurement {
/// Run flyvy on a single benchmark.
pub fn run(
command: Vec<String>,
Expand All @@ -40,12 +40,12 @@ impl BenchmarkResult {
timer.args(&args);
timer.arg(&file);
// eprintln!("{}", timer.cmdline());
let result = timer.run().expect("error getting timing");
BenchmarkResult {
let measurement = timer.run().expect("error getting timing");
BenchmarkMeasurement {
command,
params: args.join(" "),
file,
result,
measurement,
}
}

Expand All @@ -66,9 +66,9 @@ impl BenchmarkResult {
}

fn success(&self) -> &'static str {
if self.result.timed_out {
if self.measurement.timed_out {
"timeout"
} else if self.result.success {
} else if self.measurement.success {
""
} else {
"fail"
Expand All @@ -84,9 +84,9 @@ impl BenchmarkResult {
format!("{}", self.command.join(" ")),
format!("{}", file_name.display()),
format!("{}", self.success()),
format!("{:0.1}", self.result.real_time.as_secs_f64()),
format!("{:0.1}", self.result.solver_time().as_secs_f64()),
format!("{}MB", self.result.max_mem_bytes / 1024 / 1024),
format!("{:0.1}", self.measurement.real_time.as_secs_f64()),
format!("{:0.1}", self.measurement.solver_time().as_secs_f64()),
format!("{}MB", self.measurement.max_mem_bytes / 1024 / 1024),
format!("{}", self.params),
]
}
Expand Down
20 changes: 10 additions & 10 deletions benchmarking/src/time_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use nix::{
};
use process_sync::{SharedCondvar, SharedMutex};

use crate::result::RunResult;
use crate::result::RunMeasurement;

/// The `time` utility runs a benchmark, optionally kills it after a timeout,
/// and gathers resource usage statistics.
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn compile_flyvy_bin() {
}

#[derive(Debug, Clone)]
struct RawRunResults {
struct RawRunMeasurement {
real_time: Duration,
usage: Usage,
self_usage: Usage,
Expand All @@ -133,9 +133,9 @@ fn time_val_to_duration(time: TimeVal) -> Duration {
Duration::from_secs(time.tv_sec() as u64) + Duration::from_micros(time.tv_usec() as u64)
}

impl RawRunResults {
fn into_result(self) -> RunResult {
RunResult {
impl RawRunMeasurement {
fn into_result(self) -> RunMeasurement {
RunMeasurement {
real_time: self.real_time,
user_time: time_val_to_duration(self.usage.user_time()),
sys_time: time_val_to_duration(self.usage.system_time()),
Expand All @@ -148,7 +148,7 @@ impl RawRunResults {
}

impl Time {
fn get_child_results(&self, child: i32) -> Result<RawRunResults, io::Error> {
fn get_child_results(&self, child: i32) -> Result<RawRunMeasurement, io::Error> {
let child = Pid::from_raw(child);
let start = Instant::now();
let timeout = Arc::new(Mutex::new(false));
Expand Down Expand Up @@ -181,15 +181,15 @@ impl Time {
let real_time = start.elapsed();
let usage = getrusage(UsageWho::RUSAGE_CHILDREN)?;
let self_usage = getrusage(UsageWho::RUSAGE_SELF)?;
let results = RawRunResults {
let measurement = RawRunMeasurement {
real_time,
usage,
self_usage,
timed_out: *timeout.lock().unwrap(),
status,
signal,
};
Ok(results)
Ok(measurement)
}

/// Run `time` with the arguments in `self`. This uses `fork` and `exec`
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Time {
///
/// This must use the binary rather than running `exec()` directly in order
/// to isolate resource usage measurement.
pub fn run(&self) -> Result<RunResult, io::Error> {
pub fn run(&self) -> Result<RunMeasurement, io::Error> {
let mut cmd = Command::new(time_bin());
if let Some(limit) = &self.time_limit {
cmd.arg("--time-limit");
Expand All @@ -302,7 +302,7 @@ impl Time {
cmd.args(&self.args);
let output = cmd.output()?;
let output = std::str::from_utf8(&output.stdout).expect("non utf-8 output");
match RunResult::from_json(output) {
match RunMeasurement::from_json(output) {
Ok(r) => Ok(r),
Err(err) => {
eprintln!("{output}");
Expand Down

0 comments on commit 5a87f62

Please sign in to comment.