summaryrefslogtreecommitdiff
path: root/src/performance.rs
blob: 29c2328b07faecdd1b7609681a5aa80791599862 (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
//! Performance measurements from an Obnam run.

use crate::accumulated_time::AccumulatedTime;
use log::info;

/// The kinds of clocks we have.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Clock {
    /// The complete runtime of the program.
    RunTime,

    /// Time spent downloading previous backup generations.
    GenerationDownload,

    /// Time spent uploading backup generations.
    GenerationUpload,
}

/// Collected measurements from this Obnam run.
#[derive(Debug)]
pub struct Performance {
    args: Vec<String>,
    time: AccumulatedTime<Clock>,
    live_files: u64,
    files_backed_up: u64,
    chunks_uploaded: u64,
    chunks_reused: u64,
}

impl Default for Performance {
    fn default() -> Self {
        Self {
            args: std::env::args().collect(),
            time: AccumulatedTime::<Clock>::new(),
            live_files: 0,
            files_backed_up: 0,
            chunks_reused: 0,
            chunks_uploaded: 0,
        }
    }
}

impl Performance {
    /// Log all performance measurements to the log file.
    pub fn log(&self) {
        info!("Performance measurements for this Obnam run");
        for (i, arg) in self.args.iter().enumerate() {
            info!("argv[{}]={:?}", i, arg);
        }
        info!("Live files found: {}", self.live_files);
        info!("Files backed up: {}", self.files_backed_up);
        info!("Chunks uploaded: {}", self.chunks_uploaded);
        info!("Chunks reused: {}", self.chunks_reused);
        info!(
            "Downloading previous generation (seconds): {}",
            self.time.secs(Clock::GenerationDownload)
        );
        info!(
            "Uploading new generation (seconds): {}",
            self.time.secs(Clock::GenerationUpload)
        );
        info!(
            "Complete run time (seconds): {}",
            self.time.secs(Clock::RunTime)
        );
    }

    /// Start a specific clock.
    pub fn start(&mut self, clock: Clock) {
        self.time.start(clock)
    }

    /// Stop a specific clock.
    pub fn stop(&mut self, clock: Clock) {
        self.time.stop(clock)
    }

    /// Increment number of live files.
    pub fn found_live_files(&mut self, n: u64) {
        self.live_files += n;
    }

    /// Increment number of files backed up this run.
    pub fn back_up_file(&mut self) {
        self.files_backed_up += 1;
    }

    /// Increment number of reused chunks.
    pub fn reuse_chunk(&mut self) {
        self.chunks_reused += 1;
    }

    /// Increment number of uploaded chunks.
    pub fn upload_chunk(&mut self) {
        self.chunks_uploaded += 1;
    }
}