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
use crate::measurement::{attribute::Efficiency, marker::Counter, thresholds::Thresholds, Status};

use std::fmt;
#[derive(Clone)]
pub struct EfficiencyBenchmarkDef {
    name: String,
    thresholds: Option<Thresholds<Efficiency>>,
    max: Counter,
}

impl EfficiencyBenchmarkDef {
    pub fn new(name: String) -> Self {
        EfficiencyBenchmarkDef {
            name,
            thresholds: None,
            max: 0u32.into(),
        }
    }

    pub fn name(&self) -> String {
        self.name.clone()
    }

    pub fn target(&mut self, target: u32) -> &mut Self {
        self.max = target.into();
        self.thresholds = Some(Thresholds::<Efficiency>::new_efficiency(target));
        self
    }

    pub fn no_target(&mut self) -> &mut Self {
        self.thresholds = None;
        self
    }

    pub fn max(&self) -> Counter {
        self.max
    }

    pub fn thresholds(&self) -> Option<&Thresholds<Efficiency>> {
        self.thresholds.as_ref()
    }

    pub fn start(&self) -> EfficiencyBenchmarkRun {
        EfficiencyBenchmarkRun {
            definition: self.clone(),
            start_marker: Counter::new(),
            current_marker: Counter::new(),
        }
    }
}

pub struct EfficiencyBenchmarkRun {
    definition: EfficiencyBenchmarkDef,
    start_marker: Counter,
    current_marker: Counter,
}

impl EfficiencyBenchmarkRun {
    pub fn increment(&mut self) -> &mut Self {
        self.increment_by(1)
    }

    pub fn increment_by(&mut self, increment: u32) -> &mut Self {
        let counter: u32 = self.current_marker.into();
        self.current_marker = (counter + increment).into();
        self
    }

    pub fn exception(&self, info: String) -> EfficiencyBenchmarkFinish {
        println!("Test finished prematurely, due to: {}", info);
        self.stop()
    }

    pub fn stop(&self) -> EfficiencyBenchmarkFinish {
        match self.definition.thresholds() {
            Some(_thresholds) => EfficiencyBenchmarkFinish {
                definition: self.definition.clone(),
                efficiency: Efficiency::new(
                    (self.current_marker - self.start_marker).into(),
                    self.definition.max().into(),
                ),
            },
            None => EfficiencyBenchmarkFinish {
                definition: self.definition.clone(),
                efficiency: Efficiency::new(
                    (self.current_marker - self.start_marker).into(),
                    self.current_marker.into(),
                ),
            },
        }
    }
}

pub struct EfficiencyBenchmarkFinish {
    definition: EfficiencyBenchmarkDef,
    efficiency: Efficiency,
}

impl EfficiencyBenchmarkFinish {
    pub fn print(&self) {
        println!("{}", &self);
    }

    pub fn efficiency(&self) -> Efficiency {
        self.efficiency.clone()
    }

    pub fn definition(&self) -> EfficiencyBenchmarkDef {
        self.definition.clone()
    }

    pub fn status(&self) -> Status {
        self.efficiency
            .against(self.definition.thresholds().unwrap())
    }
}

impl fmt::Display for EfficiencyBenchmarkFinish {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.definition.thresholds() {
            Some(thresholds) => write!(
                f,
                "Measurement: {}. Result: {}. Actual: {} Thresholds: {}",
                self.definition.name(),
                self.efficiency.against(thresholds),
                self.efficiency,
                thresholds,
            ),
            None => write!(
                f,
                "Measurement: {}. Value: {}",
                self.definition.name(),
                self.efficiency
            ),
        }
    }
}