Skip to content

Optimize CockroachDB setup for tests (96% reduction) #493

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

Merged
merged 25 commits into from
Dec 9, 2021

Conversation

smklein
Copy link
Collaborator

@smklein smklein commented Dec 9, 2021

Optimizes setup time for CRDB-based tests by seeding + copying a seeded database, rather than populating the database anew during each test. This significantly cuts down on the setup time for any test which wants to use even a simple, single-node Cockroach instance.

Comparing the benchmark originally added in #492:

Before:

Test Setup/do_full_setup
                        time:   [7.1417 s 7.2563 s 7.3714 s]
Test Setup/do_crdb_setup
                        time:   [6.4927 s 6.8566 s 7.2067 s]
Test Setup/do_clickhouse_setup
                        time:   [521.88 ms 527.69 ms 533.80 ms]

After:

Test Setup/do_full_setup
                        time:   [1.1145 s 1.1477 s 1.1832 s]
                        change: [-84.712% -84.183% -83.623%] (p = 0.00 < 0.05)
                        Performance has improved. 
Test Setup/do_crdb_setup
                        time:   [229.49 ms 242.09 ms 253.62 ms]
                        change: [-96.706% -96.469% -96.203%] (p = 0.00 < 0.05)
                        Performance has improved.
Test Setup/do_clickhouse_setup
                        time:   [521.93 ms 526.22 ms 530.98 ms]
                        change: [-1.6256% -0.2787% +1.1729%] (p = 0.71 > 0.05)
                        No change in performance detected. 

@smklein smklein changed the title Optimize CockroachDB setup time for tests Optimize CockroachDB setup time for tests (-96% reduction of DB setup time) Dec 9, 2021
@smklein smklein changed the title Optimize CockroachDB setup time for tests (-96% reduction of DB setup time) Optimize CockroachDB setup for tests (96% reduction) Dec 9, 2021
@david-crespo
Copy link
Contributor

Life-changing! Tried it myself and got this on build, which I don't get on main.

error[E0658]: sym operands for inline assembly are unstable
   --> /Users/david/.cargo/git/checkouts/dropshot-a4a923d29dccc492/ff33033/dropshot/src/lib.rs:574:1
    |
574 |   #[usdt::provider(provider = "dropshot")]
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
   ::: /Users/david/.cargo/git/checkouts/dropshot-a4a923d29dccc492/ff33033/dropshot/src/server.rs:387:13

@david-crespo
Copy link
Contributor

Ah good, at least the macOS CI build can reproduce that.

@david-crespo
Copy link
Contributor

Confirmed that fixed it.

# before
cargo test --package omicron-nexus --test test_console_api -- test_sessions
9.48s user 2.08s system 31% cpu 36.163 total

# after
cargo test --package omicron-nexus --test test_console_api -- test_sessions
1.71s user 0.58s system 53% cpu 4.303 total

This is the best day of my life.

@@ -23,7 +23,6 @@ default-members = [
"common",
"nexus",
"nexus/src/db/db-macros",
"nexus/test-utils",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this has been removed from the default members, but we're definitely still using / testing it.

This just means that cargo build doesn't, by default, build a "test-only" helper crate - doing so would also require "cockroachdb" to be on the PATH earlier for some of our checks.

Note: This still gets built with cargo build --all-targets and cargo test, so no worries on coverage.

@@ -14,6 +14,18 @@ set -o xtrace
cargo --version
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here - and in the ".github/workflows/rust.yml" file - just make sure cockroach is downloaded and on the PATH before we cargo build --all-targets, since we're now using it in a build script.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the README to reflect this? Basically this part:

CockroachDB v21.1.10.
The test suite expects to be able to start a single-node CockroachDB cluster using the cockroach executable on your PATH.

I think that should now say "the build and test suite expect..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Collaborator

@davepacheco davepacheco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice improvement!

/// By creating a "seed" version of the database, we can cut down
/// on the time spent performing this operation. Instead, we opt
/// to copy the database from this seed location.
pub const SEED_DB_DIR: &str = "/tmp/crdb-base";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to make this use one of the crates that finds a temp dir for you. I think elsewhere we do this, and it winds up using $TMPDIR from the environment in at least some cases.

Context: /tmp on Helios is generally tmpfs, which comes from swap. This uses up physical memory. We already consume a lot of that during builds and tests and it's led to exhaustion even on decently large systems. There's no need for this to be in-memory. Plus, allowing people to override this in a standard way allows folks to put this on a ZFS dataset that they've configured differently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn copy_dir(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
) -> std::io::Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised there's not a standard crate for doing this. There are a lot of edge cases, which I guess we haven't run into in this case? (what if there's a named pipe here or a symlink?)

Also, this is all synchronous, even though the caller is async. I guess that's okay here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I looked around, and couldn't find anything much better than doing this myself.

I can make it async, but given the "test setup" context, I'm not sure how worthwhile this is to optimize for concurrency?

@@ -14,6 +14,18 @@ set -o xtrace
cargo --version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the README to reflect this? Basically this part:

CockroachDB v21.1.10.
The test suite expects to be able to start a single-node CockroachDB cluster using the cockroach executable on your PATH.

I think that should now say "the build and test suite expect..."

Base automatically changed from benchmark-tests to main December 9, 2021 21:51
@smklein smklein merged commit ab8872a into main Dec 9, 2021
@smklein smklein deleted the optimize-crdb-setup branch December 9, 2021 23:23
smklein added a commit that referenced this pull request Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants