Skip to content

Commit cc7b8e3

Browse files
sapklafriks
authored andcommitted
Add more bench (#3161)
* Improve makefile + Add benchs * Apply recommendations of @ethantkoenig
1 parent a995ad9 commit cc7b8e3

File tree

5 files changed

+124
-37
lines changed

5 files changed

+124
-37
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ test-pgsql: integrations.test generate-ini
183183

184184
.PHONY: bench-sqlite
185185
bench-sqlite: integrations.sqlite.test
186-
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.bench .
186+
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
187187

188188
.PHONY: bench-mysql
189189
bench-mysql: integrations.test generate-ini
190-
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.bench .
190+
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
191191

192192
.PHONY: bench-pgsql
193193
bench-pgsql: integrations.test generate-ini
194-
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.bench .
194+
GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
195195

196196

197197
.PHONY: integration-test-coverage

integrations/benchmarks_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"math/rand"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
api "code.gitea.io/sdk/gitea"
14+
)
15+
16+
func BenchmarkRepo(b *testing.B) {
17+
samples := []struct {
18+
url string
19+
name string
20+
skipShort bool
21+
}{
22+
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
23+
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
24+
{url: "https://github.com/moby/moby.git", name: "moby", skipShort: true},
25+
{url: "https://github.com/golang/go.git", name: "go", skipShort: true},
26+
{url: "https://github.com/torvalds/linux.git", name: "linux", skipShort: true},
27+
}
28+
prepareTestEnv(b)
29+
session := loginUser(b, "user2")
30+
b.ResetTimer()
31+
32+
for _, s := range samples {
33+
b.Run(s.name, func(b *testing.B) {
34+
if testing.Short() && s.skipShort {
35+
b.Skip("skipping test in short mode.")
36+
}
37+
b.Run("Migrate", func(b *testing.B) {
38+
for i := 0; i < b.N; i++ {
39+
testRepoMigrate(b, session, s.url, s.name)
40+
}
41+
})
42+
b.Run("Access", func(b *testing.B) {
43+
var branches []*api.Branch
44+
b.Run("APIBranchList", func(b *testing.B) {
45+
for i := 0; i < b.N; i++ {
46+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/%s/branches", "user2", s.name)
47+
resp := session.MakeRequest(b, req, http.StatusOK)
48+
b.StopTimer()
49+
if len(branches) == 0 {
50+
DecodeJSON(b, resp, &branches) //Store for next phase
51+
}
52+
b.StartTimer()
53+
}
54+
})
55+
branchCount := len(branches)
56+
b.Run("WebViewCommit", func(b *testing.B) {
57+
for i := 0; i < b.N; i++ {
58+
req := NewRequestf(b, "GET", "/%s/%s/commit/%s", "user2", s.name, branches[i%branchCount].Commit.ID)
59+
session.MakeRequest(b, req, http.StatusOK)
60+
}
61+
})
62+
})
63+
})
64+
}
65+
}
66+
67+
//StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
68+
func StringWithCharset(length int, charset string) string {
69+
b := make([]byte, length)
70+
for i := range b {
71+
b[i] = charset[rand.Intn(len(charset))]
72+
}
73+
return string(b)
74+
}
75+
76+
func BenchmarkRepoBranchCommit(b *testing.B) {
77+
samples := []int64{1, 3, 15, 16}
78+
prepareTestEnv(b)
79+
b.ResetTimer()
80+
81+
for _, repoID := range samples {
82+
b.StopTimer()
83+
repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
84+
b.StartTimer()
85+
b.Run(repo.Name, func(b *testing.B) {
86+
owner := models.AssertExistsAndLoadBean(b, &models.User{ID: repo.OwnerID}).(*models.User)
87+
session := loginUser(b, owner.LoginName)
88+
b.ResetTimer()
89+
b.Run("Create", func(b *testing.B) {
90+
for i := 0; i < b.N; i++ {
91+
b.StopTimer()
92+
branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
93+
b.StartTimer()
94+
testCreateBranch(b, session, owner.LoginName, repo.Name, "branch/master", branchName, http.StatusFound)
95+
}
96+
})
97+
b.Run("Access", func(b *testing.B) {
98+
var branches []*api.Branch
99+
req := NewRequestf(b, "GET", "/api/v1/%s/branches", repo.FullName())
100+
resp := session.MakeRequest(b, req, http.StatusOK)
101+
DecodeJSON(b, resp, &branches)
102+
branchCount := len(branches)
103+
b.ResetTimer() //We measure from here
104+
for i := 0; i < b.N; i++ {
105+
req := NewRequestf(b, "GET", "/%s/%s/commits/%s", owner.Name, repo.Name, branches[i%branchCount])
106+
session.MakeRequest(b, req, http.StatusOK)
107+
}
108+
})
109+
})
110+
}
111+
}
112+
113+
//TODO list commits /repos/{owner}/{repo}/commits

