feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore [rebased for main]#2600
Merged
mdelapenya merged 14 commits intotestcontainers:mainfrom Jul 2, 2024
Conversation
…reating & restoring snapshots
Remove all commented lines
…hat don't register as "postgres"
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
mdelapenya
reviewed
Jun 21, 2024
mdelapenya
reviewed
Jun 24, 2024
mdelapenya
reviewed
Jun 24, 2024
mdelapenya
reviewed
Jun 24, 2024
docs/modules/postgres.md
Outdated
| No default is supplied, so you need to set it explicitly. | ||
|
|
||
| <!--codeinclude--> | ||
| [Example Wait Strategies](../../modules/postgres/postgres_test.go) inside_block:waitStrategy |
Member
There was a problem hiding this comment.
Let's update the docs path:
Suggested change
| [Example Wait Strategies](../../modules/postgres/postgres_test.go) inside_block:waitStrategy | |
| [Example Wait Strategies](../../modules/postgres/wait_strategies.go) inside_block:waitStrategy |
Once this is done and the CI passes, I think we can merge this one 🚀
Contributor
Author
There was a problem hiding this comment.
Cool! Fixed those two nits, LMK how it goes.
FYI going on holiday in a few hours, so if there's more things you want me to change it'll have to wait until next week.
Thanks so much for engaging with this :)
mdelapenya
reviewed
Jun 24, 2024
ash2k
reviewed
Jun 26, 2024
Member
Contributor
Author
|
Looks good to me! :) Thanks for making the change. |
mdelapenya
reviewed
Jul 2, 2024
mdelapenya
added a commit
that referenced
this pull request
Jul 4, 2024
* main: chore(deps): bump mkdocs-include-markdown-plugin from 6.0.4 to 6.2.1 (#2617) chore(deps): bump peter-evans/slash-command-dispatch from 3.0.2 to 4.0.0 (#2561) docs: document ryuk timeouts for compose (#2620) chore: use self-hosted worker for Windows tests (#2619) feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore [rebased for main] (#2600)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See description of #2599
This is the same PR, but rebased for main.
The original description follows, just with latest benchmarks
What does this PR do?
The postgres module has functions for saving snapshots and restoring them later.
This feature is helpful for integration testing without having to manually clear the database.
This PR speeds up the snapshot/restore functionality by executing the necessary SQL commands via a regular
sql.DBconnection.If the driver is not available in the test environment, or the commands fail for some other reason (SSL config, etc), it falls back to the old variant of calling
docker exec.Why is it important?
docker execcan be unnecessarily slow, especially when using docker in a VM with Docker Desktop or colima.On my laptop (MacBook Air M1), each call to
Container.Restore()takes about 150~200ms.In a small subset of 20 of our tests, where each test restores the database before starting, with creating the initial container, this brings the total time up to 12 seconds, which is quite a lot for a few small database tests.
After these changes, the same subset of tests now takes 6 seconds.
Related issues
How to test this PR
Follow-ups
I've also considered caching the DB pool that is created to manage the databases.
After running some benchmark with this code (from branch X), I noticed it does not have a big impact on the whole test suite - my internal test suite does not change in execution time (since the time is dominated by re-connecting the actual test client, which will be terminated when the database gets deleted during the restore).
The module/postgres test suite even gets slower:
cached: main...cfstras:postgres-snapshot-sql-cached-rebased-main
not cached: this PR
testcontainers main, sql, not cached: 42.227 s ± 0.635
testcontainers main, sql, cached: 43.207 s ± 1.549
testcontainers main, without PR (but amended tests): 45.298 s ± 5.610 s
Branch: cfstras:testcontainers-go:main-with-postgres-sql-test-fixesinternal testsuite, with testcontainers main, sql, cached, with ryuk: 14.123 s ± 1.795 s
internal testsuite, with testcontainers main, sql, cached, without ryuk: 11.299 s ± 1.503 s
internal testsuite, with testcontainers main, sql, not cached, without ryuk: 10.597 s ± 0.641 s
internal testsuite, with testcontainers main, without my PR, not cached, without ryuk: 14.245 s ± 0.639 s
internal testsuite, with testcontainers main, docker-exec fallback, not cached, without ryuk: 14.216 s ± 0.450 s