Skip to content

Commit 6067e98

Browse files
committed
RUST-611 initial update to current driver
1 parent bcee44e commit 6067e98

File tree

11 files changed

+306
-292
lines changed

11 files changed

+306
-292
lines changed

benchmarks/Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ version = "0.1.0"
44
authors = ["benjirewis <[email protected]>"]
55
edition = "2018"
66

7+
[features]
8+
default = ["tokio-runtime"]
9+
tokio-runtime = ["tokio/macros", "tokio/rt-core", "tokio/rt-threaded"]
10+
async-std-runtime = ["async-std"]
11+
712
[dependencies]
813
mongodb = { path = ".." }
9-
bson = "0.13.0"
10-
serde_json = "1.0.39"
11-
lazy_static = "1.3.0"
12-
num_cpus = "1.10.1"
13-
clap = "2.33.0"
14-
indicatif = "0.11.0"
14+
serde_json = "1.0.59"
15+
lazy_static = "1.4.0"
16+
clap = "2.33.3"
17+
indicatif = "0.15.0"
18+
async-trait = "0.1.41"
19+
tokio = { version = "0.2.23", features = ["sync"] }
20+
async-std = { version = "1.6.2", optional = true, features = ["attributes"] }
21+
futures = "0.3.8"
22+
anyhow = "1.0.34"

benchmarks/src/bench/find_many.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use std::{fs::File, path::PathBuf};
1+
use std::{convert::TryInto, fs::File, path::PathBuf};
22

3-
use bson::Bson;
4-
use mongodb::{Client, Collection, Database};
3+
use anyhow::{bail, Result};
4+
use futures::stream::StreamExt;
5+
use mongodb::{bson::Bson, Client, Collection, Database};
56
use serde_json::Value;
67

7-
use crate::{
8-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
9-
error::{Error, Result},
10-
};
8+
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
119

