summaryrefslogtreecommitdiff
path: root/src/state.rs
blob: b92565614237dbc583b7cdd76eebe1be8a7e515e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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.
#[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> {
        let now = Instant::now();
        let om = match step {
            Step::Start(name) => {
                self.current_benchmark = Some(CurrentBenchmark::new(name, self.tempdir.path())?);
                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, tempdir: &Path) -> Result<Self, DataError> {
        Ok(Self {
            name: name.to_string(),
            data: Data::new(tempdir)?,
        })
    }
}