Skip to content

Instantly share code, notes, and snippets.

@inv2004
Last active September 23, 2018 02:55
Show Gist options
  • Save inv2004/87b49ee4217872d9260e13a09e6bf524 to your computer and use it in GitHub Desktop.
Save inv2004/87b49ee4217872d9260e13a09e6bf524 to your computer and use it in GitHub Desktop.
#![feature(duration_as_u128)]
#[macro_use]
extern crate bson;
extern crate mongodb;
use std::time::Instant;
use mongodb::db::ThreadedDatabase;
use mongodb::ThreadedClient;
//use mongodb::common::WriteConcern;
const DB: &str = "test_bench";
const TBL: &str = "TEST";
fn _create_indexes(client: &mongodb::Client, tbl: &str) {
let coll = client.db(DB).collection(tbl);
coll.create_index(doc! {"sequence": 1}, None)
.unwrap();
coll.create_index(doc! {"time": 1}, None)
.unwrap();
}
fn init_db() -> mongodb::Result<mongodb::Client> {
let client = mongodb::Client::connect("127.0.0.1", 27017).unwrap();
// let coll_names = client.db(DB).collection_names(None).unwrap();
//
// if !coll_names.as_slice().contains(&TBL.to_string()) {
// create_indexes(&client, &TBL_FULL);
// }
Ok(client)
}
fn bench(r: &mongodb::Client) {
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" };
(0..1000).for_each(move |_| {
r.db(DB)
.collection(TBL)
.insert_one(doc.clone(), None)
.unwrap();
});
}
fn main() {
let r = init_db().unwrap();
let now = Instant::now();
bench(&r);
let elapsed = now.elapsed();
println!("ms: {}", elapsed.as_millis());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment