Changed the code to run arynchronously
This commit is contained in:
parent
7ec01a8717
commit
df207f20fe
@ -6,6 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
futures = "0.3.28"
|
||||||
mongodb = "2.5.0"
|
mongodb = "2.5.0"
|
||||||
serde = "1.0.163"
|
serde = "1.0.163"
|
||||||
tokio = "1.28.1"
|
tokio = "1.28.1"
|
||||||
|
50
src/main.rs
50
src/main.rs
@ -1,5 +1,8 @@
|
|||||||
use mongodb::{Client, options::{ClientOptions, Credential}};
|
use mongodb::{Client, options::{ClientOptions, Credential}, Collection};
|
||||||
use std::time::{self, SystemTime};
|
use std::time::{self, SystemTime, Duration};
|
||||||
|
use tokio::task;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod packet;
|
mod packet;
|
||||||
use packet::Packet;
|
use packet::Packet;
|
||||||
@ -28,23 +31,40 @@ async fn main() {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const num_queries: u32 = 5_000;
|
//Setup the number of queries that we want to run as well as Arcs and Mutexes to pass to threads
|
||||||
let collection = db.collection::<Packet>("test");
|
const NUM_QUERIES: u32 = 5_000;
|
||||||
let mut queries: Vec<std::time::Duration> = vec![];
|
let collection: Arc<Collection<Packet>> = Arc::new(db.collection::<Packet>("test"));
|
||||||
|
let mut tasks = vec![];
|
||||||
for _ in 0..num_queries {
|
let queries: Arc<Mutex<Vec<Duration>>> = Arc::new(Mutex::new(vec![]));
|
||||||
let start_time = SystemTime::now();
|
let start_time = SystemTime::now();
|
||||||
|
|
||||||
collection.insert_one(Packet{
|
//Start spawning threads to run the requests
|
||||||
time: time::SystemTime::now(),
|
for _ in 0..NUM_QUERIES {
|
||||||
text: format!("Current Unix time: {:?}", time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap())
|
tasks.push(task::spawn(run_query(collection.clone(), queries.clone())))
|
||||||
}, None).await.unwrap();
|
|
||||||
|
|
||||||
let duration = time::SystemTime::now().duration_since(start_time).unwrap();
|
|
||||||
queries.push(duration);
|
|
||||||
}
|
}
|
||||||
|
futures::future::join_all(tasks).await;
|
||||||
|
|
||||||
for (i, duration) in queries.iter().enumerate() {
|
//Print out the results
|
||||||
|
for (i, duration) in queries.lock().unwrap().iter().enumerate() {
|
||||||
println!("{}: {}.{} milliseconds", i, duration.as_millis(), duration.as_micros());
|
println!("{}: {}.{} milliseconds", i, duration.as_millis(), duration.as_micros());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("Ran {} queries in {}s", NUM_QUERIES, time::SystemTime::now().duration_since(start_time).unwrap().as_secs_f32());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function that runs an upload to a a MongoDB collection with a recorded timestamp
|
||||||
|
/// asynchronously
|
||||||
|
async fn run_query(collection: Arc<Collection::<Packet>>, query_records: Arc<Mutex<Vec<Duration>>>) {
|
||||||
|
//Record the start time for a query
|
||||||
|
let query_time = SystemTime::now();
|
||||||
|
|
||||||
|
//Perform the insert
|
||||||
|
collection.insert_one(Packet{
|
||||||
|
time: SystemTime::now(),
|
||||||
|
text: format!("Current Unix time: {:?}", SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap())
|
||||||
|
}, None).await.unwrap();
|
||||||
|
|
||||||
|
//Record the duration and add it to the list
|
||||||
|
let duration = SystemTime::now().duration_since(query_time).unwrap();
|
||||||
|
query_records.lock().unwrap().push(duration);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user