Skip to content

Commit 66967e6

Browse files
authored
RUST-1994 Add a benchmark for client connection (#1180)
1 parent ebb2469 commit 66967e6

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

benchmarks/src/bench/run_command.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use mongodb::{
88
use crate::bench::{drop_database, Benchmark, DATABASE_NAME};
99

1010
pub struct RunCommandBenchmark {
11-
db: Database,
11+
db: Option<Database>,
1212
num_iter: usize,
1313
cmd: Document,
1414
uri: String,
@@ -17,16 +17,33 @@ pub struct RunCommandBenchmark {
1717
pub struct Options {
1818
pub num_iter: usize,
1919
pub uri: String,
20+
pub cold_start: bool,
21+
}
22+
23+
impl RunCommandBenchmark {
24+
async fn init_db(uri: &str) -> Result<Database> {
25+
let client = Client::with_uri_str(uri).await?;
26+
Ok(client.database(&DATABASE_NAME))
27+
}
28+
29+
async fn get_db(&self) -> Result<Database> {
30+
match self.db.as_ref() {
31+
Some(db) => Ok(db.clone()),
32+
None => Self::init_db(&self.uri).await,
33+
}
34+
}
2035
}
2136

2237
#[async_trait::async_trait]
2338
impl Benchmark for RunCommandBenchmark {
2439
type Options = Options;
2540

2641
async fn setup(options: Self::Options) -> Result<Self> {
27-
let client = Client::with_uri_str(&options.uri).await?;
28-
let db = client.database(&DATABASE_NAME);
29-
drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?;
42+
let db = if options.cold_start {
43+
None
44+
} else {
45+
Some(Self::init_db(&options.uri).await?)
46+
};
3047

3148
Ok(RunCommandBenchmark {
3249
db,
@@ -39,7 +56,8 @@ impl Benchmark for RunCommandBenchmark {
3956
async fn do_task(&self) -> Result<()> {
4057
for _ in 0..self.num_iter {
4158
let _doc = self
42-
.db
59+
.get_db()
60+
.await?
4361
.run_command(self.cmd.clone())
4462
.await
4563
.context("run command")?;
@@ -49,7 +67,7 @@ impl Benchmark for RunCommandBenchmark {
4967
}
5068

5169
async fn teardown(&self) -> Result<()> {
52-
drop_database(self.uri.as_str(), self.db.name()).await?;
70+
drop_database(self.uri.as_str(), &DATABASE_NAME).await?;
5371

5472
Ok(())
5573
}

benchmarks/src/main.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ use std::{
2727

2828
use anyhow::Result;
2929
use clap::{App, Arg, ArgMatches};
30-
use futures::Future;
31-
use futures::FutureExt;
30+
use futures::{Future, FutureExt};
3231
use mongodb::options::ClientOptions;
3332
use once_cell::sync::Lazy;
3433

@@ -62,6 +61,7 @@ const DEEP_BSON_DECODING: &'static str = "Deep BSON Decoding";
6261
const FULL_BSON_ENCODING: &'static str = "Full BSON Encoding";
6362
const FULL_BSON_DECODING: &'static str = "Full BSON Decoding";
6463
const RUN_COMMAND_BENCH: &'static str = "Run Command";
64+
const RUN_COMMAND_COLD_START_BENCH: &'static str = "Run Command (cold start)";
6565
const FIND_ONE_BENCH: &'static str = "Find one";
6666
const FIND_MANY_BENCH: &'static str = "Find many and empty cursor";
6767
const FIND_MANY_BENCH_RAW: &'static str = "Find many and empty cursor (raw BSON)";
@@ -101,12 +101,14 @@ enum BenchmarkId {
101101
GridFsUpload,
102102
GridFsMultiDownload,
103103
GridFsMultiUpload,
104+
RunCommandColdStart,
104105
}
105106

106107
impl BenchmarkId {
107108
fn name(self) -> &'static str {
108109
match self {
109110
BenchmarkId::RunCommand => RUN_COMMAND_BENCH,
111+
BenchmarkId::RunCommandColdStart => RUN_COMMAND_COLD_START_BENCH,
110112
BenchmarkId::FindOneById => FIND_ONE_BENCH,
111113
BenchmarkId::SmallDocInsertOne => SMALL_DOC_INSERT_ONE_BENCH,
112114
BenchmarkId::LargeDocInsertOne => LARGE_DOC_INSERT_ONE_BENCH,
@@ -186,7 +188,7 @@ const WRITE_BENCHES: &[&'static str] = &[
186188
GRIDFS_MULTI_UPLOAD_BENCH,
187189
];
188190

189-
const MAX_ID: u8 = BenchmarkId::GridFsMultiUpload as u8;
191+
const MAX_ID: u8 = BenchmarkId::RunCommandColdStart as u8;
190192

191193
async fn run_benchmarks(
192194
uri: &str,
@@ -208,13 +210,28 @@ async fn run_benchmarks(
208210
let run_command_options = bench::run_command::Options {
209211
num_iter: 10000,
210212
uri: uri.to_string(),
213+
cold_start: false,
211214
};
212215
let run_command =
213216
bench::run_benchmark::<RunCommandBenchmark>(run_command_options).await?;
214217

215218
comp_score += score_test(run_command, RUN_COMMAND_BENCH, 0.13, more_info);
216219
}
217220

221+
// Run command, including client setup time
222+
BenchmarkId::RunCommandColdStart => {
223+
let run_command_options = bench::run_command::Options {
224+
num_iter: 100,
225+
uri: uri.to_string(),
226+
cold_start: true,
227+
};
228+
let run_command =
229+
bench::run_benchmark::<RunCommandBenchmark>(run_command_options).await?;
230+
231+
comp_score +=
232+
score_test(run_command, RUN_COMMAND_COLD_START_BENCH, 0.13, more_info);
233+
}
234+
218235
// Small doc insertOne
219236
BenchmarkId::SmallDocInsertOne => {
220237
let small_insert_one_options = bench::insert_one::Options {
@@ -530,6 +547,7 @@ fn parse_ids(matches: ArgMatches) -> HashSet<BenchmarkId> {
530547

531548
if matches.is_present("single") {
532549
ids.insert(BenchmarkId::RunCommand);
550+
ids.insert(BenchmarkId::RunCommandColdStart);
533551
ids.insert(BenchmarkId::FindOneById);
534552
ids.insert(BenchmarkId::SmallDocInsertOne);
535553
ids.insert(BenchmarkId::LargeDocInsertOne);
@@ -557,6 +575,7 @@ fn parse_ids(matches: ArgMatches) -> HashSet<BenchmarkId> {
557575
}
558576
if matches.is_present("driver") {
559577
ids.insert(BenchmarkId::RunCommand);
578+
ids.insert(BenchmarkId::RunCommandColdStart);
560579
ids.insert(BenchmarkId::FindOneById);
561580
ids.insert(BenchmarkId::SmallDocInsertOne);
562581
ids.insert(BenchmarkId::LargeDocInsertOne);
@@ -591,6 +610,13 @@ async fn main() {
591610
"MAX_ID not up to date"
592611
);
593612

613+
let mut id_help = String::from("\nRun benchmarks by id number (comma-separated):\n");
614+
for ix in 1..=MAX_ID {
615+
let id = BenchmarkId::try_from(ix).unwrap();
616+
id_help.push_str(&format!(" {}: {}\n", ix, id.name()));
617+
}
618+
id_help.push_str(" all: All benchmarks\n ");
619+
594620
let matches = App::new("RustDriverBenchmark")
595621
.version(env!("CARGO_PKG_VERSION"))
596622
.about("Runs performance micro-bench")
@@ -637,33 +663,7 @@ async fn main() {
637663
.long("ids")
638664
.takes_value(true)
639665
.help("Run benchmarks by id number (comma-separated)")
640-
.long_help(
641-
"
642-
Run benchmarks by id number (comma-separated):
643-
1: Run command
644-
2: Find one by ID
645-
3: Small doc insertOne
646-
4: Large doc insertOne
647-
5: Find many and empty the cursor
648-
6: Small doc bulk insert
649-
7: Large doc bulk insert
650-
8: LDJSON multi-file import
651-
9: LDJSON multi-file export
652-
10: BSON flat document decode
653-
11: BSON flat document encode
654-
12: BSON deeply nested document decode
655-
13: BSON deeply nested document encode
656-
14: BSON full document decode
657-
15: BSON full document encode
658-
16: Find many and empty the cursor (raw BSON)
659-
17: Find many and empty the cursor (serde structs)
660-
18: GridFS download
661-
19: GridFS upload
662-
20: GridFS multi-file download
663-
21: GridFS multi-file upload
664-
all: All benchmarks
665-
",
666-
),
666+
.long_help(&id_help),
667667
)
668668
.arg(
669669
Arg::with_name("output")

0 commit comments

Comments
 (0)