summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/state.rs b/src/state.rs
index bce5115..b925656 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -2,10 +2,13 @@ use crate::data::{Data, DataError};
use crate::result::{Measurement, OpMeasurements, Operation};
use crate::step::Step;
use std::time::Instant;
+use std::path::{Path};
+use tempfile::{tempdir, TempDir};
/// Runtime state for running Obnam benchmarks.
pub struct State {
current_benchmark: Option<CurrentBenchmark>,
+ tempdir: TempDir,
}
/// Possible errors from changing benchmark state.
@@ -15,16 +18,21 @@ pub enum StateError {
#[error("Internal error: step expected inside a benchmark")]
OutOfStep,
+ /// Failed to create a temporary directory.
+ #[error(transparent)]
+ CreateTemp(#[from] std::io::Error),
+
/// Error managing test data.
#[error(transparent)]
Data(#[from] DataError),
}
impl State {
- pub fn new() -> Self {
- Self {
+ pub fn new() -> Result<Self, StateError> {
+ Ok(Self {
current_benchmark: None,
- }
+ tempdir: tempdir()?,
+ })
}
fn current(&self) -> Result<&CurrentBenchmark, StateError> {
@@ -39,7 +47,7 @@ impl State {
let now = Instant::now();
let om = match step {
Step::Start(name) => {
- self.current_benchmark = Some(CurrentBenchmark::new(name)?);
+ self.current_benchmark = Some(CurrentBenchmark::new(name, self.tempdir.path())?);
None
}
Step::Stop(_) => {
@@ -93,10 +101,10 @@ struct CurrentBenchmark {
}
impl CurrentBenchmark {
- fn new(name: &str) -> Result<Self, DataError> {
+ fn new(name: &str, tempdir: &Path) -> Result<Self, DataError> {
Ok(Self {
name: name.to_string(),
- data: Data::new()?,
+ data: Data::new(tempdir)?,
})
}
}