1210
pub struct FindManyBenchmark {
1311
db: Database,
@@ -21,40 +19,43 @@ pub struct Options {
2119
pub uri: String,
2220
}
2321

22+
#[async_trait::async_trait]
2423
impl Benchmark for FindManyBenchmark {
2524
type Options = Options;
2625

27-
fn setup(options: Self::Options) -> Result<Self> {
28-
let client = Client::with_uri_str(&options.uri)?;
26+
async fn setup(options: Self::Options) -> Result<Self> {
27+
let client = Client::with_uri_str(&options.uri).await?;
2928
let db = client.database(&DATABASE_NAME);
30-
db.drop()?;
29+
db.drop(None).await?;
3130

32-
let mut file = File::open(options.path)?;
31+
let num_iter = options.num_iter;
3332

34-
let json: Value = serde_json::from_reader(&mut file)?;
35-
let doc = match json.into() {
33+
let mut file = spawn_blocking_and_await!(File::open(options.path))?;
34+
35+
let json: Value = spawn_blocking_and_await!(serde_json::from_reader(&mut file))?;
36+
let doc = match json.try_into()? {
3637
Bson::Document(doc) => doc,
37-
_ => return Err(Error::UnexpectedJson("invalid json test file".to_string())),
38+
_ => bail!("invalid json test file"),
3839
};
3940

4041
let coll = db.collection(&COLL_NAME);
41-
let docs = vec![doc.clone(); options.num_iter];
42-
coll.insert_many(docs, None)?;
42+
let docs = vec![doc.clone(); num_iter];
43+
coll.insert_many(docs, None).await?;
4344

4445
Ok(FindManyBenchmark { db, coll })
4546
}
4647

47-
fn do_task(&self) -> Result<()> {
48-
let cursor = self.coll.find(None, None)?;
49-
for doc in cursor {
48+
async fn do_task(&self) -> Result<()> {
49+
let mut cursor = self.coll.find(None, None).await?;
50+
while let Some(doc) = cursor.next().await {
5051
doc?;
5152
}
5253

5354
Ok(())
5455
}
5556

56-
fn teardown(&self) -> Result<()> {
57-
self.db.drop()?;
57+
async fn teardown(&self) -> Result<()> {
58+
self.db.drop(None).await?;
5859

5960
Ok(())
6061
}

benchmarks/src/bench/find_one.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
use std::{fs::File, path::PathBuf};
1+
use std::{convert::TryInto, fs::File, path::PathBuf};
22

3-
use bson::Bson;
4-
use mongodb::{options::FindOptions, Client, Collection, Database};
3+
use anyhow::{bail, Result};
4+
use mongodb::{
5+
bson::{doc, Bson},
6+
Client,
7+
Collection,
8+
Database,
9+
};
510
use serde_json::Value;
611

7-
use crate::{
8-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
9-
error::{Error, Result},
10-
};
12+
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
1113

1214
pub struct FindOneBenchmark {
1315
db: Database,
@@ -22,49 +24,46 @@ pub struct Options {
2224
pub uri: String,
2325
}
2426

27+
#[async_trait::async_trait]
2528
impl Benchmark for FindOneBenchmark {
2629
type Options = Options;
2730

28-
fn setup(options: Self::Options) -> Result<Self> {
29-
let client = Client::with_uri_str(&options.uri)?;
31+
async fn setup(options: Self::Options) -> Result<Self> {
32+
let client = Client::with_uri_str(&options.uri).await?;
3033
let db = client.database(&DATABASE_NAME);
31-
db.drop()?;
34+
db.drop(None).await?;
35+
36+
let num_iter = options.num_iter;
3237

33-
let mut file = File::open(options.path)?;
38+
let mut file = spawn_blocking_and_await!(File::open(options.path))?;
3439

35-
let json: Value = serde_json::from_reader(&mut file)?;
36-
let mut doc = match json.into() {
40+
let json: Value = spawn_blocking_and_await!(serde_json::from_reader(&mut file))?;
41+
let mut doc = match json.try_into()? {
3742
Bson::Document(doc) => doc,
38-
_ => return Err(Error::UnexpectedJson("invalid json test file".to_string())),
43+
_ => bail!("invalid json test file"),
3944
};
4045

4146
let coll = db.collection(&COLL_NAME);
42-
for i in 0..options.num_iter {
47+
for i in 0..num_iter {
4348
doc.insert("_id", i as i32);
44-
coll.insert_one(doc.clone(), None)?;
49+
coll.insert_one(doc.clone(), None).await?;
4550
}
4651

47-
Ok(FindOneBenchmark {
48-
db,
49-
num_iter: options.num_iter,
50-
coll,
51-
})
52+
Ok(FindOneBenchmark { db, num_iter, coll })
5253
}
5354

54-
fn do_task(&self) -> Result<()> {
55-
let find_options = FindOptions::builder().limit(Some(1)).build();
55+
async fn do_task(&self) -> Result<()> {
5656
for i in 0..self.num_iter {
57-
let mut cursor = self
58-
.coll
59-
.find(Some(doc! { "_id": i as i32 }), Some(find_options.clone()))?;
60-
let _doc = cursor.next();
57+
self.coll
58+
.find_one(Some(doc! { "_id": i as i32 }), None)
59+
.await?;
6160
}
6261

6362
Ok(())
6463
}
6564

66-
fn teardown(&self) -> Result<()> {
67-
self.db.drop()?;
65+
async fn teardown(&self) -> Result<()> {
66+
self.db.drop(None).await?;
6867

6968
Ok(())
7069
}

benchmarks/src/bench/insert_many.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
use std::{fs::File, path::PathBuf};
2-
3-
use bson::{Bson, Document};
4-
use mongodb::{Client, Collection, Database};
1+
use std::{convert::TryInto, fs::File, path::PathBuf};
2+
3+
use anyhow::{bail, Result};
4+
use mongodb::{
5+
bson::{Bson, Document},
6+
Client,
7+
Collection,
8+
Database,
9+
};
510
use serde_json::Value;
611

7-
use crate::{
8-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
9-
error::{Error, Result},
10-
};
12+
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
1113

1214
pub struct InsertManyBenchmark {
1315
db: Database,
@@ -23,47 +25,50 @@ pub struct Options {
2325
pub uri: String,
2426
}
2527

28+
#[async_trait::async_trait]
2629
impl Benchmark for InsertManyBenchmark {
2730
type Options = Options;
2831

29-
fn setup(options: Self::Options) -> Result<Self> {
30-
let client = Client::with_uri_str(&options.uri)?;
32+
async fn setup(options: Self::Options) -> Result<Self> {
33+
let client = Client::with_uri_str(&options.uri).await?;
3134
let db = client.database(&DATABASE_NAME);
32-
db.drop()?;
35+
db.drop(None).await?;
36+
37+
let num_copies = options.num_copies;
3338

34-
let mut file = File::open(options.path)?;
39+
let mut file = spawn_blocking_and_await!(File::open(options.path))?;
3540

36-
let json: Value = serde_json::from_reader(&mut file)?;
41+
let json: Value = spawn_blocking_and_await!(serde_json::from_reader(&mut file))?;
3742

3843
let coll = db.collection(&COLL_NAME);
3944

4045
Ok(InsertManyBenchmark {
4146
db,
42-
num_copies: options.num_copies,
47+
num_copies,
4348
coll,
44-
doc: match json.into() {
49+
doc: match json.try_into()? {
4550
Bson::Document(doc) => doc,
46-
_ => return Err(Error::UnexpectedJson("invalid json test file".to_string())),
51+
_ => bail!("invalid json test file"),
4752
},
4853
})
4954
}
5055

51-
fn before_task(&mut self) -> Result<()> {
52-
self.coll.drop()?;
53-
self.db.create_collection(&COLL_NAME, None)?;
56+
async fn before_task(&mut self) -> Result<()> {
57+
self.coll.drop(None).await?;
58+
self.db.create_collection(&COLL_NAME, None).await?;
5459

5560
Ok(())
5661
}
5762

58-
fn do_task(&self) -> Result<()> {
63+
async fn do_task(&self) -> Result<()> {
5964
let insertions = vec![self.doc.clone(); self.num_copies];
60-
self.coll.insert_many(insertions, None)?;
65+
self.coll.insert_many(insertions, None).await?;
6166

6267
Ok(())
6368
}
6469

65-
fn teardown(&self) -> Result<()> {
66-
self.db.drop()?;
70+
async fn teardown(&self) -> Result<()> {
71+
self.db.drop(None).await?;
6772

6873
Ok(())
6974
}

benchmarks/src/bench/insert_one.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
use std::{fs::File, path::PathBuf};
2-
3-
use bson::{Bson, Document};
4-
use mongodb::{Client, Collection, Database};
1+
use std::{convert::TryInto, fs::File, path::PathBuf};
2+
3+
use anyhow::{bail, Result};
4+
use mongodb::{
5+
bson::{Bson, Document},
6+
Client,
7+
Collection,
8+
Database,
9+
};
510
use serde_json::Value;
611

7-
use crate::{
8-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
9-
error::{Error, Result},
10-
};
12+
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
1113

1214
pub struct InsertOneBenchmark {
1315
db: Database,
@@ -23,48 +25,51 @@ pub struct Options {
2325
pub uri: String,
2426
}
2527

28+
#[async_trait::async_trait]
2629
impl Benchmark for InsertOneBenchmark {
2730
type Options = Options;
2831

29-
fn setup(options: Self::Options) -> Result<Self> {
30-
let client = Client::with_uri_str(&options.uri)?;
32+
async fn setup(options: Self::Options) -> Result<Self> {
33+
let client = Client::with_uri_str(&options.uri).await?;
3134
let db = client.database(&DATABASE_NAME);
32-
db.drop()?;
35+
db.drop(None).await?;
36+
37+
let num_iter = options.num_iter;
3338

34-
let mut file = File::open(options.path)?;
39+
let mut file = spawn_blocking_and_await!(File::open(options.path))?;
3540

36-
let json: Value = serde_json::from_reader(&mut file)?;
41+
let json: Value = spawn_blocking_and_await!(serde_json::from_reader(&mut file))?;
3742

3843
let coll = db.collection(&COLL_NAME);
3944

4045
Ok(InsertOneBenchmark {
4146
db,
42-
num_iter: options.num_iter,
47+
num_iter,
4348
coll,
44-
doc: match json.into() {
49+
doc: match json.try_into()? {
4550
Bson::Document(doc) => doc,
46-
_ => return Err(Error::UnexpectedJson("invalid json test file".to_string())),
51+
_ => bail!("invalid json test file"),
4752
},
4853
})
4954
}
5055

51-
fn before_task(&mut self) -> Result<()> {
52-
self.coll.drop()?;
53-
self.db.create_collection(&COLL_NAME, None)?;
56+
async fn before_task(&mut self) -> Result<()> {
57+
self.coll.drop(None).await?;
58+
self.db.create_collection(&COLL_NAME, None).await?;
5459

5560
Ok(())
5661
}
5762

58-
fn do_task(&self) -> Result<()> {
63+
async fn do_task(&self) -> Result<()> {
5964
for _ in 0..self.num_iter {
60-
self.coll.insert_one(self.doc.clone(), None)?;
65+
self.coll.insert_one(self.doc.clone(), None).await?;
6166
}
6267

6368
Ok(())
6469
}
6570

66-
fn teardown(&self) -> Result<()> {
67-
self.db.drop()?;
71+
async fn teardown(&self) -> Result<()> {
72+
self.db.drop(None).await?;
6873

6974
Ok(())
7075
}

0 commit comments

Comments
 (0)