Skip to content

Commit 2922c1b

Browse files
committed
Moved benchmark from side repo
1 parent b381305 commit 2922c1b

File tree

7 files changed

+148
-3
lines changed

7 files changed

+148
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ Cargo.lock
33
.idea
44
.DS_Store
55
node_modules
6+
7+
benchmark/target

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ members = [
6464
"async-graphql-warp",
6565
"async-graphql-tide",
6666
# "async-graphql-lambda",
67-
]
67+
"benchmark",
68+
]

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ To see how you would create a Relay-compliant server using async-graphql, warp,
5454

5555
## Benchmark
5656

57+
Ensure that there is no CPU-heavy process in background!
58+
5759
```shell script
58-
git clone https://github.com/async-graphql/benchmark
59-
cargo run --release
60+
cd benchmark
61+
cargo bench
6062
```
6163

64+
Now HTML report is available at `benchmark/target/criterion/report`
65+
6266
## Features
6367

6468
* Fully support async/await

benchmark/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "graphql-benchmark"
3+
version = "0.1.0"
4+
authors = ["sunli <[email protected]>"]
5+
edition = "2018"
6+
7+
[dev-dependencies]
8+
criterion = "0.3"
9+
simple = { path = "simple" }
10+
11+
[[bench]]
12+
name = "simple"
13+
harness = false

benchmark/benches/simple.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use criterion::{criterion_group, criterion_main, Criterion, black_box};
2+
use simple::{Q, run, parse, serialize, GQLResponse};
3+
4+
pub fn criterion_benchmark(c: &mut Criterion) {
5+
c.bench_function("run", |b| b.iter(|| run(black_box(Q))));
6+
c.bench_function("parse", |b| b.iter(|| parse(black_box(Q))));
7+
let res = GQLResponse(run(Q));
8+
c.bench_function("serialize", |b| b.iter(|| serialize(black_box(&res))));
9+
}
10+
11+
criterion_group!(benches, criterion_benchmark);
12+
criterion_main!(benches);

benchmark/simple/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "simple"
3+
version = "0.1.0"
4+
authors = ["Ivan Plesskih <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
async-graphql = { path = "../.." }
9+
async-std = { version = "1.5.0", features = ["attributes"] }
10+
futures = "0.3.4"
11+
serde_json = "*"
12+
lazy_static = "*"
13+
async-graphql-parser = { path = "../../async-graphql-parser" }

benchmark/simple/src/lib.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use async_graphql::*;
2+
pub use http::GQLResponse;
3+
use async_graphql_parser::{parse_query, query::Document};
4+
use async_std::task;
5+
6+
pub struct QueryRoot;
7+
8+
#[Object]
9+
impl QueryRoot {
10+
#[field]
11+
async fn value_i32(&self) -> i32 {
12+
999
13+
}
14+
15+
#[field]
16+
async fn obj(&self) -> MyObj {
17+
MyObj
18+
}
19+
}
20+
21+
pub struct MyObj;
22+
23+
#[Object]
24+
impl MyObj {
25+
#[field]
26+
async fn value_i32(&self) -> i32 {
27+
999
28+
}
29+
30+
#[field]
31+
async fn value_list(&self) -> &[i32] {
32+
&[1, 2, 3, 4, 5, 6, 7, 8, 9]
33+
}
34+
35+
#[field]
36+
async fn obj(&self) -> MyObj {
37+
MyObj
38+
}
39+
}
40+
41+
pub const Q: &str = r#"{
42+
valueI32 obj {
43+
valueI32 valueList obj {
44+
valueI32 valueList obj {
45+
valueI32 valueList obj {
46+
valueI32 valueList obj {
47+
valueI32 valueList obj {
48+
valueI32 valueList obj {
49+
valueI32 valueList obj {
50+
valueI32 valueList obj {
51+
valueI32 valueList obj {
52+
valueI32 valueList obj {
53+
valueI32 valueList obj {
54+
valueI32 valueList obj {
55+
valueI32 valueList obj {
56+
valueI32 valueList obj {
57+
valueI32 valueList
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}"#;
74+
75+
lazy_static::lazy_static! {
76+
static ref S: Schema<QueryRoot, EmptyMutation, EmptySubscription> = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
77+
// static ref D: Document = parse_query(Q).unwrap();
78+
}
79+
80+
pub fn run(q: &str) -> Result<QueryResponse> {
81+
task::block_on(async {
82+
S.execute(q).await
83+
})
84+
}
85+
86+
pub fn parse(q: &str) -> Document {
87+
parse_query(q).unwrap()
88+
}
89+
90+
// pub fn validate() {
91+
// check_rules(&S.env.registry, &D, S.validation_mode).unwrap();
92+
// }
93+
//
94+
// pub fn resolve() {
95+
// do_resolve(...).unwrap();
96+
// }
97+
98+
pub fn serialize(r: &GQLResponse) -> String {
99+
serde_json::to_string(&r).unwrap()
100+
}

0 commit comments

Comments
 (0)