summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-12-05 21:01:44 +0200
committerLars Wirzenius <liw@liw.fi>2021-12-05 21:01:44 +0200
commitcfd03ee39e5fd5bef42e1de229a0868fae909c33 (patch)
tree6a8dbad109cd7b5f70e2f60df6210dbbe2cb86ec
parenta4221521b353009727608263a1d169b4abc7033f (diff)
downloadobnam-benchmark-cfd03ee39e5fd5bef42e1de229a0868fae909c33.tar.gz
feat: create a temporary directory for test data
Sponsored-by: author
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/bin/obnam-benchmark.rs2
-rw-r--r--src/data.rs13
-rw-r--r--src/state.rs20
5 files changed, 26 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bd4e55c..3673319 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -587,6 +587,7 @@ dependencies = [
"structopt",
"subplot-build",
"subplotlib",
+ "tempfile",
"thiserror",
]
diff --git a/Cargo.toml b/Cargo.toml
index 2979f4d..79ea8d2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ serde = { version = "1.0.101", features = ["derive"] }
serde_json = "1.0.72"
serde_yaml = "0.8.21"
structopt = "0.3.25"
+tempfile = "3.2.0"
thiserror = "1.0.30"
[build-dependencies]
diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs
index 418a46e..0ad8491 100644
--- a/src/bin/obnam-benchmark.rs
+++ b/src/bin/obnam-benchmark.rs
@@ -53,7 +53,7 @@ struct Run {
impl Run {
fn run(&self) -> anyhow::Result<()> {
let spec = Specification::from_file(&self.spec)?;
- let mut state = State::new();
+ let mut state = State::new()?;
let mut result = Result::default();
for step in spec.steps() {
if let Some(m) = state.execute(&step)? {
diff --git a/src/data.rs b/src/data.rs
index aaaebc9..04376bc 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -1,4 +1,5 @@
use crate::specification::{Create, FileCount};
+use std::path::{Path, PathBuf};
/// Test data management for Obnam benchmarks.
///
@@ -6,7 +7,9 @@ use crate::specification::{Create, FileCount};
/// that backup. That data is later cleaned up. Is all handled by this
/// structure.
#[derive(Debug)]
-pub struct Data {}
+pub struct Data {
+ tempdir: PathBuf,
+}
/// Possible errors from managing data.
#[derive(Debug, thiserror::Error)]
@@ -14,12 +17,14 @@ pub enum DataError {
}
impl Data {
- pub(crate) fn new() -> Result<Self, DataError> {
- Ok(Self {})
+ pub(crate) fn new(tempdir: &Path) -> Result<Self, DataError> {
+ Ok(Self {
+ tempdir: tempdir.to_path_buf(),
+ })
}
pub(crate) fn create(&self, create: &Create) -> Result<(), DataError> {
- println!("create {:?}", create);
+ println!("create {:?} in {}", create, self.tempdir.display());
Ok(())
}
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)?,
})
}
}