summaryrefslogtreecommitdiff
path: root/src/step.rs
blob: 9e3c8ebacbde1a009e5011984d551a93786e40cb (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
111
112
113
114
115
116
117
use crate::result::{Measurement, OpMeasurements, Operation};
use crate::specification::{Change, Create, FileCount};
use std::time::Instant;

/// A step in the execution of a benchmark.
#[derive(Debug)]
pub enum Step {
    /// Start a benchmark.
    Start(
        /// Unique name of the benchmark.
        String,
    ),
    /// Finish a benchmark with a given name.
    Stop(
        /// Unique name of the benchmark.
        String,
    ),
    /// Create test data files.
    Create(Create),
    /// Rename test data files.
    Rename(FileCount),
    /// Delete test data files.
    Delete(FileCount),
    /// Make the nth backup in the benchmark.
    Backup(
        /// n
        usize,
    ),
    /// Restore the nth backup in the benchmark.
    Restore(
        /// n
        usize,
    ),
}

/// Possible errors from executing a benchmark step.
#[derive(Debug, thiserror::Error)]
pub enum StepError {
    /// Generic I/O error.
    #[error(transparent)]
    Io(std::io::Error),
}

impl Step {
    pub(crate) fn from(change: &Change) -> Self {
        match change {
            Change::Create(x) => Self::Create(x.clone()),
            Change::Rename(x) => Self::Rename(x.clone()),
            Change::Delete(x) => Self::Delete(x.clone()),
        }
    }

    pub fn execute(
        &self,
        current: &mut Option<String>,
    ) -> Result<Option<OpMeasurements>, StepError> {
        let now = Instant::now();
        let om = match self {
            Self::Start(name) => {
                *current = Some(name.to_string());
                None
            }
            Self::Stop(_) => {
                *current = None;
                None
            }
            Self::Create(x) => {
                create_files(x)?;
                None
            }
            Self::Rename(x) => {
                rename_files(x)?;
                None
            }
            Self::Delete(x) => {
                delete_files(x)?;
                None
            }
            Self::Backup(x) => Some(backup(*x, current.as_ref().unwrap())?),
            Self::Restore(x) => Some(restore(*x, current.as_ref().unwrap())?),
        };

        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: &str) -> Result<OpMeasurements, StepError> {
    let mut om = OpMeasurements::new(current, Operation::Backup(i));
    om.push(Measurement::TotalFiles(0));
    om.push(Measurement::TotalData(0));
    Ok(om)
}

fn restore(i: usize, current: &str) -> Result<OpMeasurements, StepError> {
    Ok(OpMeasurements::new(current, Operation::Restore(i)))
}

fn create_files(_: &Create) -> Result<(), StepError> {
    Ok(())
}

fn rename_files(_: &FileCount) -> Result<(), StepError> {
    Ok(())
}

fn delete_files(_: &FileCount) -> Result<(), StepError> {
    Ok(())
}