-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore [rebased for main] #2600
feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore [rebased for main] #2600
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. |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's update the docs path:
[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 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 :)
…rs-go into postgres-snapshot-sql-rebased-main
Looks good to me! :) Thanks for making the change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
* 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)
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.DB
connection.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 exec
can 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