summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs119
1 files changed, 0 insertions, 119 deletions
diff --git a/src/state.rs b/src/state.rs
deleted file mode 100644
index d70af11..0000000
--- a/src/state.rs
+++ /dev/null
@@ -1,119 +0,0 @@
-use crate::data::{Data, DataError};
-use crate::result::{Measurement, OpMeasurements, Operation};
-use crate::step::Step;
-use log::{debug, info};
-use std::path::Path;
-use std::time::Instant;
-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.
-#[derive(Debug, thiserror::Error)]
-pub enum StateError {
- /// A step is executed in the wrong order.
- #[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() -> Result<Self, StateError> {
- Ok(Self {
- current_benchmark: None,
- tempdir: tempdir()?,
- })
- }
-
- fn current(&self) -> Result<&CurrentBenchmark, StateError> {
- if let Some(x) = &self.current_benchmark {
- Ok(x)
- } else {
- Err(StateError::OutOfStep)
- }
- }
-
- pub fn execute(&mut self, step: &Step) -> Result<Option<OpMeasurements>, StateError> {
- debug!("executing step {:?}", step);
- let now = Instant::now();
- let om = match step {
- Step::Start(name) => {
- info!("starting benchmark {}", name);
- self.current_benchmark = Some(CurrentBenchmark::new(name, self.tempdir.path())?);
- None
- }
- Step::Stop(name) => {
- info!("ending benchmark {}", name);
- self.current_benchmark = None;
- None
- }
- Step::Create(x) => {
- info!("creating {} test data files", x.files);
- self.current()?.data.create(x)?;
- None
- }
- Step::Rename(x) => {
- info!("renaming {} test data files", x.files);
- self.current()?.data.rename(x)?;
- None
- }
- Step::Delete(x) => {
- info!("deleting {} test data files", x.files);
- self.current()?.data.delete(x)?;
- None
- }
- Step::Backup(x) => Some(backup(*x, self.current()?)?),
- Step::Restore(x) => Some(restore(*x, self.current()?)?),
- };
-
- let t = std::time::Duration::from_millis(10);
- std::thread::sleep(t);
-
- if let Some(mut om) = om {
- let ms = now.elapsed().as_millis();
- debug!("step duration was {} ms", ms);
- om.push(Measurement::DurationMs(ms));
- Ok(Some(om))
- } else {
- Ok(None)
- }
- }
-}
-
-fn backup(i: usize, current: &CurrentBenchmark) -> Result<OpMeasurements, StateError> {
- info!("backing up generation number {}", i);
- let mut om = OpMeasurements::new(&current.name, Operation::Backup(i));
- om.push(Measurement::TotalFiles(current.data.file_count()?));
- om.push(Measurement::TotalData(current.data.file_size()?));
- Ok(om)
-}
-
-fn restore(i: usize, current: &CurrentBenchmark) -> Result<OpMeasurements, StateError> {
- info!("restoring generation number {}", i);
- Ok(OpMeasurements::new(&current.name, Operation::Restore(i)))
-}
-
-struct CurrentBenchmark {
- name: String,
- data: Data,
-}
-
-impl CurrentBenchmark {
- fn new(name: &str, tempdir: &Path) -> Result<Self, DataError> {
- Ok(Self {
- name: name.to_string(),
- data: Data::new(tempdir)?,
- })
- }
-}