Skip to content

Commit

Permalink
parser for analyze.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
daveads committed Aug 9, 2024
1 parent 278f83f commit e7ed139
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ graphql/gqlgen/main
tailcall-src

metals.*
parser/target/*
165 changes: 165 additions & 0 deletions parser/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions parser/Cargo.toml
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"
70 changes: 70 additions & 0 deletions parser/src/lib.rs
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
}
}

1 comment on commit e7ed139

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query Server Requests/sec Latency (ms) Relative
1 { posts { id userId title user { id name email }}}
[Tailcall] 29,844.20 3.34 205.19x
[async-graphql] 2,029.94 49.20 13.96x
[Caliban] 1,781.53 55.89 12.25x
[GraphQL JIT] 1,307.22 76.17 8.99x
[Gqlgen] 806.55 123.11 5.55x
[Netflix DGS] 364.33 199.27 2.50x
[Apollo GraphQL] 266.27 368.78 1.83x
[Hasura] 145.45 562.21 1.00x
2 { posts { title }}
[Tailcall] 58,774.50 1.69 68.28x
[Caliban] 10,001.00 10.32 11.62x
[async-graphql] 9,886.59 10.22 11.48x
[Gqlgen] 2,232.18 46.36 2.59x
[Apollo GraphQL] 1,750.92 57.03 2.03x
[Netflix DGS] 1,595.29 70.84 1.85x
[GraphQL JIT] 1,351.12 73.90 1.57x
[Hasura] 860.84 115.97 1.00x
3 { greet }
[Caliban] 70,431.70 1.05 28.33x
[Tailcall] 59,908.60 1.68 24.10x
[Gqlgen] 48,354.20 5.06 19.45x
[async-graphql] 48,262.20 2.22 19.41x
[Netflix DGS] 8,168.54 14.99 3.29x
[Apollo GraphQL] 8,101.04 12.56 3.26x
[GraphQL JIT] 5,122.85 19.49 2.06x
[Hasura] 2,486.24 40.13 1.00x

Please sign in to comment.