Update to latest tokio
This commit is contained in:
parent
d22fab76c8
commit
c42c71b02d
9 changed files with 49 additions and 50 deletions
integration_tests/async_await
juniper
juniper_benchmarks
juniper_codegen/src/util
juniper_rocket_async
|
@ -10,4 +10,4 @@ async = []
|
||||||
[dependencies]
|
[dependencies]
|
||||||
juniper = { path = "../../juniper", features = ["async"] }
|
juniper = { path = "../../juniper", features = ["async"] }
|
||||||
futures = "=0.3.1"
|
futures = "=0.3.1"
|
||||||
tokio = "=0.2.0-alpha.6"
|
tokio = { version = "0.2", features = ["rt-core", "time", "macros"] }
|
|
@ -34,8 +34,7 @@ impl User {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delayed() -> bool {
|
async fn delayed() -> bool {
|
||||||
let when = tokio::clock::now() + std::time::Duration::from_millis(100);
|
tokio::time::delay_for(std::time::Duration::from_millis(100)).await;
|
||||||
tokio::timer::delay(when).await;
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,8 +60,7 @@ impl Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delayed() -> bool {
|
async fn delayed() -> bool {
|
||||||
let when = tokio::clock::now() + std::time::Duration::from_millis(100);
|
tokio::time::delay_for(std::time::Duration::from_millis(100)).await;
|
||||||
tokio::timer::delay(when).await;
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,14 +70,8 @@ struct Mutation;
|
||||||
#[juniper::graphql_object]
|
#[juniper::graphql_object]
|
||||||
impl Mutation {}
|
impl Mutation {}
|
||||||
|
|
||||||
fn run<O>(f: impl std::future::Future<Output = O>) -> O {
|
#[tokio::test]
|
||||||
tokio::runtime::current_thread::Runtime::new()
|
async fn async_simple() {
|
||||||
.unwrap()
|
|
||||||
.block_on(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn async_simple() {
|
|
||||||
let schema = RootNode::new(Query, Mutation);
|
let schema = RootNode::new(Query, Mutation);
|
||||||
let doc = r#"
|
let doc = r#"
|
||||||
query {
|
query {
|
||||||
|
@ -95,9 +87,9 @@ fn async_simple() {
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let vars = Default::default();
|
let vars = Default::default();
|
||||||
let f = juniper::execute_async(doc, None, &schema, &vars, &());
|
let (res, errs) = juniper::execute_async(doc, None, &schema, &vars, &())
|
||||||
|
.await
|
||||||
let (res, errs) = run(f).unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(errs.is_empty());
|
assert!(errs.is_empty());
|
||||||
|
|
||||||
|
|
|
@ -48,4 +48,4 @@ uuid = { version = ">= 0.7, < 0.8", optional = true }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bencher = "0.1.2"
|
bencher = "0.1.2"
|
||||||
serde_json = { version = "1.0.2" }
|
serde_json = { version = "1.0.2" }
|
||||||
tokio = "=0.2.0-alpha.6"
|
tokio = { version = "0.2", features = ["macros", "rt-core", "time"] }
|
||||||
|
|
|
@ -35,8 +35,7 @@ impl User {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delayed() -> bool {
|
async fn delayed() -> bool {
|
||||||
let when = tokio::clock::now() + std::time::Duration::from_millis(100);
|
tokio::time::delay_for(std::time::Duration::from_millis(100)).await;
|
||||||
tokio::timer::delay(when).await;
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,8 +61,7 @@ impl Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delayed() -> bool {
|
async fn delayed() -> bool {
|
||||||
let when = tokio::clock::now() + std::time::Duration::from_millis(100);
|
tokio::time::delay_for(std::time::Duration::from_millis(100)).await;
|
||||||
tokio::timer::delay(when).await;
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,14 +71,8 @@ struct Mutation;
|
||||||
#[crate::graphql_object_internal]
|
#[crate::graphql_object_internal]
|
||||||
impl Mutation {}
|
impl Mutation {}
|
||||||
|
|
||||||
fn run<O>(f: impl std::future::Future<Output = O>) -> O {
|
#[tokio::test]
|
||||||
tokio::runtime::current_thread::Runtime::new()
|
async fn async_simple() {
|
||||||
.unwrap()
|
|
||||||
.block_on(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn async_simple() {
|
|
||||||
let schema = RootNode::new(Query, Mutation);
|
let schema = RootNode::new(Query, Mutation);
|
||||||
let doc = r#"
|
let doc = r#"
|
||||||
query {
|
query {
|
||||||
|
@ -94,9 +86,9 @@ fn async_simple() {
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let vars = Default::default();
|
let vars = Default::default();
|
||||||
let f = crate::execute_async(doc, None, &schema, &vars, &());
|
let (res, errs) = crate::execute_async(doc, None, &schema, &vars, &())
|
||||||
|
.await
|
||||||
let (res, errs) = run(f).unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(errs.is_empty());
|
assert!(errs.is_empty());
|
||||||
|
|
||||||
|
|
|
@ -5,18 +5,17 @@ authors = ["Christoph Herzog <chris@theduke.at>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "benchmark"
|
name = "benchmark"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
async = ["juniper/async", "futures"]
|
async = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
juniper = { path = "../juniper" }
|
juniper = { path = "../juniper", features = ["async"] }
|
||||||
futures = { version = "=0.3.1", optional = true }
|
futures = "=0.3.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = "0.2.11"
|
criterion = "0.2.11"
|
||||||
tokio = "=0.2.0-alpha.6"
|
tokio = { version = "0.2.0", features = ["rt-threaded", "rt-core"] }
|
||||||
|
|
|
@ -5,10 +5,10 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion, Parameter
|
||||||
use juniper::{graphql_value, InputValue, ToInputValue, Value};
|
use juniper::{graphql_value, InputValue, ToInputValue, Value};
|
||||||
use juniper_benchmarks as j;
|
use juniper_benchmarks as j;
|
||||||
|
|
||||||
fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) {
|
||||||
const QUERY: &'static str = r#"
|
const ASYNC_QUERY: &'static str = r#"
|
||||||
query Query($id: Int) {
|
query Query($id: Int) {
|
||||||
user(id: $id) {
|
users_async_instant(ids: [$id]!) {
|
||||||
id
|
id
|
||||||
kind
|
kind
|
||||||
username
|
username
|
||||||
|
@ -17,8 +17,19 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
|
const SYNC_QUERY: &'static str = r#"
|
||||||
|
query Query($id: Int) {
|
||||||
|
users_sync_instant(ids: [$id]!) {
|
||||||
|
id
|
||||||
|
kind
|
||||||
|
username
|
||||||
|
email
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
c.bench(
|
c.bench(
|
||||||
"Sync vs Async - Single User Flat - Instant",
|
"Sync vs Async - Users Flat - Instant",
|
||||||
ParameterizedBenchmark::new(
|
ParameterizedBenchmark::new(
|
||||||
"Sync",
|
"Sync",
|
||||||
|b, count| {
|
|b, count| {
|
||||||
|
@ -28,7 +39,7 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
let ids = InputValue::list(ids);
|
let ids = InputValue::list(ids);
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
j::execute(
|
j::execute(
|
||||||
QUERY,
|
SYNC_QUERY,
|
||||||
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -36,7 +47,10 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
vec![1, 10],
|
vec![1, 10],
|
||||||
)
|
)
|
||||||
.with_function("Async - Single Thread", |b, count| {
|
.with_function("Async - Single Thread", |b, count| {
|
||||||
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
|
let mut rt = tokio::runtime::Builder::new()
|
||||||
|
.basic_scheduler()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let ids = (0..*count)
|
let ids = (0..*count)
|
||||||
.map(|x| InputValue::scalar(x as i32))
|
.map(|x| InputValue::scalar(x as i32))
|
||||||
|
@ -45,14 +59,17 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let f = j::execute_async(
|
let f = j::execute_async(
|
||||||
QUERY,
|
ASYNC_QUERY,
|
||||||
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
||||||
);
|
);
|
||||||
rt.block_on(f)
|
rt.block_on(f)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.with_function("Async - Threadpool", |b, count| {
|
.with_function("Async - Threadpool", |b, count| {
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
let mut rt = tokio::runtime::Builder::new()
|
||||||
|
.threaded_scheduler()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let ids = (0..*count)
|
let ids = (0..*count)
|
||||||
.map(|x| InputValue::scalar(x as i32))
|
.map(|x| InputValue::scalar(x as i32))
|
||||||
|
@ -61,7 +78,7 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let f = j::execute_async(
|
let f = j::execute_async(
|
||||||
QUERY,
|
ASYNC_QUERY,
|
||||||
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
|
||||||
);
|
);
|
||||||
rt.block_on(f)
|
rt.block_on(f)
|
||||||
|
@ -70,5 +87,5 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, bench_sync_vs_async_single_user_flat_instant);
|
criterion_group!(benches, bench_sync_vs_async_users_flat_instant);
|
||||||
criterion_main!(benches);
|
criterion_main!(benches);
|
||||||
|
|
|
@ -105,7 +105,6 @@ pub fn execute(query: &str, vars: Variables) -> QueryResult {
|
||||||
juniper::execute(query, None, &root, &vars, &ctx).map_err(|e| format!("{:?}", e))
|
juniper::execute(query, None, &root, &vars, &ctx).map_err(|e| format!("{:?}", e))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
|
||||||
pub async fn execute_async(query: &str, vars: Variables) -> QueryResult {
|
pub async fn execute_async(query: &str, vars: Variables) -> QueryResult {
|
||||||
let root = new_schema();
|
let root = new_schema();
|
||||||
let ctx = Context::new();
|
let ctx = Context::new();
|
||||||
|
|
|
@ -930,7 +930,7 @@ impl GraphQLTypeDefiniton {
|
||||||
) -> #juniper_crate_name::BoxFuture<'b, #juniper_crate_name::ExecutionResult<#scalar>>
|
) -> #juniper_crate_name::BoxFuture<'b, #juniper_crate_name::ExecutionResult<#scalar>>
|
||||||
where #scalar: Send + Sync,
|
where #scalar: Send + Sync,
|
||||||
{
|
{
|
||||||
//use futures::future;
|
use futures::future;
|
||||||
use #juniper_crate_name::GraphQLType;
|
use #juniper_crate_name::GraphQLType;
|
||||||
match field {
|
match field {
|
||||||
#( #resolve_matches_async )*
|
#( #resolve_matches_async )*
|
||||||
|
|
|
@ -22,7 +22,7 @@ juniper = { version = "0.14.1", default-features = false, path = "../juniper"}
|
||||||
|
|
||||||
futures = { version = "=0.3.1", features = ["compat"] }
|
futures = { version = "=0.3.1", features = ["compat"] }
|
||||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" }
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" }
|
||||||
tokio = "=0.2.0-alpha.6"
|
tokio = "0.2"
|
||||||
|
|
||||||
[dev-dependencies.juniper]
|
[dev-dependencies.juniper]
|
||||||
version = "0.14.1"
|
version = "0.14.1"
|
||||||
|
|
Loading…
Add table
Reference in a new issue