diff --git a/Cargo.lock b/Cargo.lock index f91294697e7..2e427fa7b2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6198,6 +6198,7 @@ dependencies = [ "omicron-test-utils", "omicron-uuid-kinds", "omicron-workspace-hack", + "once_cell", "openapiv3", "oso", "oximeter 0.1.0", diff --git a/nexus/db-queries/Cargo.toml b/nexus/db-queries/Cargo.toml index 151dbc71f78..732ea5e6b61 100644 --- a/nexus/db-queries/Cargo.toml +++ b/nexus/db-queries/Cargo.toml @@ -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 diff --git a/nexus/db-queries/benches/README.adoc b/nexus/db-queries/benches/README.adoc index f4f1a716344..1fab3b884fb 100644 --- a/nexus/db-queries/benches/README.adoc +++ b/nexus/db-queries/benches/README.adoc @@ -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 diff --git a/nexus/db-queries/benches/harness/mod.rs b/nexus/db-queries/benches/harness/mod.rs index b4232cab202..c10aee87faf 100644 --- a/nexus/db-queries/benches/harness/mod.rs +++ b/nexus/db-queries/benches/harness/mod.rs @@ -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; @@ -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"); -} diff --git a/nexus/db-queries/benches/sled_reservation.rs b/nexus/db-queries/benches/sled_reservation.rs index c1d1f6d0785..99b103025f3 100644 --- a/nexus/db-queries/benches/sled_reservation.rs +++ b/nexus/db-queries/benches/sled_reservation.rs @@ -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; @@ -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 @@ -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 @@ -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(); @@ -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; }); } }