diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-12 13:20:35 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-15 09:54:06 +0300 |
commit | bc46fdc80491e01d196aa7c2337002da180289ee (patch) | |
tree | bf15684bf8895fbec8c7fe8b0a3aa88497e5c30f | |
parent | 4eaaec27644aea6b768c8944732cd95c6d385400 (diff) |
housekeeping: Speed up tests writing large packfiles via truncate(3p)
Some of our tests in the housekeeping package need to verify whether we
correctly compute the cutoff point when we want to do a full repack
based on the size of the largest packfile. We thus have to write such a
packfile into our temporary directory, which we're currently doing by
using some implementation that use `io.Reader`. This is inefficient, and
the tests spend a significant amount of time to just write these files.
Simplify the code and speed it up by just using truncate(3p) instead.
This speeds up the test from around 1s to 0.35s on my computer.
-rw-r--r-- | internal/git/housekeeping/optimize_repository_test.go | 18 |
1 files changed, 3 insertions, 15 deletions
diff --git a/internal/git/housekeeping/optimize_repository_test.go b/internal/git/housekeeping/optimize_repository_test.go index 473c17341..096394d4f 100644 --- a/internal/git/housekeeping/optimize_repository_test.go +++ b/internal/git/housekeeping/optimize_repository_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io" "os" "path/filepath" "strings" @@ -26,15 +25,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) -type infiniteReader struct{} - -func (r infiniteReader) Read(b []byte) (int, error) { - for i := range b { - b[i] = '\000' - } - return len(b), nil -} - func TestNeedsRepacking(t *testing.T) { t.Parallel() @@ -193,11 +183,9 @@ func TestNeedsRepacking(t *testing.T) { // We first create a single big packfile which is used to determine the // boundary of when we repack. - bigPackfile, err := os.OpenFile(filepath.Join(packDir, "big.pack"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644) - require.NoError(t, err) - defer testhelper.MustClose(t, bigPackfile) - _, err = io.Copy(bigPackfile, io.LimitReader(infiniteReader{}, tc.packfileSize)) - require.NoError(t, err) + bigPackPath := filepath.Join(packDir, "big.pack") + require.NoError(t, os.WriteFile(bigPackPath, nil, 0o644)) + require.NoError(t, os.Truncate(bigPackPath, tc.packfileSize)) // And then we create one less packfile than we need to hit the boundary. // This is done to assert that we indeed don't repack before hitting the |