integrations/repo_branch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19-
func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
19+
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
2020
var csrf string
2121
if expectedStatus == http.StatusNotFound {
2222
csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))

integrations/repo_migrate_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,3 @@ func TestRepoMigrate(t *testing.T) {
4040
session := loginUser(t, "user2")
4141
testRepoMigrate(t, session, "https://github.com/go-gitea/git.git", "git")
4242
}
43-
44-
func BenchmarkRepoMigrate(b *testing.B) {
45-
samples := []struct {
46-
url string
47-
name string
48-
}{
49-
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
50-
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
51-
{url: "https://github.com/moby/moby.git", name: "moby"},
52-
{url: "https://github.com/golang/go.git", name: "go"},
53-
{url: "https://github.com/torvalds/linux.git", name: "linux"},
54-
}
55-
56-
prepareTestEnv(b)
57-
session := loginUser(b, "user2")
58-
b.ResetTimer()
59-
60-
for _, s := range samples {
61-
b.Run(s.name, func(b *testing.B) {
62-
for i := 0; i < b.N; i++ {
63-
testRepoMigrate(b, session, s.url, s.name)
64-
}
65-
66-
})
67-
}
68-
}

models/unit_tests.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
115115
}
116116

117117
// BeanExists for testing, check if a bean exists
118-
func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
118+
func BeanExists(t testing.TB, bean interface{}, conditions ...interface{}) bool {
119119
exists, err := loadBeanIfExists(bean, conditions...)
120120
assert.NoError(t, err)
121121
return exists
122122
}
123123

124124
// AssertExistsAndLoadBean assert that a bean exists and load it from the test
125125
// database
126-
func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
126+
func AssertExistsAndLoadBean(t testing.TB, bean interface{}, conditions ...interface{}) interface{} {
127127
exists, err := loadBeanIfExists(bean, conditions...)
128128
assert.NoError(t, err)
129129
assert.True(t, exists,
@@ -133,7 +133,7 @@ func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...inter
133133
}
134134

135135
// GetCount get the count of a bean
136-
func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
136+
func GetCount(t testing.TB, bean interface{}, conditions ...interface{}) int {
137137
sess := x.NewSession()
138138
defer sess.Close()
139139
whereConditions(sess, conditions)
@@ -143,7 +143,7 @@ func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
143143
}
144144

145145
// AssertNotExistsBean assert that a bean does not exist in the test database
146-
func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
146+
func AssertNotExistsBean(t testing.TB, bean interface{}, conditions ...interface{}) {
147147
exists, err := loadBeanIfExists(bean, conditions...)
148148
assert.NoError(t, err)
149149
assert.False(t, exists)
@@ -158,18 +158,18 @@ func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ..
158158
}
159159

160160
// AssertSuccessfulInsert assert that beans is successfully inserted
161-
func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
161+
func AssertSuccessfulInsert(t testing.TB, beans ...interface{}) {
162162
_, err := x.Insert(beans...)
163163
assert.NoError(t, err)
164164
}
165165

166166
// AssertCount assert the count of a bean
167-
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
167+
func AssertCount(t testing.TB, bean interface{}, expected interface{}) {
168168
assert.EqualValues(t, expected, GetCount(t, bean))
169169
}
170170

171171
// AssertInt64InRange assert value is in range [low, high]
172-
func AssertInt64InRange(t *testing.T, low, high, value int64) {
172+
func AssertInt64InRange(t testing.TB, low, high, value int64) {
173173
assert.True(t, value >= low && value <= high,
174174
"Expected value in range [%d, %d], found %d", low, high, value)
175175
}

0 commit comments

Comments
 (0)