Last active
March 6, 2021 14:12
-
-
Save Pzixel/f68897097b8ee66cb9301a16780159fe to your computer and use it in GitHub Desktop.
Mongodb test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "mongodb-test" | |
version = "0.1.0" | |
authors = ["Psilon <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
tokio = { version = "0.2", features = ["macros"] } | |
mongodb = "1.1.1" | |
[profile.release] | |
lto = true | |
codegen-units = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use mongodb::{Client, options::ClientOptions, Collection}; | |
use mongodb::options::{Credential, AuthMechanism, StreamAddress}; | |
use mongodb::bson::doc; | |
use std::time::Instant; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let address = "5.5.5.5:27017"; | |
let database = "mydb"; | |
let username = "user"; | |
let password = "pass"; | |
let collection = "myCollection"; | |
//--- | |
let mut client_options = ClientOptions::default(); | |
client_options.direct_connection = Some(true); | |
client_options.hosts = vec![StreamAddress::parse(address)?]; | |
client_options.credential = Some(Credential::builder().username(username.to_string()).password(password.to_string()).mechanism(AuthMechanism::ScramSha1).source(database.to_string()).build()); | |
let client = Client::with_options(client_options)?; | |
let db = client.database(database); | |
let collection = db.collection(collection); | |
bench(&collection).await?; | |
Ok(()) | |
} | |
async fn bench(collection: &Collection) -> Result<(), mongodb::error::Error> { | |
let doc = doc!{"type" : "done", "order_id" : "a5cce297-3533-4032-917a-98e7cee4820b", "reason" : "canceled", "product_id" : "BTC-USD", "sequence" : 7030956642 as u64, "time" : "2018-09-23T01:12:08.856000Z", "side" : "buy", "price" : "6596.48000000", "remaining_size" : "0.00150000" }; | |
let docs: Vec<_> = (0..1000).map(|_| doc.clone()).collect(); | |
let now = Instant::now(); | |
for doc in docs { | |
collection | |
.insert_one(doc, None) | |
.await?; | |
} | |
let elapsed = now.elapsed(); | |
println!("ms: {}", elapsed.as_millis()); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment