- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbenchmark.rs
133 lines (127 loc) · 7.46 KB
/
benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use futures::future::try_join_all;
use std::env::var;
use std::error::Error;
use std::sync::Arc;
use std::time::{Duration,Instant};
constTASKS:u128 = 1000;
constQUERIES:u128 = 100;
#[test]
fnbenchmark() -> Result<(),Box<dynError>>{
let queries = TASKS*QUERIES;
let tcp_url = var("TCP_URL")?;
let uds_url = var("UDS_URL")?;
letmut tokio_rr = tokio::runtime::Runtime::new()?;
println!("Benchmark concurrency({}), queries({}):",TASKS, queries);
println!(" - async-postgres on async-std runtime:");
let elapsed = async_std::task::block_on(async_runtime(&tcp_url))?;
println!(" - tcp: {} us/q", elapsed.as_micros() / queries);
let elapsed = async_std::task::block_on(async_runtime(&uds_url))?;
println!(" - uds: {} us/q", elapsed.as_micros() / queries);
println!(" - async-postgres on tokio runtime:");
let elapsed = tokio_rr.block_on(tokio_runtime(&tcp_url))?;
println!(" - tcp: {} us/q", elapsed.as_micros() / queries);
let elapsed = tokio_rr.block_on(tokio_runtime(&uds_url))?;
println!(" - uds: {} us/q", elapsed.as_micros() / queries);
println!(" - tokio-postgres on tokio runtime:");
let elapsed = tokio_rr.block_on(tokio_postgres(&tcp_url))?;
println!(" - tcp: {} us/q", elapsed.as_micros() / queries);
let elapsed = tokio_rr.block_on(tokio_postgres(&uds_url))?;
println!(" - uds: {} us/q", elapsed.as_micros() / queries);
Ok(())
}
asyncfnasync_runtime(url:&str) -> Result<Duration,Box<dynError>>{
use async_std::task::spawn;
let(client, conn) = async_postgres::connect(url.parse()?).await?;
spawn(conn);
let shared_client = Arc::new(client);
let stmt = shared_client
.prepare("SELECT * FROM posts WHERE id=$1")
.await?;
let start = Instant::now();
let tasks = (0..TASKS).map(|_| {
let client = shared_client.clone();
let stmt = stmt.clone();
spawn(asyncmove{
let queries = (0..QUERIES).map(|_| client.query_one(&stmt,&[&1i32]));
try_join_all(queries).await
})
});
let results = try_join_all(tasks).await?;
let elapsed = start.elapsed();
// check
for rows in results {
for row in rows {
assert_eq!("MIT LICENSE", row.get::<_,&str>(1));
assert_eq!(
"Permission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.",
row.get::<_,&str>(2)
);
}
}
Ok(elapsed)
}
asyncfntokio_runtime(url:&str) -> Result<Duration,Box<dynError>>{
use tokio::spawn;
let(client, conn) = async_postgres::connect(url.parse()?).await?;
spawn(conn);
let shared_client = Arc::new(client);
let stmt = shared_client
.prepare("SELECT * FROM posts WHERE id=$1")
.await?;
let start = Instant::now();
let tasks = (0..1000).map(|_| {
let client = shared_client.clone();
let stmt = stmt.clone();
spawn(asyncmove{
let queries = (0..100).map(|_| client.query_one(&stmt,&[&1i32]));
try_join_all(queries).await
})
});
let results = try_join_all(tasks).await?;
let elapsed = start.elapsed();
// check
for rows in results {
for row in rows? {
assert_eq!("MIT LICENSE", row.get::<_,&str>(1));
assert_eq!(
"Permission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.",
row.get::<_,&str>(2)
);
}
}
Ok(elapsed)
}
asyncfntokio_postgres(url:&str) -> Result<Duration,Box<dynError>>{
use tokio::spawn;
use tokio_postgres::NoTls;
let(client, conn) = tokio_postgres::connect(url,NoTls).await?;
spawn(conn);
let shared_client = Arc::new(client);
let stmt = shared_client
.prepare("SELECT * FROM posts WHERE id=$1")
.await?;
let start = Instant::now();
let tasks = (0..1000)
.map(|_| {
let client = shared_client.clone();
let stmt = stmt.clone();
spawn(asyncmove{
let queries = (0..100).map(|_| client.query_one(&stmt,&[&1i32]));
try_join_all(queries).await
})
})
.collect::<Vec<_>>();
let results = try_join_all(tasks).await?;
let elapsed = start.elapsed();
// check
for rows in results {
for row in rows? {
assert_eq!("MIT LICENSE", row.get::<_,&str>(1));
assert_eq!(
"Permission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.",
row.get::<_,&str>(2)
);
}
}
Ok(elapsed)
}