diff --git a/Makefile b/Makefile index 683b8e522..7d92d719f 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,10 @@ lint: test: for PKG in $(PACKAGES); do go test -cover -coverprofile $$GOPATH/src/$$PKG/coverage.out $$PKG || exit 1; done; +.PHONY: bench +bench: + go test -run=XXXXXX -benchtime=10s -bench=. || exit 1 + .PHONY: build build: go build . diff --git a/tree_entry_test.go b/tree_entry_test.go new file mode 100644 index 000000000..166ddca74 --- /dev/null +++ b/tree_entry_test.go @@ -0,0 +1,67 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "io/ioutil" + "os" + "testing" +) + +func setupGitRepo(url string) (string, error) { + dir, err := ioutil.TempDir("", "gitea-bench") + if err != nil { + return "", err + } + err = Clone(url, dir, CloneRepoOptions{}) + if err != nil { + return "", err + } + return dir, nil +} + +//TODO use https://blog.golang.org/subtests when removing support for Go1.6 +func benchmarkGetCommitsInfo(url string, b *testing.B) { + b.StopTimer() + + // setup env + repoPath, err := setupGitRepo(url) + if err != nil { + b.Fatal(err) + } + defer os.RemoveAll(repoPath) + + repo, err := OpenRepository(repoPath) + if err != nil { + b.Fatal(err) + } + + commit, err := repo.GetBranchCommit("master") + if err != nil { + b.Fatal(err) + } + + entries, err := commit.Tree.ListEntries() + if err != nil { + b.Fatal(err) + } + entries.Sort() + + b.StartTimer() + // run the GetCommitsInfo function b.N times + for n := 0; n < b.N; n++ { + _, err = entries.GetCommitsInfo(commit, "") + if err != nil { + b.Fatal(err) + } + } +} + + +func BenchmarkGetCommitsInfoGitea(b *testing.B) { benchmarkGetCommitsInfo("https://github.com/go-gitea/gitea.git", b) } //5k+ commits +func BenchmarkGetCommitsInfoMoby(b *testing.B) { benchmarkGetCommitsInfo("https://github.com/moby/moby.git", b) } //32k+ commits +func BenchmarkGetCommitsInfoGo(b *testing.B) { benchmarkGetCommitsInfo("https://github.com/golang/go.git", b) } //32k+ commits +func BenchmarkGetCommitsInfoLinux(b *testing.B) { benchmarkGetCommitsInfo("https://github.com/torvalds/linux.git", b) } //677k+ commits +func BenchmarkGetCommitsInfoManyFile(b *testing.B) { benchmarkGetCommitsInfo("https://github.com/ethantkoenig/manyfiles", b) }