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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#[cfg(feature = "yansi")]
pub extern crate yansi;
#[cfg(feature = "yansi")]
use yansi::Color;
use std::fmt;
#[derive(Debug)]
pub struct Apdex {
pub threshold: f64,
pub satisfied: u64,
pub tolerating: u64,
pub frustrated: u64,
}
pub struct ApdexRating<'i>(&'i Apdex);
impl Default for Apdex {
fn default() -> Apdex {
Apdex::new(4.0)
}
}
impl Apdex {
pub fn new(threshold: f64) -> Apdex {
Apdex {
threshold,
satisfied: 0,
tolerating: 0,
frustrated: 0,
}
}
pub fn with_respnse_times(threshold: f64, response_times: impl IntoIterator<Item = Result<f64, ()>>) -> Apdex {
response_times.into_iter().fold(Self::new(threshold), |mut apdex, response_time| {
apdex.insert(response_time);
apdex})
}
pub fn with_hit_rate(threshold: f64, assumed_hit_rate: f64, response_times: impl IntoIterator<Item = Result<f64, ()>>) -> Apdex {
let mut apdex = Self::with_respnse_times(threshold, response_times);
let misses = apdex.total();
let hits = (misses as f64 / (1.0 - assumed_hit_rate) - misses as f64).ceil() as u64;
apdex.satisfied += hits;
apdex
}
pub fn insert(&mut self, response_time: Result<f64, ()>) {
if let Ok(response_time) = response_time {
if response_time <= self.threshold {
self.satisfied += 1;
} else if response_time <= self.threshold * 4.0 {
self.tolerating += 1;
} else {
self.frustrated += 1;
}
} else {
self.frustrated += 1;
}
}
pub fn total(&self) -> u64 {
self.satisfied + self.tolerating + self.frustrated
}
pub fn no_samples(&self) -> bool {
self.total() == 0
}
pub fn small_group(&self) -> bool {
let total = self.total();
total > 0 && total < 100
}
pub fn score(&self) -> Option<f64> {
if self.no_samples() {
None
} else {
Some((self.satisfied as f64 + (self.tolerating as f64 / 2.0)) / self.total() as f64)
}
}
pub fn score_rating(&self) -> ApdexRating {
ApdexRating(&self)
}
pub fn rating_word(&self) -> &'static str {
if let Some(score) = self.score() {
if score >= 0.94 {
"Excellent"
} else if score >= 0.85 {
"Good"
} else if score >= 0.70 {
"Fair"
} else if score >= 0.50 {
"Poor"
} else {
"Unacceptable"
}
} else {
"NoSample"
}
}
#[cfg(feature = "yansi")]
pub fn color(&self) -> Color {
if let Some(score) = self.score() {
if self.small_group() {
return Color::Unset
}
if score >= 0.94 {
Color::Cyan
} else if score >= 0.85 {
Color::Green
} else if score >= 0.70 {
Color::Purple
} else {
Color::Red
}
} else {
return Color::Unset
}
}
fn write_threshold(&self, f: &mut fmt::Formatter) -> fmt::Result {
let low_sample_indicator = if self.small_group() {
"*"
} else {
""
};
if self.threshold < 10.0 {
write!(f, " [{:.1}]{}", self.threshold, low_sample_indicator)
} else {
write!(f, " [{:.0}]{}", self.threshold, low_sample_indicator)
}
}
}
impl fmt::Display for Apdex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(score) = self.score() {
write!(f, "{:.2}", score)
} else {
write!(f, "NS")
}?;
self.write_threshold(f)
}
}
impl<'i> fmt::Display for ApdexRating<'i> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.rating_word())?;
self.0.write_threshold(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn score() {
let apdex = Apdex::with_respnse_times(1.0, [0.0, 0.1, 0.2, 0.5, 1.0, 4.0, 3.0, 2.0, 5.0].iter().cloned().map(Ok));
assert!(apdex.score().unwrap() > 0.71);
assert!(apdex.score().unwrap() < 0.73);
}
#[test]
fn score_errors() {
let apdex = Apdex::with_respnse_times(1.0, [Ok(0.0), Ok(0.1), Ok(0.2), Ok(0.5), Ok(1.0), Ok(4.0), Ok(3.0), Ok(2.0), Err(())].iter().cloned());
assert!(apdex.score().unwrap() > 0.71);
assert!(apdex.score().unwrap() < 0.73);
}
#[test]
fn no_score() {
let apdex = Apdex::default();
assert!(apdex.score().is_none());
}
#[test]
fn uniform_output_no_samples() {
let apdex = Apdex::default();
assert_eq!(format!("{}", apdex), "NS [4.0]");
}
#[test]
fn uniform_output_no_samples_high_t() {
let apdex = Apdex::new(10.0);
assert_eq!(format!("{}", apdex), "NS [10]");
}
#[test]
fn uniform_output_one_small_group() {
let mut apdex = Apdex::default();
apdex.insert(Ok(0.1));
assert_eq!(format!("{}", apdex), "1.00 [4.0]*");
}
#[test]
fn uniform_output_one() {
let mut apdex = Apdex::default();
for _i in 0..100 {
apdex.insert(Ok(0.1));
}
assert_eq!(format!("{}", apdex), "1.00 [4.0]");
}
#[test]
fn uniform_output() {
let mut apdex = Apdex::default();
for _i in 0..100 {
apdex.insert(Ok(0.1));
}
for _i in 0..100 {
apdex.insert(Ok(5.0));
}
assert_eq!(format!("{}", apdex), "0.75 [4.0]");
}
#[test]
fn rating_output_no_samples_high_t() {
let apdex = Apdex::new(10.0);
assert_eq!(format!("{}", apdex.score_rating()), "NoSample [10]");
}
#[test]
fn rating_output_no_samples() {
let apdex = Apdex::default();
assert_eq!(format!("{}", apdex.score_rating()), "NoSample [4.0]");
}
#[test]
fn rating_output_small_group() {
let mut apdex = Apdex::default();
apdex.insert(Ok(0.1));
assert_eq!(format!("{}", apdex.score_rating()), "Excellent [4.0]*");
}
#[test]
fn rating_output() {
let mut apdex = Apdex::default();
for _i in 0..100 {
apdex.insert(Ok(0.1));
}
for _i in 0..100 {
apdex.insert(Ok(5.0));
}
assert_eq!(format!("{}", apdex.score_rating()), "Fair [4.0]");
}
}