summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/state.rs b/src/state.rs
new file mode 100644
index 0000000..bce5115
--- /dev/null
+++ b/src/state.rs
@@ -0,0 +1,102 @@
+use crate::data::{Data, DataError};
+use crate::result::{Measurement, OpMeasurements, Operation};
+use crate::step::Step;
+use std::time::Instant;
+
+/// Runtime state for running Obnam benchmarks.
+pub struct State {
+ current_benchmark: Option<CurrentBenchmark>,
+}
+
+/// 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,
+
+ /// Error managing test data.
+ #[error(transparent)]
+ Data(#[from] DataError),
+}
+
+impl State {
+ pub fn new() -> Self {
+ Self {
+ current_benchmark: None,
+ }
+ }
+
+ 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> {
+ let now = Instant::now();
+ let om = match step {
+ Step::Start(name) => {
+ self.current_benchmark = Some(CurrentBenchmark::new(name)?);
+ None
+ }
+ Step::Stop(_) => {
+ self.current_benchmark = None;
+ None
+ }
+ Step::Create(x) => {
+ self.current()?.data.create(x)?;
+ None
+ }
+ Step::Rename(x) => {
+ self.current()?.data.rename(x)?;
+ None
+ }
+ Step::Delete(x) => {
+ self.current()?.data.delete(x)?;
+ None
+ }
+ Step::Backup(x) => Some(backup(*x, self.current()?)?),
+ Step::Restore(x) => Some(restore(*x, self.current()?)?),
+ };
+
+ println!("{:?}", step);
+ let t = std::time::Duration::from_millis(10);
+ std::thread::sleep(t);
+
+ if let Some(mut om) = om {
+ let ms = now.elapsed().as_millis();
+ om.push(Measurement::DurationMs(ms));
+ Ok(Some(om))
+ } else {
+ Ok(None)
+ }
+ }
+}
+
+fn backup(i: usize, current: &CurrentBenchmark) -> Result<OpMeasurements, StateError> {
+ let mut om = OpMeasurements::new(&current.name, Operation::Backup(i));
+ om.push(Measurement::TotalFiles(0));
+ om.push(Measurement::TotalData(0));
+ Ok(om)
+}
+
+fn restore(i: usize, current: &CurrentBenchmark) -> Result<OpMeasurements, StateError> {
+ Ok(OpMeasurements::new(&current.name, Operation::Restore(i)))
+}
+
+struct CurrentBenchmark {
+ name: String,
+ data: Data,
+}
+
+impl CurrentBenchmark {
+ fn new(name: &str) -> Result<Self, DataError> {
+ Ok(Self {
+ name: name.to_string(),
+ data: Data::new()?,
+ })
+ }
+}