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
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Strategy {
    Duration(std::time::Duration),
    Overall(u32),
    PerThread(u32),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Monitor {
    Standard(u64),
    Progress(u64),
    Disabled(u64),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Configuration {
    fetch_limit: Option<usize>,
    monitor: Monitor,
    shutdown_grace_period: Duration,
    status_pace: Duration,
    step_delay: Duration,
    strategy: Strategy,
    thread_no: usize,
}

impl Configuration {
    pub fn fetch_limit(&self) -> Option<usize> {
        self.fetch_limit
    }

    pub fn monitor(&self) -> &Monitor {
        &self.monitor
    }

    pub fn shutdown_grace_period(&self) -> Duration {
        self.shutdown_grace_period
    }
    pub fn status_pace(&self) -> Duration {
        self.status_pace
    }

    pub fn step_delay(&self) -> Duration {
        self.step_delay
    }

    pub fn strategy(&self) -> &Strategy {
        &self.strategy
    }

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

    pub fn total_votes(&self) -> u32 {
        match self.strategy() {
            Strategy::Duration(duration) => {
                (duration.as_millis() / self.step_delay.as_millis()) as u32
            }
            Strategy::PerThread(per_thread) => self.thread_no() as u32 * per_thread,
            Strategy::Overall(overall) => *overall,
        }
    }
}

pub struct ConfigurationBuilder {
    fetch_limit: Option<usize>,
    monitor: Monitor,
    shutdown_grace_period: Duration,
    status_pace: Duration,
    step_delay: Duration,
    strategy: Strategy,
    thread_no: usize,
}

impl ConfigurationBuilder {
    pub fn duration(duration: Duration) -> Self {
        Self {
            fetch_limit: None,
            monitor: Monitor::Disabled(100),
            shutdown_grace_period: Duration::ZERO,
            status_pace: Duration::from_secs(1),
            step_delay: Duration::from_millis(100),
            strategy: Strategy::Duration(duration),
            thread_no: 1,
        }
    }

    pub fn overall_requests(n_requests: u32) -> Self {
        Self {
            fetch_limit: None,
            monitor: Monitor::Disabled(100),
            shutdown_grace_period: Duration::ZERO,
            status_pace: Duration::from_secs(1),
            step_delay: Duration::from_millis(100),
            strategy: Strategy::Overall(n_requests),
            thread_no: 1,
        }
    }

    pub fn requests_per_thread(n_requests: u32) -> Self {
        Self {
            fetch_limit: None,
            monitor: Monitor::Disabled(100),
            shutdown_grace_period: Duration::ZERO,
            status_pace: Duration::from_secs(1),
            step_delay: Duration::from_millis(100),
            strategy: Strategy::PerThread(n_requests),
            thread_no: 1,
        }
    }

    pub fn fetch_limit(self, fetch_limit: usize) -> Self {
        Self {
            fetch_limit: Some(fetch_limit),
            ..self
        }
    }

    pub fn monitor(self, monitor: Monitor) -> Self {
        Self { monitor, ..self }
    }

    pub fn shutdown_grace_period(self, shutdown_grace_period: Duration) -> Self {
        Self {
            shutdown_grace_period,
            ..self
        }
    }

    pub fn status_pace(self, status_pace: Duration) -> Self {
        Self {
            status_pace,
            ..self
        }
    }

    pub fn step_delay(self, step_delay: Duration) -> Self {
        Self { step_delay, ..self }
    }

    pub fn thread_no(self, thread_no: usize) -> Self {
        Self { thread_no, ..self }
    }

    pub fn build(self) -> Configuration {
        Configuration {
            fetch_limit: self.fetch_limit,
            monitor: self.monitor,
            shutdown_grace_period: self.shutdown_grace_period,
            status_pace: self.status_pace,
            step_delay: self.step_delay,
            strategy: self.strategy,
            thread_no: self.thread_no,
        }
    }
}