Skip to content

Commit 875583f

Browse files
authored
RUST-626 Run benchmarks in Evergreen (#428)
1 parent f7186ce commit 875583f

17 files changed

+1218
-272
lines changed

.evergreen/benchmarks.yml

Lines changed: 456 additions & 0 deletions
Large diffs are not rendered by default.

.evergreen/run-bson-benchmarks.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
. ~/.cargo/env
6+
7+
cd benchmarks
8+
cargo run \
9+
--release \
10+
-- --output="../benchmark-results.json" --bson
11+
12+
cat ../benchmark-results.json

.evergreen/run-compile-benchmarks.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
5+
. ~/.cargo/env
6+
7+
FEATURES=""
8+
9+
if [ "$ASYNC_RUNTIME" = "tokio" ]; then
10+
FEATURES="tokio-runtime"
11+
elif [ "$ASYNC_RUNTIME" = "async-std" ]; then
12+
FEATURES="async-std-runtime"
13+
else
14+
echo "invalid async runtime: ${ASYNC_RUNTIME}" >&2
15+
exit 1
16+
fi
17+
18+
rustc --version
19+
20+
SECONDS=0
21+
cargo build --release
22+
DURATION_IN_SECONDS="$SECONDS"
23+
24+
cat > benchmark-results.json <<-EOF
25+
[
26+
{
27+
"info": {
28+
"test_name": "Compile Time",
29+
"args": {}
30+
},
31+
"metrics": [
32+
{
33+
"name": "compile_time_seconds",
34+
"value": $DURATION_IN_SECONDS
35+
}
36+
]
37+
}
38+
]
39+
EOF
40+
41+
cat benchmark-results.json

.evergreen/run-driver-benchmarks.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
. ~/.cargo/env
6+
7+
FEATURES=""
8+
9+
if [ "$ASYNC_RUNTIME" = "tokio" ]; then
10+
FEATURES="tokio-runtime"
11+
elif [ "$ASYNC_RUNTIME" = "async-std" ]; then
12+
FEATURES="async-std-runtime"
13+
else
14+
echo "invalid async runtime: ${ASYNC_RUNTIME}" >&2
15+
exit 1
16+
fi
17+
18+
cd benchmarks
19+
cargo run \
20+
--release \
21+
--no-default-features \
22+
--features ${FEATURES} \
23+
-- --output="../benchmark-results.json" --single --multi --parallel
24+
25+
cat ../benchmark-results.json

benchmarks/Cargo.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ edition = "2018"
66

77
[features]
88
default = ["tokio-runtime"]
9-
tokio-runtime = ["tokio/fs", "tokio/macros", "tokio/rt", "tokio/rt-multi-thread", "tokio-stream"]
10-
async-std-runtime = ["async-std"]
9+
tokio-runtime = [
10+
"tokio/fs",
11+
"tokio/macros",
12+
"tokio/rt",
13+
"tokio/rt-multi-thread",
14+
"tokio-stream",
15+
"mongodb/tokio-runtime"
16+
]
17+
async-std-runtime = ["async-std", "mongodb/async-std-runtime"]
1118

1219
[dependencies]
13-
mongodb = { path = ".." }
20+
mongodb = { path = "..", default-features = false }
1421
serde_json = "1.0.59"
1522
lazy_static = "1.4.0"
1623
clap = "2.33.3"
@@ -22,3 +29,4 @@ tokio-stream = { version = "0.1.6", features = ["io-util"], optional = true }
2229
async-std = { version = "1.9.0", optional = true, features = ["attributes", "unstable"] }
2330
futures = "0.3.8"
2431
anyhow = "1.0.34"
32+
serde = "1"

benchmarks/src/bench/find_many.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ use std::{convert::TryInto, path::PathBuf};
22

33
use anyhow::{bail, Result};
44
use futures::stream::StreamExt;
5-
use mongodb::{Client, Collection, Database, bson::{Bson, Document}};
5+
use mongodb::{
6+
bson::{Bson, Document},
7+
Client,
8+
Collection,
9+
Database,
10+
};
611
use serde_json::Value;
712

813
use crate::{
9-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
14+
bench::{drop_database, Benchmark, COLL_NAME, DATABASE_NAME},
1015
fs::read_to_string,
1116
};
1217

1318
pub struct FindManyBenchmark {
1419
db: Database,
1520
coll: Collection<Document>,
21+
uri: String,
1622
}
1723

1824
// Specifies the options to `FindManyBenchmark::setup` operation.
@@ -29,7 +35,7 @@ impl Benchmark for FindManyBenchmark {
2935
async fn setup(options: Self::Options) -> Result<Self> {
3036
let client = Client::with_uri_str(&options.uri).await?;
3137
let db = client.database(&DATABASE_NAME);
32-
db.drop(None).await?;
38+
drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?;
3339

3440
let num_iter = options.num_iter;
3541

@@ -45,7 +51,11 @@ impl Benchmark for FindManyBenchmark {
4551
let docs = vec![doc.clone(); num_iter];
4652
coll.insert_many(docs, None).await?;
4753

48-
Ok(FindManyBenchmark { db, coll })
54+
Ok(FindManyBenchmark {
55+
db,
56+
coll,
57+
uri: options.uri,
58+
})
4959
}
5060

5161
async fn do_task(&self) -> Result<()> {
@@ -58,7 +68,7 @@ impl Benchmark for FindManyBenchmark {
5868
}
5969

6070
async fn teardown(&self) -> Result<()> {
61-
self.db.drop(None).await?;
71+
drop_database(self.uri.as_str(), self.db.name()).await?;
6272

6373
Ok(())
6474
}

benchmarks/src/bench/find_one.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
use std::{convert::TryInto, path::PathBuf};
22

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

712
use crate::{
8-
bench::{Benchmark, COLL_NAME, DATABASE_NAME},
13+
bench::{drop_database, Benchmark, COLL_NAME, DATABASE_NAME},
914
fs::read_to_string,
1015
};
1116

1217
pub struct FindOneBenchmark {
1318
db: Database,
1419
num_iter: usize,
1520
coll: Collection<Document>,
21+
uri: String,
1622
}
1723

1824
// Specifies the options to a `FindOneBenchmark::setup` operation.
@@ -29,7 +35,7 @@ impl Benchmark for FindOneBenchmark {
2935
async fn setup(options: Self::Options) -> Result<Self> {
3036
let client = Client::with_uri_str(&options.uri).await?;
3137
let db = client.database(&DATABASE_NAME);
32-
db.drop(None).await?;
38+
drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?;
3339

3440
let num_iter = options.num_iter;
3541

@@ -47,7 +53,12 @@ impl Benchmark for FindOneBenchmark {
4753
coll.insert_one(doc.clone(), None).await?;
4854
}
4955

50-
Ok(FindOneBenchmark { db, num_iter, coll })
56+
Ok(FindOneBenchmark {
57+
db,
58+
num_iter,
59+
coll,
60+
uri: options.uri,
61+
})
5162
}
5263

5364
async fn do_task(&self) -> Result<()> {
@@ -61,7 +72,7 @@ impl Benchmark for FindOneBenchmark {
6172
}
6273

6374
async fn teardown(&self) -> Result<()> {
64-
self.db.drop(None).await?;
75+
drop_database(self.uri.as_str(), self.db.name()).await?;
6576

6677
Ok(())
6778
}

benchmarks/src/bench/insert_many.rs

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

3-
use anyhow::{bail, Result};
3+
use anyhow::{bail, Context, Result};
44
use mongodb::{
55
bson::{Bson, Document},
66
Client,
@@ -9,16 +9,17 @@ use mongodb::{
99
};
1010
use serde_json::Value;
1111

12-
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
12+
use crate::bench::{drop_database, Benchmark, COLL_NAME, DATABASE_NAME};
1313

1414
pub struct InsertManyBenchmark {
1515
db: Database,
1616
num_copies: usize,
1717
coll: Collection<Document>,
1818
doc: Document,
19+
uri: String,
1920
}
2021

21-
// Specifies the options to a `InsertManyBenchmark::setup` operation.
22+
/// Specifies the options to a `InsertManyBenchmark::setup` operation.
2223
pub struct Options {
2324
pub num_copies: usize,
2425
pub path: PathBuf,
@@ -32,9 +33,10 @@ impl Benchmark for InsertManyBenchmark {
3233
async fn setup(options: Self::Options) -> Result<Self> {
3334
let client = Client::with_uri_str(&options.uri).await?;
3435
let db = client.database(&DATABASE_NAME);
35-
db.drop(None).await?;
36+
drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?;
3637

3738
let num_copies = options.num_copies;
39+
let uri = options.uri.clone();
3840

3941
// This benchmark uses a file that's quite large, and unfortunately `serde_json` has no
4042
// async version of `from_reader`, so rather than read the whole file into memory at once,
@@ -55,25 +57,34 @@ impl Benchmark for InsertManyBenchmark {
5557
Bson::Document(doc) => doc,
5658
_ => bail!("invalid json test file"),
5759
},
60+
uri,
5861
})
5962
}
6063

6164
async fn before_task(&mut self) -> Result<()> {
6265
self.coll.drop(None).await?;
63-
self.db.create_collection(COLL_NAME.as_str(), None).await?;
66+
self.db
67+
.create_collection(COLL_NAME.as_str(), None)
68+
.await
69+
.context("create in before")?;
6470

6571
Ok(())
6672
}
6773

6874
async fn do_task(&self) -> Result<()> {
6975
let insertions = vec![&self.doc; self.num_copies];
70-
self.coll.insert_many(insertions, None).await?;
76+
self.coll
77+
.insert_many(insertions, None)
78+
.await
79+
.context("insert many")?;
7180

7281
Ok(())
7382
}
7483

7584
async fn teardown(&self) -> Result<()> {
76-
self.db.drop(None).await?;
85+
drop_database(self.uri.as_str(), self.db.name())
86+
.await
87+
.context("teardown")?;
7788

7889
Ok(())
7990
}

benchmarks/src/bench/insert_one.rs

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

3-
use anyhow::{bail, Result};
3+
use anyhow::{bail, Context, Result};
44
use mongodb::{
55
bson::{Bson, Document},
66
Client,
@@ -9,16 +9,17 @@ use mongodb::{
99
};
1010
use serde_json::Value;
1111

12-
use crate::bench::{Benchmark, COLL_NAME, DATABASE_NAME};
12+
use crate::bench::{drop_database, Benchmark, COLL_NAME, DATABASE_NAME};
1313

1414
pub struct InsertOneBenchmark {
1515
db: Database,
1616
num_iter: usize,
1717
coll: Collection<Document>,
1818
doc: Document,
19+
uri: String,
1920
}
2021

21-
// Specifies the options to a `InsertOneBenchmark::setup` operation.
22+
/// Specifies the options to a `InsertOneBenchmark::setup` operation.
2223
pub struct Options {
2324
pub num_iter: usize,
2425
pub path: PathBuf,
@@ -32,9 +33,10 @@ impl Benchmark for InsertOneBenchmark {
3233
async fn setup(options: Self::Options) -> Result<Self> {
3334
let client = Client::with_uri_str(&options.uri).await?;
3435
let db = client.database(&DATABASE_NAME);
35-
db.drop(None).await?;
36+
drop_database(&options.uri, &DATABASE_NAME).await?;
3637

3738
let num_iter = options.num_iter;
39+
let uri = options.uri.clone();
3840

3941
// This benchmark uses a file that's quite large, and unfortunately `serde_json` has no
4042
// async version of `from_reader`, so rather than read the whole file into memory at once,
@@ -55,26 +57,35 @@ impl Benchmark for InsertOneBenchmark {
5557
Bson::Document(doc) => doc,
5658
_ => bail!("invalid json test file"),
5759
},
60+
uri,
5861
})
5962
}
6063

6164
async fn before_task(&mut self) -> Result<()> {
6265
self.coll.drop(None).await?;
63-
self.db.create_collection(COLL_NAME.as_str(), None).await?;
66+
self.db
67+
.create_collection(COLL_NAME.as_str(), None)
68+
.await
69+
.context("create collection")?;
6470

6571
Ok(())
6672
}
6773

6874
async fn do_task(&self) -> Result<()> {
6975
for _ in 0..self.num_iter {
70-
self.coll.insert_one(&self.doc, None).await?;
76+
self.coll
77+
.insert_one(&self.doc, None)
78+
.await
79+
.context("insert one")?;
7180
}
7281

7382
Ok(())
7483
}
7584

7685
async fn teardown(&self) -> Result<()> {
77-
self.db.drop(None).await?;
86+
drop_database(&self.uri, &DATABASE_NAME)
87+
.await
88+
.context("drop database teardown")?;
7889

7990
Ok(())
8091
}

0 commit comments

Comments
 (0)