diff --git a/Makefile.util.mk b/Makefile.util.mk index 18f6b0ff4986a95d140eee277761f292b7913c7c..98e2158effa20af4a14d0f5eb1af49eee5a1204c 100644 --- a/Makefile.util.mk +++ b/Makefile.util.mk @@ -17,6 +17,9 @@ complexity: .GOPATH/.ok bin/gocyclo test: .GOPATH/.ok gitlab-pages go test $(if $V,-v) -timeout=1m $(allpackages) +bench: .GOPATH/.ok gitlab-pages + go test -bench=. $(allpackages) + # The acceptance tests cannot count for coverage cover: bin/gocovmerge .GOPATH/.ok @echo "NOTE: make cover does not exit 1 on failure, don't use it to check for tests success!" diff --git a/domain_config.go b/domain_config.go index e0f00cc49869fd37dbd44ebccefde96a8fa8fb42..08601f669460a5cc6307edeaf8bf26db484f9dc3 100644 --- a/domain_config.go +++ b/domain_config.go @@ -29,7 +29,7 @@ func (c *domainConfig) Valid(rootDomain string) bool { } func (c *domainsConfig) Read(group, project string) (err error) { - configFile, err := os.Open(filepath.Join(group, project, "config.json")) + configFile, err := os.Open(filepath.Join(domainRoot, group, project, "config.json")) if err != nil { return err } diff --git a/domains.go b/domains.go index 000aa42e9ef01d51541b68ec28844d0dadfefe6d..5ec27e3c0490f4ad572db11c7bf1cd775bb97bb3 100644 --- a/domains.go +++ b/domains.go @@ -62,7 +62,7 @@ func (d domains) readProject(rootDomain, group, project string) error { return errors.New("deleted project") } - _, err := os.Lstat(filepath.Join(group, project, "public")) + _, err := os.Lstat(filepath.Join(domainRoot, group, project, "public")) if err != nil { return errors.New("missing public/ in project") } @@ -72,7 +72,7 @@ func (d domains) readProject(rootDomain, group, project string) error { } func (d domains) readProjects(rootDomain, group string) (count int) { - projects, err := os.Open(group) + projects, err := os.Open(filepath.Join(domainRoot, group)) if err != nil { return } @@ -97,8 +97,10 @@ func (d domains) readProjects(rootDomain, group string) (count int) { return } +var domainRoot = "." + func (d domains) ReadGroups(rootDomain string) error { - groups, err := os.Open(".") + groups, err := os.Open(domainRoot) if err != nil { return err } diff --git a/domains_test.go b/domains_test.go index 51bafc9f8426800ee6b00bf82f9b89d66e1332ae..9cd3f97eccd6db21f3ff191c131839ab82539245 100644 --- a/domains_test.go +++ b/domains_test.go @@ -2,6 +2,7 @@ package main import ( "crypto/rand" + "fmt" "io/ioutil" "os" "testing" @@ -81,3 +82,34 @@ func TestWatchDomains(t *testing.T) { domains = <-update assert.NotNil(t, domains, "if the domains are updated after the timestamp removal") } + +func BenchmarkReadGroups(b *testing.B) { + testRoot, err := ioutil.TempDir("", "gitlab-pages-test") + require.NoError(b, err) + defer os.RemoveAll(testRoot) + + defer func(d string) { + domainRoot = d + }(domainRoot) + domainRoot = testRoot + + nGroups := 10000 + b.Logf("creating fake domains directory with %d groups", nGroups) + fakeConfig := []byte(`{"Domains":[{"Domain":"foo","Certificate":"bar","Key":"baz"}]}`) + for i := 0; i < nGroups; i++ { + for j := 0; j < 5; j++ { + dir := fmt.Sprintf("%s/group-%d/project-%d/public", testRoot, i, j) + require.NoError(b, os.MkdirAll(dir, 0755)) + require.NoError(b, ioutil.WriteFile(dir+"/config.json", fakeConfig, 0644)) + } + if i%100 == 0 { + fmt.Print(".") + } + } + + b.Run("ReadGroups", func(b *testing.B) { + testDomains := domains(make(map[string]*domain)) + require.NoError(b, testDomains.ReadGroups("example.com")) + b.Logf("found %d domains", len(testDomains)) + }) +}