Skip to content

[nexus-db-queries] demo showing cargo nextest bench #8413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion nexus/db-queries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ itertools.workspace = true
nexus-inventory.workspace = true
nexus-reconfigurator-planning.workspace = true
nexus-test-utils.workspace = true
once_cell.workspace = true
omicron-sled-agent.workspace = true
omicron-test-utils = { workspace = true, features = ["seed-gen"] }
omicron-test-utils.workspace = true
openapiv3.workspace = true
oso.workspace = true
pem.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-queries/benches/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ These queries can be run with:

[source,bash]
----
cargo bench -p nexus-db-queries
cargo nextest bench -p nexus-db-queries
----

Additionally, the "SHOW_CONTENTION" environment variable can be set to display
Expand Down
22 changes: 0 additions & 22 deletions nexus/db-queries/benches/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use nexus_db_queries::db::pub_test_utils::TestDatabase;
use nexus_db_queries::db::pub_test_utils::helpers::create_project;
use nexus_test_utils::sql::Row;
use nexus_test_utils::sql::process_rows;
use omicron_test_utils::dev;
use slog::Logger;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -197,24 +196,3 @@ impl TestHarness {
self.db.terminate().await;
}
}

/// Typically we run our database tests using "cargo nextest run",
/// which triggers the "crdb-seed" binary to create an initialized
/// database when we boot up.
///
/// If we're using "cargo bench", we don't get that guarantee.
/// Go through the database ensuring process manually.
pub async fn setup_db(log: &Logger) {
print!("setting up seed cockroachdb database... ");
let (seed_tar, status) = dev::seed::ensure_seed_tarball_exists(
log,
dev::seed::should_invalidate_seed(),
)
.await
.expect("Failed to create seed tarball for CRDB");
status.log(log, &seed_tar);
unsafe {
std::env::set_var(dev::CRDB_SEED_TAR_ENV, seed_tar);
}
println!("OK");
}
26 changes: 19 additions & 7 deletions nexus/db-queries/benches/sled_reservation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nexus_db_queries::db::DataStore;
use omicron_common::api::external;
use omicron_test_utils::dev;
use omicron_uuid_kinds::InstanceUuid;
use once_cell::sync::Lazy;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -97,7 +98,7 @@ const SLED_PARAMS: [usize; 3] = [1, 4, 8];
// You can run these with the following command:
//
// ```bash
// cargo bench -p nexus-db-queries
// cargo nextest bench -p nexus-db-queries
// ```
//
// You can also set the "SHOW_CONTENTION" environment variable to display
Expand Down Expand Up @@ -421,7 +422,6 @@ fn sled_reservation_benchmark(c: &mut Criterion) {
let logctx = dev::test_setup_log("sled-reservation");

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(harness::setup_db(&logctx.log));

let group_patterns = [
// No Affinity Groups
Expand Down Expand Up @@ -490,13 +490,23 @@ fn sled_reservation_benchmark(c: &mut Criterion) {
// This mitigates any database-caching issues like "loading schema
// on boot", or "connection pooling", as the pool stays the same
// between calls to the benchmark function.
//
// We use a Lazy to only initialize the harness if the
// benchmark's in run mode, not in list mode. (We can't use
// std::sync::LazyLock as of Rust 1.87, because we need
// into_value which isn't available yet.)
let log = logctx.log.clone();
let harness = rt.block_on(async move {
TestHarness::new(&log, sleds).await
let harness = Lazy::new(|| {
rt.block_on(async move {
TestHarness::new(&log, sleds).await
})
});

// Actually invoke the benchmark.
group.bench_function(&name, |b| {
// Force evaluation of the harness outside to_async to
// avoid nested block_on.
let harness = &*harness;
b.to_async(&rt).iter_custom(|iters| {
let opctx = harness.opctx();
let db = harness.db();
Expand All @@ -517,10 +527,12 @@ fn sled_reservation_benchmark(c: &mut Criterion) {
// Clean-up the harness; we'll use a new database between
// varations in parameters.
rt.block_on(async move {
if std::env::var("SHOW_CONTENTION").is_ok() {
harness.print_contention().await;
if let Ok(harness) = Lazy::into_value(harness) {
if std::env::var("SHOW_CONTENTION").is_ok() {
harness.print_contention().await;
}
harness.terminate().await;
}
harness.terminate().await;
});
}
}
Expand Down
Loading