Skip to content

Commit 195e3d3

Browse files
authored
Merge pull request #882 from ahmed-mekky/master
add metrics to output
2 parents d07ff6b + 635c080 commit 195e3d3

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

schema.json

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,54 @@
5656
"sizePerSec"
5757
]
5858
},
59+
"metrics": {
60+
"description": "CI friendly statistics",
61+
"type": "object",
62+
"properties": {
63+
"success_rate": {
64+
"description": "The number of success requests / All requests which isn't includes deadline",
65+
"type": "number"
66+
},
67+
"requests_per_sec": {
68+
"description": "The number of requests per second",
69+
"type": "number"
70+
},
71+
"latency_ms": {
72+
"description": "Selected latency statistics in millieseconds",
73+
"type": "object",
74+
"properties": {
75+
"min": {
76+
"type": "number"
77+
},
78+
"mean": {
79+
"type": "number"
80+
},
81+
"p50": {
82+
"type": "number"
83+
},
84+
"p95": {
85+
"type": "number"
86+
},
87+
"p99": {
88+
"type": "number"
89+
}
90+
},
91+
"required": [
92+
"min",
93+
"mean",
94+
"p50",
95+
"p95",
96+
"p99",
97+
"max"
98+
]
99+
}
100+
},
101+
"required": [
102+
"success_rate",
103+
"requests_per_sec",
104+
"latency_ms"
105+
]
106+
},
59107
"responseTimeHistogram": {
60108
"description": "The histogram of response time in seconds. The key is the response time in seconds and the value is the number of requests",
61109
"type": "object",
@@ -433,4 +481,4 @@
433481
"statusCodeDistribution",
434482
"errorDistribution"
435483
]
436-
}
484+
}

src/printer.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ fn print_json<W: Write>(
167167
size_per_sec: f64,
168168
}
169169

170+
#[derive(Serialize)]
171+
struct Metrics {
172+
success_rate: f64,
173+
requests_per_sec: f64,
174+
latency_ms: LatencyMs,
175+
}
176+
177+
#[derive(Serialize)]
178+
#[serde(rename = "latency_ms")]
179+
struct LatencyMs {
180+
min: f64,
181+
mean: f64,
182+
p50: f64,
183+
p95: f64,
184+
p99: f64,
185+
max: f64,
186+
}
187+
170188
#[derive(Serialize)]
171189
struct Triple {
172190
average: f64,
@@ -196,6 +214,7 @@ fn print_json<W: Write>(
196214
#[derive(Serialize)]
197215
struct Result {
198216
summary: Summary,
217+
metrics: Metrics,
199218
#[serde(rename = "responseTimeHistogram")]
200219
response_time_histogram: BTreeMap<String, usize>,
201220
#[serde(rename = "latencyPercentiles")]
@@ -259,7 +278,20 @@ fn print_json<W: Write>(
259278
.percentiles
260279
.into_iter()
261280
.map(|(p, v)| (format!("p{p}"), v))
262-
.collect();
281+
.collect::<BTreeMap<String, f64>>();
282+
283+
let metrics = Metrics {
284+
success_rate: res.success_rate(),
285+
requests_per_sec: res.len() as f64 / total_duration.as_secs_f64(),
286+
latency_ms: LatencyMs {
287+
min: ms(latency_stat.min()),
288+
mean: ms(latency_stat.mean()),
289+
p50: ms(latency_percentiles["p50"]),
290+
p95: ms(latency_percentiles["p95"]),
291+
p99: ms(latency_percentiles["p99"]),
292+
max: ms(latency_stat.max()),
293+
},
294+
};
263295

264296
let first_byte_statistics = res.first_byte_all_statistics();
265297

@@ -389,6 +421,7 @@ fn print_json<W: Write>(
389421
w,
390422
&Result {
391423
summary,
424+
metrics,
392425
response_time_histogram,
393426
latency_percentiles,
394427
first_byte_histogram,
@@ -732,6 +765,11 @@ fn percentiles(values: &mut [f64]) -> BTreeMap<String, f64> {
732765
.collect()
733766
}
734767

768+
// convert s to ms
769+
fn ms(value: f64) -> f64 {
770+
(value * 1000.0 * 1000.0).round() / 1000.0
771+
}
772+
735773
#[cfg(test)]
736774
mod tests {
737775
use float_cmp::assert_approx_eq;

0 commit comments

Comments
 (0)