49 lines
2.0 KiB
Rust

use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::storages::traits::BenchmarkRepository;
use async_trait::async_trait;
pub struct PostgresBenchmarkRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl BenchmarkRepository for PostgresBenchmarkRepository {
async fn benchmark(&self) -> Result<i64, String> {
self.connection.execute("DELETE FROM Rent", &[]).await.unwrap();
self.connection.execute("DELETE FROM Car", &[]).await.unwrap();
self.connection.execute("DELETE FROM Owner", &[]).await.unwrap();
self.connection.execute("DELETE FROM Client", &[]).await.unwrap();
let start_time = chrono::Utc::now().naive_local();
for n in 1..2001 {
let str = format!("INSERT INTO Owner(name, surname, middlename, phone) VALUES ('Иван{0}', 'Иванов{0}', 'Иванович{0}', {0}) RETURNING id", n);
let owner_id: i32 = self.connection.query(
&str,
&[]
).await.unwrap().get(0).unwrap().get(0);
let client_id: i32 = self.connection.query(
&str.replace("Owner", "Client").replace("Иван", "Петр"),
&[]
).await.unwrap().get(0).unwrap().get(0);
let str = format!("INSERT INTO Car(Brand, Model, Price, owner_id, car_station_id) VALUES ('Лада{0}', 'Гранта{0}', {0}.00, {1}, 2) RETURNING id", n, owner_id);
let car_id: i32 = self.connection.query(
&str,
&[]
).await.unwrap().get(0).unwrap().get(0);
let str = format!("INSERT INTO Rent(car_id, client_id, start_time, time_amount) VALUES ({1}, {2}, now()::timestamp, {0}) RETURNING id", n, car_id, client_id);
let _: i32 = self.connection.query(
&str,
&[]
).await.unwrap().get(0).unwrap().get(0);
}
let elapsed = chrono::Utc::now().naive_local() - start_time;
Ok(elapsed.num_microseconds().unwrap())
}
}