summaryrefslogtreecommitdiff
path: root/src/step.rs
blob: 1142765a866bf8802917ddf36726e009c7e7824f (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
use crate::result::{OpMeasurements, Operation, Measurement};
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 op = match self {
            Self::Start(name) => {
                *current = Some(name.to_string());
                println!("start benchmark {}", name);
                None
            }
            Self::Stop(name) => {
                *current = None;
                println!("stop benchmark {}", name);
                None
            }
            Self::Create(x) => {
                println!("create {} files of {} bytes", x.files, x.file_size);
                None
            }
            Self::Rename(x) => {
                println!("rename {}", x.files);
                None
            }
            Self::Delete(x) => {
                println!("delete {}", x.files);
                None
            }
            Self::Backup(x) => {
                println!("backup {}", x);
                Some(Operation::Backup)
            }
            Self::Restore(x) => {
                println!("restore {}", x);
                Some(Operation::Restore)
            }
        };

        let t = std::time::Duration::from_millis(10);
        std::thread::sleep(t);

        let m = if let Some(op) = op {
            let ms = now.elapsed().as_millis();
            assert!(current.is_some());
            let current = current.as_ref().unwrap();
            let mut om = OpMeasurements::new(current, op);
            om.push(Measurement::DurationMs(ms));
            Some(om)
        } else {
            None
        };
        Ok(m)
    }
}