Skip to content

Commit 77e169a

Browse files
committed
Improve makefile + Add benchs
1 parent 0e384c6 commit 77e169a

File tree

5 files changed

+136
-37
lines changed

5 files changed

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