feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore#2599
feat(postgres): use faster sql.DB instead of docker exec psql for snapshot/restore#2599cfstras wants to merge 6 commits intotestcontainers:v1from
Conversation
…reating & restoring snapshots
Remove all commented lines
…hat don't register as "postgres"
|
@mdelapenya |
|
Thanks for submitting this PR, I'll take a look asap. I'd prefer targeting everything to |
|
Hi @mdelapenya, thanks for your response :) I've re-based onto The benchmarks are run with hyperfine, which is a great tool indeed :) |
|
If you mind, let's close this one in favor of #2600, thanks! |
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 my suite of 20 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.
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:
cfstras@9ad456d: