From d09650e7227efca1d0a0bc0ca094b90f81be9497 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Thu, 6 Dec 2018 10:38:07 +0100 Subject: Refactor domain.BenchmarkReadGroups --- internal/domain/domain_config_test.go | 3 +- internal/domain/domain_test.go | 48 +++++++++++++++++++-------- internal/domain/map_test.go | 61 ++++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 38 deletions(-) (limited to 'internal/domain') diff --git a/internal/domain/domain_config_test.go b/internal/domain/domain_config_test.go index 05db0aa3..a89ead35 100644 --- a/internal/domain/domain_config_test.go +++ b/internal/domain/domain_config_test.go @@ -38,7 +38,8 @@ func TestDomainConfigValidness(t *testing.T) { } func TestDomainConfigRead(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() d := domainsConfig{} err := d.Read("test-group", "test-project") diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index 7482dbe7..a4ce6670 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -11,7 +11,6 @@ import ( "testing" "time" - log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -27,7 +26,8 @@ func serveFileOrNotFound(domain *D) http.HandlerFunc { } func TestGroupServeHTTP(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testGroup := &D{ projectName: "", @@ -66,7 +66,8 @@ func TestGroupServeHTTP(t *testing.T) { } func TestDomainServeHTTP(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testDomain := &D{ group: group{name: "group"}, @@ -219,7 +220,8 @@ func testHTTPGzip(t *testing.T, handler http.HandlerFunc, mode, url string, valu } func TestGroupServeHTTPGzip(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testGroup := &D{ projectName: "", @@ -290,7 +292,8 @@ func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, value } func TestGroup404ServeHTTP(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testGroup := &D{ projectName: "", @@ -318,7 +321,8 @@ func TestGroup404ServeHTTP(t *testing.T) { } func TestDomain404ServeHTTP(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testDomain := &D{ group: group{name: "group.404"}, @@ -333,7 +337,8 @@ func TestDomain404ServeHTTP(t *testing.T) { } func TestPredefined404ServeHTTP(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() testDomain := &D{ group: group{name: "group"}, @@ -388,6 +393,9 @@ func TestDomainCertificate(t *testing.T) { } func TestCacheControlHeaders(t *testing.T) { + cleanup := setUpTests(t) + defer cleanup() + testGroup := &D{ group: group{ name: "group", @@ -440,15 +448,27 @@ func TestOpenNoFollow(t *testing.T) { var chdirSet = false -func setUpTests() { +func setUpTests(t require.TestingT) func() { + return chdirInPath(t, "../../shared/pages") +} + +func chdirInPath(t require.TestingT, path string) func() { + noOp := func() {} if chdirSet { - return + return noOp } - err := os.Chdir("../../shared/pages") - if err != nil { - log.WithError(err).Print("chdir") - } else { - chdirSet = true + cwd, err := os.Getwd() + require.NoError(t, err, "Cannot Getwd") + + err = os.Chdir(path) + require.NoError(t, err, "Cannot Chdir") + + chdirSet = true + return func() { + err := os.Chdir(cwd) + require.NoError(t, err, "Cannot Chdir in cleanup") + + chdirSet = false } } diff --git a/internal/domain/map_test.go b/internal/domain/map_test.go index 2306edef..f7b065e4 100644 --- a/internal/domain/map_test.go +++ b/internal/domain/map_test.go @@ -30,7 +30,8 @@ func getEntriesForBenchmark(t *testing.B) godirwalk.Dirents { } func TestReadProjects(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() dm := make(Map) dm.ReadGroups("test.io", getEntries(t)) @@ -99,7 +100,8 @@ func writeRandomTimestamp(t *testing.T) { } func TestWatch(t *testing.T) { - setUpTests() + cleanup := setUpTests(t) + defer cleanup() require.NoError(t, os.RemoveAll(updateFile)) @@ -134,36 +136,49 @@ func recvTimeout(t *testing.T, ch <-chan Map) Map { } } -func BenchmarkReadGroups(b *testing.B) { +func buildFakeDomainsDirectory(t require.TestingT, nGroups, levels int) func() { testRoot, err := ioutil.TempDir("", "gitlab-pages-test") - require.NoError(b, err) - - cwd, err := os.Getwd() - require.NoError(b, err) - - defer func(oldWd, testWd string) { - os.Chdir(oldWd) - fmt.Printf("cleaning up test directory %s\n", testWd) - os.RemoveAll(testWd) - }(cwd, testRoot) - - require.NoError(b, os.Chdir(testRoot)) + require.NoError(t, err) - nGroups := 10000 - b.Logf("creating fake domains directory with %d groups", nGroups) for i := 0; i < nGroups; i++ { - for j := 0; j < 5; j++ { - dir := fmt.Sprintf("%s/group-%d/project-%d", testRoot, i, j) - require.NoError(b, os.MkdirAll(dir+"/public", 0755)) - - fakeConfig := fmt.Sprintf(`{"Domains":[{"Domain":"foo.%d.%d.example.io","Certificate":"bar","Key":"baz"}]}`, i, j) - require.NoError(b, ioutil.WriteFile(dir+"/config.json", []byte(fakeConfig), 0644)) + parent := fmt.Sprintf("%s/group-%d", testRoot, i) + domain := fmt.Sprintf("%d.example.io", i) + buildFakeProjectsDirectory(t, parent, domain) + for j := 0; j < levels; j++ { + parent = fmt.Sprintf("%s/sub", parent) + domain = fmt.Sprintf("%d.%s", j, domain) + buildFakeProjectsDirectory(t, parent, domain) } if i%100 == 0 { fmt.Print(".") } } + cleanup := chdirInPath(t, testRoot) + + return func() { + defer cleanup() + fmt.Printf("cleaning up test directory %s\n", testRoot) + os.RemoveAll(testRoot) + } +} + +func buildFakeProjectsDirectory(t require.TestingT, groupPath, domain string) { + for j := 0; j < 5; j++ { + dir := fmt.Sprintf("%s/project-%d", groupPath, j) + require.NoError(t, os.MkdirAll(dir+"/public", 0755)) + + fakeConfig := fmt.Sprintf(`{"Domains":[{"Domain":"foo.%d.%s","Certificate":"bar","Key":"baz"}]}`, j, domain) + require.NoError(t, ioutil.WriteFile(dir+"/config.json", []byte(fakeConfig), 0644)) + } +} + +func BenchmarkReadGroups(b *testing.B) { + nGroups := 10000 + b.Logf("creating fake domains directory with %d groups", nGroups) + cleanup := buildFakeDomainsDirectory(b, nGroups, 0) + defer cleanup() + b.Run("ReadGroups", func(b *testing.B) { var dm Map for i := 0; i < 2; i++ { -- cgit v1.2.3