Skip to content

Commit a67861b

Browse files
authored
Fix Benchmark tests, remove a broken one & add two new (#15250)
* Benchmark Integration TESTS * CI: add benching-arm64 pipeline * BenchmarkRepo: name test case tests * Fix BenchmarkRepoBranchCommit beside Create new Branch * CI: benching use amd64 * rm total broken "BenchmarkRepo" * dont run benchmark in CI
1 parent c29e852 commit a67861b

6 files changed

+100
-99
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ _testmain.go
3232

3333
*coverage.out
3434
coverage.all
35+
cpu.out
3536

3637
/modules/options/bindata.go
3738
/modules/options/bindata.go.hash

integrations/api_branch_test.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,28 @@ func testAPICreateBranches(t *testing.T, giteaURL *url.URL) {
151151
for _, test := range tests {
152152
defer resetFixtures(t)
153153
session := ctx.Session
154-
token := getTokenForLoggedInUser(t, session)
155-
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/my-noo-repo/branches?token="+token, &api.CreateBranchRepoOption{
156-
BranchName: test.NewBranch,
157-
OldBranchName: test.OldBranch,
158-
})
159-
resp := session.MakeRequest(t, req, test.ExpectedHTTPStatus)
160-
161-
var branch api.Branch
162-
DecodeJSON(t, resp, &branch)
163-
164-
if test.ExpectedHTTPStatus == http.StatusCreated {
165-
assert.EqualValues(t, test.NewBranch, branch.Name)
166-
}
154+
testAPICreateBranch(t, session, "user2", "my-noo-repo", test.OldBranch, test.NewBranch, test.ExpectedHTTPStatus)
167155
}
168156
}
169157

158+
func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBranch, newBranch string, status int) bool {
159+
token := getTokenForLoggedInUser(t, session)
160+
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/"+user+"/"+repo+"/branches?token="+token, &api.CreateBranchRepoOption{
161+
BranchName: newBranch,
162+
OldBranchName: oldBranch,
163+
})
164+
resp := session.MakeRequest(t, req, status)
165+
166+
var branch api.Branch
167+
DecodeJSON(t, resp, &branch)
168+
169+
if status == http.StatusCreated {
170+
assert.EqualValues(t, newBranch, branch.Name)
171+
}
172+
173+
return resp.Result().StatusCode == status
174+
}
175+
170176
func TestAPIBranchProtection(t *testing.T) {
171177
defer prepareTestEnv(t)()
172178

integrations/api_repo_file_create_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,36 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
105105
}
106106
}
107107

108+
func BenchmarkAPICreateFileSmall(b *testing.B) {
109+
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
110+
b := t.(*testing.B)
111+
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
112+
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
113+
114+
for n := 0; n < b.N; n++ {
115+
treePath := fmt.Sprintf("update/file%d.txt", n)
116+
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
117+
}
118+
})
119+
}
120+
121+
func BenchmarkAPICreateFileMedium(b *testing.B) {
122+
data := make([]byte, 10*1024*1024)
123+
124+
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
125+
b := t.(*testing.B)
126+
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
127+
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
128+
129+
b.ResetTimer()
130+
for n := 0; n < b.N; n++ {
131+
treePath := fmt.Sprintf("update/file%d.txt", n)
132+
copy(data, treePath)
133+
createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
134+
}
135+
})
136+
}
137+
108138
func TestAPICreateFile(t *testing.T) {
109139
onGiteaRun(t, func(t *testing.T, u *url.URL) {
110140
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16

integrations/api_repo_file_helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
api "code.gitea.io/gitea/modules/structs"
1111
)
1212

13-
func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName string) (*api.FileResponse, error) {
13+
func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName, content string) (*api.FileResponse, error) {
1414
opts := &repofiles.UpdateRepoFileOptions{
1515
OldBranch: branchName,
1616
TreePath: treePath,
17-
Content: "This is a NEW file",
17+
Content: content,
1818
IsNewFile: true,
1919
Author: nil,
2020
Committer: nil,
@@ -23,5 +23,5 @@ func createFileInBranch(user *models.User, repo *models.Repository, treePath, br
2323
}
2424

2525
func createFile(user *models.User, repo *models.Repository, treePath string) (*api.FileResponse, error) {
26-
return createFileInBranch(user, repo, treePath, repo.DefaultBranch)
26+
return createFileInBranch(user, repo, treePath, repo.DefaultBranch, "This is a NEW file")
2727
}

integrations/benchmarks_test.go

Lines changed: 40 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,14 @@ package integrations
77
import (
88
"math/rand"
99
"net/http"
10+
"net/url"
1011
"testing"
1112

1213
"code.gitea.io/gitea/models"
1314
api "code.gitea.io/gitea/modules/structs"
1415
)
1516

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-
defer 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/)
17+
// StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
6818
func StringWithCharset(length int, charset string) string {
6919
b := make([]byte, length)
7020
for i := range b {
@@ -74,40 +24,48 @@ func StringWithCharset(length int, charset string) string {
7424
}
7525

7626
func BenchmarkRepoBranchCommit(b *testing.B) {
77-
samples := []int64{1, 3, 15, 16}
78-
defer prepareTestEnv(b)()
79-
b.ResetTimer()
27+
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
28+
b := t.(*testing.B)
29+
30+
samples := []int64{1, 2, 3}
31+
b.ResetTimer()
8032

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++ {
33+
for _, repoID := range samples {
34+
b.StopTimer()
35+
repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
36+
b.StartTimer()
37+
b.Run(repo.Name, func(b *testing.B) {
38+
session := loginUser(b, "user2")
39+
b.ResetTimer()
40+
b.Run("CreateBranch", func(b *testing.B) {
9141
b.StopTimer()
9242
branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
9343
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].Name)
44+
for i := 0; i < b.N; i++ {
45+
b.Run("new_"+branchName, func(b *testing.B) {
46+
b.Skip("benchmark broken") // TODO fix
47+
testAPICreateBranch(b, session, repo.OwnerName, repo.Name, repo.DefaultBranch, "new_"+branchName, http.StatusCreated)
48+
})
49+
}
50+
})
51+
b.Run("GetBranches", func(b *testing.B) {
52+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
10653
session.MakeRequest(b, req, http.StatusOK)
107-
}
54+
})
55+
b.Run("AccessCommits", func(b *testing.B) {
56+
var branches []*api.Branch
57+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
58+
resp := session.MakeRequest(b, req, http.StatusOK)
59+
DecodeJSON(b, resp, &branches)
60+
b.ResetTimer() //We measure from here
61+
if len(branches) != 0 {
62+
for i := 0; i < b.N; i++ {
63+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
64+
session.MakeRequest(b, req, http.StatusOK)
65+
}
66+
}
67+
})
10868
})
109-
})
110-
}
69+
}
70+
})
11171
}
112-
113-
//TODO list commits /repos/{owner}/{repo}/commits

integrations/git_helper_for_declarative_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func allowLFSFilters() []string {
7676
return filteredLFSGlobalArgs[:j]
7777
}
7878

79-
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
79+
func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
8080
if len(prepare) == 0 || prepare[0] {
8181
defer prepareTestEnv(t, 1)()
8282
}
@@ -108,6 +108,12 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
108108
callback(t, u)
109109
}
110110

111+
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
112+
onGiteaRunTB(t, func(t testing.TB, u *url.URL) {
113+
callback(t.(*testing.T), u)
114+
}, prepare...)
115+
}
116+
111117
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
112118
return func(t *testing.T) {
113119
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))

0 commit comments

Comments
 (0)