Skip to content

Commit 739dc41

Browse files
committed
Fix BenchmarkRepoBranchCommit beside Create new Branch
1 parent 3e7d2a8 commit 739dc41

File tree

2 files changed

+70
-45
lines changed

2 files changed

+70
-45
lines changed

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/benchmarks_test.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
package integrations
66

77
import (
8-
"fmt"
8+
"math/rand"
99
"net/http"
10+
"net/url"
1011
"testing"
1112

1213
"code.gitea.io/gitea/models"
@@ -66,40 +67,58 @@ func BenchmarkRepo(b *testing.B) {
6667
}
6768
}
6869

70+
//StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
71+
func StringWithCharset(length int, charset string) string {
72+
b := make([]byte, length)
73+
for i := range b {
74+
b[i] = charset[rand.Intn(len(charset))]
75+
}
76+
return string(b)
77+
}
78+
6979
func BenchmarkRepoBranchCommit(b *testing.B) {
70-
b.Skip("benchmark broken") // TODO fix
71-
samples := []int64{1, 15, 16}
72-
defer prepareTestEnv(b)()
73-
b.ResetTimer()
80+
onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
81+
b := t.(*testing.B)
7482

75-
for _, repoID := range samples {
76-
b.StopTimer()
77-
repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
78-
b.StartTimer()
79-
b.Run(repo.Name, func(b *testing.B) {
80-
owner := models.AssertExistsAndLoadBean(b, &models.User{ID: repo.OwnerID}).(*models.User)
81-
session := loginUser(b, owner.LoginName)
82-
b.ResetTimer()
83-
b.Run("CreateBranch", func(b *testing.B) {
84-
for i := 0; i < b.N; i++ {
85-
testCreateBranch(b, session, owner.LoginName, repo.Name, "branch/master", fmt.Sprintf("new_branch_nr%d", i), http.StatusFound)
86-
}
87-
})
88-
b.Run("AccessBranchCommits", func(b *testing.B) {
89-
var branches []*api.Branch
90-
req := NewRequestf(b, "GET", "/api/v1/%s/branches", repo.FullName())
91-
resp := session.MakeRequest(b, req, http.StatusOK)
92-
DecodeJSON(b, resp, &branches)
93-
b.ResetTimer() //We measure from here
94-
if len(branches) != 0 {
83+
samples := []int64{1, 2, 3}
84+
b.ResetTimer()
85+
86+
for _, repoID := range samples {
87+
b.StopTimer()
88+
repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
89+
b.StartTimer()
90+
b.Run(repo.Name, func(b *testing.B) {
91+
session := loginUser(b, "user2")
92+
b.ResetTimer()
93+
b.Run("CreateBranch", func(b *testing.B) {
94+
b.Skip("benchmark broken") // TODO fix
95+
b.StopTimer()
96+
branchName := "new_" + StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
97+
b.StartTimer()
9598
for i := 0; i < b.N; i++ {
96-
req := NewRequestf(b, "GET", "/api/v1/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
97-
session.MakeRequest(b, req, http.StatusOK)
99+
b.Run(branchName, func(b *testing.B) {
100+
testAPICreateBranch(b, session, repo.OwnerName, repo.Name, repo.DefaultBranch, branchName, http.StatusCreated)
101+
})
98102
}
99-
}
103+
})
104+
b.Run("GetBranches", func(b *testing.B) {
105+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
106+
session.MakeRequest(b, req, http.StatusOK)
107+
})
108+
b.Run("AccessCommits", func(b *testing.B) {
109+
var branches []*api.Branch
110+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
111+
resp := session.MakeRequest(b, req, http.StatusOK)
112+
DecodeJSON(b, resp, &branches)
113+
b.ResetTimer() //We measure from here
114+
if len(branches) != 0 {
115+
for i := 0; i < b.N; i++ {
116+
req := NewRequestf(b, "GET", "/api/v1/repos/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
117+
session.MakeRequest(b, req, http.StatusOK)
118+
}
119+
}
120+
})
100121
})
101-
})
102-
}
122+
}
123+
})
103124
}
104-
105-
//TODO list commits /repos/{owner}/{repo}/commits

0 commit comments

Comments
 (0)