From fba9a8116d2fd3a8ef61d06db679174380db546f Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Wed, 24 May 2017 12:07:26 +0200 Subject: [PATCH 1/2] Add bench task --- Makefile | 4 +++ tree_entry_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tree_entry_test.go 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..6463260f1 --- /dev/null +++ b/tree_entry_test.go @@ -0,0 +1,70 @@ +// 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 { + dir, err := ioutil.TempDir("", "gitea-bench") + if err != nil { + panic(err) + } + /* Manual method + _, err = NewCommand("clone", url, dir).Run() + if err != nil { + log.Fatal(err) + } + */ + err = Clone(url, dir, CloneRepoOptions{}) + if err != nil { + panic(err) + } + return dir +} + +//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 := setupGitRepo(url) + defer os.RemoveAll(repoPath) + + repo, err := OpenRepository(repoPath) + if err != nil { + panic(err) + } + + commit, err := repo.GetBranchCommit("master") + if err != nil { + panic(err) + } + + entries, err := commit.Tree.ListEntries() + if err != nil { + panic(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 { + panic(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) } From 565d1798f6528d52a1ca039c0ea671bebb0f5d16 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 26 May 2017 00:32:40 +0200 Subject: [PATCH 2/2] Use b.Fatal in place of panic --- tree_entry_test.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/tree_entry_test.go b/tree_entry_test.go index 6463260f1..166ddca74 100644 --- a/tree_entry_test.go +++ b/tree_entry_test.go @@ -10,22 +10,16 @@ import ( "testing" ) -func setupGitRepo(url string) string { +func setupGitRepo(url string) (string, error) { dir, err := ioutil.TempDir("", "gitea-bench") if err != nil { - panic(err) + return "", err } - /* Manual method - _, err = NewCommand("clone", url, dir).Run() - if err != nil { - log.Fatal(err) - } - */ err = Clone(url, dir, CloneRepoOptions{}) if err != nil { - panic(err) + return "", err } - return dir + return dir, nil } //TODO use https://blog.golang.org/subtests when removing support for Go1.6 @@ -33,22 +27,25 @@ func benchmarkGetCommitsInfo(url string, b *testing.B) { b.StopTimer() // setup env - repoPath := setupGitRepo(url) + repoPath, err := setupGitRepo(url) + if err != nil { + b.Fatal(err) + } defer os.RemoveAll(repoPath) repo, err := OpenRepository(repoPath) if err != nil { - panic(err) + b.Fatal(err) } commit, err := repo.GetBranchCommit("master") if err != nil { - panic(err) + b.Fatal(err) } entries, err := commit.Tree.ListEntries() if err != nil { - panic(err) + b.Fatal(err) } entries.Sort() @@ -57,7 +54,7 @@ func benchmarkGetCommitsInfo(url string, b *testing.B) { for n := 0; n < b.N; n++ { _, err = entries.GetCommitsInfo(commit, "") if err != nil { - panic(err) + b.Fatal(err) } } }