68 lines
2.3 KiB
Rust
68 lines
2.3 KiB
Rust
|
use chrono::Utc;
|
||
|
use mongodb::Database;
|
||
|
use mongodb::bson::oid::ObjectId;
|
||
|
use mongodb::bson::{doc, Document, Bson};
|
||
|
use futures::stream::{TryStreamExt, StreamExt};
|
||
|
use mongodb::options::{FindOptions, AggregateOptions};
|
||
|
use rust_decimal::Decimal;
|
||
|
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
|
||
|
use std::default;
|
||
|
use std::sync::Arc;
|
||
|
use async_trait::async_trait;
|
||
|
|
||
|
use crate::storages::traits::BenchmarkRepository;
|
||
|
|
||
|
|
||
|
pub struct MongoBenchmarkRepository {
|
||
|
pub database: Arc<Database>
|
||
|
}
|
||
|
|
||
|
#[async_trait]
|
||
|
impl BenchmarkRepository for MongoBenchmarkRepository {
|
||
|
async fn benchmark(&self) -> Result<i64, String> {
|
||
|
let collection = self.database.collection::<Document>("clients");
|
||
|
|
||
|
collection.delete_many(doc! {}, None).await.unwrap();
|
||
|
|
||
|
let start_time = Utc::now().naive_local();
|
||
|
|
||
|
for i in 1..2001 {
|
||
|
let car_id = ObjectId::new();
|
||
|
let client_id = ObjectId::new();
|
||
|
let owner_id = ObjectId::new();
|
||
|
let rent_id = ObjectId::new();
|
||
|
|
||
|
collection.insert_one(doc! {
|
||
|
"_id": client_id,
|
||
|
"name": format!("Иван{}", i),
|
||
|
"surname": format!("Иванов{}", i),
|
||
|
"middlename": format!("Иванович{}", i),
|
||
|
"phone": i.to_string(),
|
||
|
"rents": [{
|
||
|
"_id": rent_id,
|
||
|
"start_time": chrono::Utc::now(),
|
||
|
"time_amount": i,
|
||
|
"car_id": car_id.clone()
|
||
|
}]
|
||
|
}, None).await.unwrap();
|
||
|
|
||
|
collection.insert_one(doc! {
|
||
|
"_id": owner_id,
|
||
|
"name": format!("Петр{}", i),
|
||
|
"surname": format!("Петров{}", i),
|
||
|
"middlename": format!("Петрович{}", i),
|
||
|
"phone": i.to_string(),
|
||
|
"rents": [],
|
||
|
"cars": [{
|
||
|
"_id": car_id,
|
||
|
"brand": format!("Лада{}", i),
|
||
|
"model": format!("Гранта{}", i),
|
||
|
"price": i as f64,
|
||
|
"car_station_id": ObjectId::parse_str("6466ca4c26b3b0b31d9a45bc").unwrap()
|
||
|
}]
|
||
|
}, None).await.unwrap();
|
||
|
}
|
||
|
|
||
|
return Ok((Utc::now().naive_local() - start_time).num_microseconds().unwrap())
|
||
|
}
|
||
|
}
|