-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ graphql/gqlgen/main | |
tailcall-src | ||
|
||
metals.* | ||
parser/target/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "parser" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
wasm-bindgen = { version = "0.2.92" } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde-wasm-bindgen = "0.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use wasm_bindgen::prelude::*; | ||
use serde::{Serialize, Deserialize}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct ServerMetrics { | ||
req_sec: f64, | ||
latency: f64, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn parse_server_metrics(servers: Vec<String>, result_contents: Vec<String>) -> JsValue { | ||
let server_metrics = calculate_server_metrics(&servers, &result_contents); | ||
serde_wasm_bindgen::to_value(&server_metrics).unwrap() | ||
} | ||
|
||
fn calculate_server_metrics(servers: &[String], result_contents: &[String]) -> HashMap<String, ServerMetrics> { | ||
let mut server_metrics = HashMap::new(); | ||
|
||
for (idx, server) in servers.iter().enumerate() { | ||
let start_idx = idx * 3; | ||
let mut req_sec_vals = Vec::new(); | ||
let mut latency_vals = Vec::new(); | ||
|
||
for j in 0..3 { | ||
let input_idx = start_idx + j; | ||
if input_idx < result_contents.len() { | ||
if let Some(req_sec) = parse_metric(&result_contents[input_idx], "Requests/sec") { | ||
req_sec_vals.push(req_sec); | ||
} | ||
if let Some(latency) = parse_metric(&result_contents[input_idx], "Latency") { | ||
latency_vals.push(latency); | ||
} | ||
} | ||
} | ||
|
||
server_metrics.insert(server.clone(), ServerMetrics { | ||
req_sec: calculate_average(&req_sec_vals), | ||
latency: calculate_average(&latency_vals), | ||
}); | ||
} | ||
|
||
server_metrics | ||
} | ||
|
||
fn parse_metric(input: &str, metric: &str) -> Option<f64> { | ||
let lines: Vec<&str> = input.lines().collect(); | ||
let metric_line = lines.iter().find(|line| line.trim().starts_with(metric))?; | ||
|
||
let parts: Vec<&str> = metric_line.split_whitespace().collect(); | ||
match metric { | ||
"Latency" => { | ||
// Assume format: "Latency 13.08ms 5.37ms 153.52ms 88.69%" | ||
parts.get(1).and_then(|val| val.trim_end_matches("ms").parse::<f64>().ok()) | ||
}, | ||
"Requests/sec" => { | ||
// Assume format: "Requests/sec: 7746.27" | ||
parts.get(1).and_then(|val| val.parse::<f64>().ok()) | ||
}, | ||
_ => None, | ||
} | ||
} | ||
|
||
fn calculate_average(values: &[f64]) -> f64 { | ||
if values.is_empty() { | ||
0.0 | ||
} else { | ||
values.iter().sum::<f64>() / values.len() as f64 | ||
} | ||
} |
e7ed139
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ posts { id userId title user { id name email }}}
29,844.20
3.34
205.19x
2,029.94
49.20
13.96x
1,781.53
55.89
12.25x
1,307.22
76.17
8.99x
806.55
123.11
5.55x
364.33
199.27
2.50x
266.27
368.78
1.83x
145.45
562.21
1.00x
{ posts { title }}
58,774.50
1.69
68.28x
10,001.00
10.32
11.62x
9,886.59
10.22
11.48x
2,232.18
46.36
2.59x
1,750.92
57.03
2.03x
1,595.29
70.84
1.85x
1,351.12
73.90
1.57x
860.84
115.97
1.00x
{ greet }
70,431.70
1.05
28.33x
59,908.60
1.68
24.10x
48,354.20
5.06
19.45x
48,262.20
2.22
19.41x
8,168.54
14.99
3.29x
8,101.04
12.56
3.26x
5,122.85
19.49
2.06x
2,486.24
40.13
1.00x