summaryrefslogtreecommitdiff
path: root/src/result.rs
blob: cce5d2d180106fb65608e7605a3a02be72c990a6 (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
use chrono::prelude::*;
use git_testament::{git_testament, render_testament};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs::File;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};

git_testament!(TESTAMENT);

#[derive(Debug, Deserialize, Serialize)]
pub struct SuiteMeasurements {
    measurements: Vec<OpMeasurements>,
    obnam_version: String,
    obnam_benchmark_version: String,
    benchmark_started: String,
    hostname: String,
    host_cpus: usize,
    host_ram: u64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OpMeasurements {
    benchmark: String,
    op: Operation,
    measurements: Vec<Measurement>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Measurement {
    TotalFiles(u64),
    TotalData(u64),
    DurationMs(u128),
}

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum Operation {
    Start,
    Stop,
    Create,
    Rename,
    Delete,
    Backup,
    Restore,
    ManifestLive,
    ManifestRestored,
    CompareManiests,
}

#[derive(Debug, thiserror::Error)]
pub enum SuiteMeasurementsError {
    #[error("failed to get CPU info: {0}")]
    CpuInfo(procfs::ProcError),

    #[error("failed to get RAM info: {0}")]
    MemInfo(procfs::ProcError),

    #[error("failed to get hostname: {0}")]
    Hostname(nix::Error),

    #[error("failed to open result file {0} for reading: {1}")]
    Open(PathBuf, std::io::Error),

    #[error("failed to read result file {0}: {1}")]
    Read(PathBuf, serde_json::Error),
}

impl SuiteMeasurements {
    pub fn new(obnam_version: String) -> Result<Self, SuiteMeasurementsError> {
        let cpu = procfs::CpuInfo::new().map_err(SuiteMeasurementsError::CpuInfo)?;
        let mem = procfs::Meminfo::new().map_err(SuiteMeasurementsError::MemInfo)?;
        let mut buf = [0u8; 1024];
        let hostname =
            nix::unistd::gethostname(&mut buf).map_err(SuiteMeasurementsError::Hostname)?;
        let hostname = hostname.to_string_lossy();
        Ok(Self {
            measurements: vec![],
            obnam_version,
            obnam_benchmark_version: render_testament!(TESTAMENT),
            benchmark_started: Utc::now().format("%Y-%m-%dT%H%M%S").to_string(),
            hostname: hostname.to_string(),
            host_ram: mem.mem_total,
            host_cpus: cpu.num_cores(),
        })
    }

    pub fn from_file(filename: &Path) -> Result<Self, SuiteMeasurementsError> {
        let data = File::open(filename)
            .map_err(|err| SuiteMeasurementsError::Open(filename.to_path_buf(), err))?;
        let m: Self = serde_json::from_reader(&data)
            .map_err(|err| SuiteMeasurementsError::Read(filename.to_path_buf(), err))?;
        Ok(m)
    }

    pub fn hostname(&self) -> &str {
        &self.hostname
    }

    pub fn timestamp(&self) -> &str {
        &self.benchmark_started
    }

    pub fn cpus(&self) -> usize {
        self.host_cpus
    }

    pub fn ram(&self) -> u64 {
        self.host_ram
    }

    pub fn obnam_version(&self) -> &str {
        self.obnam_version
            .strip_prefix("obnam-backup ")
            .or(Some(""))
            .unwrap()
    }

    pub fn push(&mut self, m: OpMeasurements) {
        self.measurements.push(m);
    }

    pub fn benchmark_names(&self) -> Vec<String> {
        let names: HashSet<&str> = HashSet::from_iter(self.measurements.iter().map(|m| m.name()));
        let mut names: Vec<String> = names.iter().map(|x| x.to_string()).collect();
        names.sort();
        names
    }

    pub fn ops(&self) -> impl Iterator<Item = &OpMeasurements> {
        self.measurements.iter()
    }
}

impl OpMeasurements {
    pub fn new(benchmark: &str, op: Operation) -> Self {
        let benchmark = benchmark.to_string();
        Self {
            benchmark,
            op,
            measurements: vec![],
        }
    }

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

    pub fn push(&mut self, m: Measurement) {
        self.measurements.push(m);
    }

    pub fn op(&self) -> Operation {
        self.op
    }

    pub fn iter(&self) -> impl Iterator<Item = &Measurement> {
        self.measurements.iter()
    }

    pub fn millis(&self) -> u128 {
        for m in self.iter() {
            if let Measurement::DurationMs(ms) = m {
                return *ms;
            }
        }
        0
    }
}