MCPcopy Create free account
hub / github.com/Hexilee/async-postgres / tokio_runtime

Function tokio_runtime

tests/benchmark.rs:68–98  ·  view source on GitHub ↗
(url: &str)

Source from the content-addressed store, hash-verified

66}
67
68async fn tokio_runtime(url: &str) -> Result<Duration, Box<dyn Error>> {
69 use tokio::spawn;
70 let (client, conn) = async_postgres::connect(url.parse()?).await?;
71 spawn(conn);
72 let shared_client = Arc::new(client);
73 let stmt = shared_client
74 .prepare("SELECT * FROM posts WHERE id=$1")
75 .await?;
76 let start = Instant::now();
77 let tasks = (0..1000).map(|_| {
78 let client = shared_client.clone();
79 let stmt = stmt.clone();
80 spawn(async move {
81 let queries = (0..100).map(|_| client.query_one(&stmt, &[&1i32]));
82 try_join_all(queries).await
83 })
84 });
85 let results = try_join_all(tasks).await?;
86 let elapsed = start.elapsed();
87 // check
88 for rows in results {
89 for row in rows? {
90 assert_eq!("MIT LICENSE", row.get::<_, &str>(1));
91 assert_eq!(
92 "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.",
93 row.get::<_, &str>(2)
94 );
95 }
96 }
97 Ok(elapsed)
98}
99
100async fn tokio_postgres(url: &str) -> Result<Duration, Box<dyn Error>> {
101 use tokio::spawn;

Callers 1

benchmarkFunction · 0.85

Calls 1

connectFunction · 0.85

Tested by 1

benchmarkFunction · 0.68