summaryrefslogtreecommitdiff
path: root/src/suite.rs
blob: e9003306b9c260188783b6687ec06e87eda26e93 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::client::{ObnamClient, ObnamClientError};
use crate::daemon::DaemonManager;
use crate::junk::junk;
use crate::result::{Measurement, OpMeasurements, Operation};
use crate::server::{ObnamServer, ObnamServerError};
use crate::specification::{Create, FileCount};
use crate::step::Step;
use log::{debug, info};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tempfile::{tempdir, TempDir};
use walkdir::WalkDir;

/// A running benchmark suite.
///
/// This manages temporary data created for the benchmarks, and
/// executes individual steps in the suite.
pub struct Suite {
    manager: DaemonManager,
    benchmark: Option<Benchmark>,
}

/// Possible errors from running a benchmark suite.
#[derive(Debug, thiserror::Error)]
pub enum SuiteError {
    /// Error creating a temporary directory.
    #[error(transparent)]
    TempDir(#[from] std::io::Error),

    /// File creation failed.
    #[error("Failed to create file {0}: {1}")]
    CreateFile(PathBuf, std::io::Error),

    /// Error from counting files.
    #[error("Error counting files in {0}: {1}")]
    FileCount(PathBuf, walkdir::Error),

    /// Error looking up file metadata.
    #[error("Error looking up file metadata: {0}: {1}")]
    FileMeta(PathBuf, walkdir::Error),

    /// Error using an Obnam client.
    #[error(transparent)]
    Client(#[from] ObnamClientError),

    /// Error managing an Obnam server.
    #[error(transparent)]
    Server(#[from] ObnamServerError),
}

impl Suite {
    pub fn new() -> Result<Self, SuiteError> {
        Ok(Self {
            manager: DaemonManager::new(),
            benchmark: None,
        })
    }

    /// Execute one step in the benchmark suite.
    ///
    /// Return a measurement of the step.
    pub fn execute(&mut self, step: &Step) -> Result<OpMeasurements, SuiteError> {
        let time = Instant::now();
        eprintln!("step: {:?}", step);
        let mut om = match step {
            Step::Start(name) => {
                assert!(self.benchmark.is_none());
                let mut benchmark = Benchmark::new(name, &self.manager)?;
                let om = benchmark.start()?;
                self.benchmark = Some(benchmark);
                om
            }
            Step::Stop(name) => {
                assert!(self.benchmark.is_some());
                assert_eq!(name, self.benchmark.as_ref().unwrap().name());
                let om = self.benchmark.as_mut().unwrap().stop()?;
                self.benchmark = None;
                om
            }
            Step::Create(x) => {
                assert!(self.benchmark.is_some());
                self.benchmark.as_mut().unwrap().create(x)?
            }
            Step::Rename(x) => {
                assert!(self.benchmark.is_some());
                self.benchmark.as_mut().unwrap().rename(x)?
            }
            Step::Delete(x) => {
                assert!(self.benchmark.is_some());
                self.benchmark.as_mut().unwrap().delete(x)?
            }
            Step::Backup(x) => {
                assert!(self.benchmark.is_some());
                self.benchmark.as_mut().unwrap().backup(*x)?
            }
            Step::Restore(x) => {
                assert!(self.benchmark.is_some());
                self.benchmark.as_mut().unwrap().restore(*x)?
            }
        };

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

        let ms = time.elapsed().as_millis();
        debug!("step duration was {} ms", ms);
        om.push(Measurement::DurationMs(ms));
        Ok(om)
    }
}

struct Benchmark {
    name: String,
    client: ObnamClient,
    server: ObnamServer,
    live: TempDir,
}

impl Benchmark {
    fn new(name: &str, manager: &DaemonManager) -> Result<Self, SuiteError> {
        let server = ObnamServer::new(manager)?;
        let live = tempdir().map_err(SuiteError::TempDir)?;
        let client = ObnamClient::new(server.url(), live.path().to_path_buf())?;
        Ok(Self {
            name: name.to_string(),
            client,
            server,
            live,
        })
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn live(&self) -> PathBuf {
        self.live.path().to_path_buf()
    }

    fn start(&mut self) -> Result<OpMeasurements, SuiteError> {
        info!("starting benchmark {}", self.name());
        self.client
            .run(&["init", "--insecure-passphrase=hunter2"])?;
        Ok(OpMeasurements::new(self.name(), Operation::Start))
    }

    fn stop(&mut self) -> Result<OpMeasurements, SuiteError> {
        info!("ending benchmark {}", self.name);
        self.server.stop();
        Ok(OpMeasurements::new(self.name(), Operation::Stop))
    }

    fn create(&mut self, create: &Create) -> Result<OpMeasurements, SuiteError> {
        let root = self.live();
        info!(
            "creating {} files of {} bytes each in {}",
            create.files,
            create.file_size,
            root.display()
        );

        for i in 0..create.files {
            let filename = root.join(format!("{}", i));
            let mut f =
                File::create(&filename).map_err(|err| SuiteError::CreateFile(filename, err))?;
            junk(&mut f, create.file_size)?;
        }

        Ok(OpMeasurements::new(self.name(), Operation::Create))
    }

    fn rename(&mut self, count: &FileCount) -> Result<OpMeasurements, SuiteError> {
        info!("renaming {} test data files", count.files);
        Ok(OpMeasurements::new(self.name(), Operation::Rename))
    }

    fn delete(&mut self, count: &FileCount) -> Result<OpMeasurements, SuiteError> {
        info!("deleting {} test data files", count.files);
        Ok(OpMeasurements::new(self.name(), Operation::Delete))
    }

    fn backup(&mut self, n: usize) -> Result<OpMeasurements, SuiteError> {
        info!("making backup {} in benchmark {}", n, self.name());
        self.client.run(&["backup"])?;
        let mut om = OpMeasurements::new(self.name(), Operation::Backup);
        let stats = filestats(&self.live())?;
        om.push(Measurement::TotalFiles(stats.count));
        om.push(Measurement::TotalData(stats.size));
        Ok(om)
    }

    fn restore(&mut self, n: usize) -> Result<OpMeasurements, SuiteError> {
        info!("restoring backup {} in benchmark {}", n, self.name());
        Ok(OpMeasurements::new(self.name(), Operation::Restore))
    }
}

#[derive(Debug, Default)]
struct FileStats {
    count: u64,
    size: u64,
}

fn filestats(dirname: &Path) -> Result<FileStats, SuiteError> {
    let mut stats = FileStats::default();
    for e in WalkDir::new(dirname) {
        let e = e.map_err(|err| SuiteError::FileCount(dirname.to_path_buf(), err))?;
        stats.count += 1;
        stats.size += e
            .metadata()
            .map_err(|err| SuiteError::FileMeta(e.path().to_path_buf(), err))?
            .len();
    }
    Ok(stats)
}