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

Function tokio_postgres

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

Source from the content-addressed store, hash-verified

98}
99
100async fn tokio_postgres(url: &str) -> Result<Duration, Box<dyn Error>> {
101 use tokio::spawn;
102 use tokio_postgres::NoTls;
103 let (client, conn) = tokio_postgres::connect(url, NoTls).await?;
104 spawn(conn);
105 let shared_client = Arc::new(client);
106 let stmt = shared_client
107 .prepare("SELECT * FROM posts WHERE id=$1")
108 .await?;
109 let start = Instant::now();
110 let tasks = (0..1000)
111 .map(|_| {
112 let client = shared_client.clone();
113 let stmt = stmt.clone();
114 spawn(async move {
115 let queries = (0..100).map(|_| client.query_one(&stmt, &[&1i32]));
116 try_join_all(queries).await
117 })
118 })
119 .collect::<Vec<_>>();
120 let results = try_join_all(tasks).await?;
121 let elapsed = start.elapsed();
122 // check
123 for rows in results {
124 for row in rows? {
125 assert_eq!("MIT LICENSE", row.get::<_, &str>(1));
126 assert_eq!(
127 "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.",
128 row.get::<_, &str>(2)
129 );
130 }
131 }
132 Ok(elapsed)
133}

Callers 1

benchmarkFunction · 0.85

Calls 1

connectFunction · 0.85

Tested by 1

benchmarkFunction · 0.68