|
5 | 5 | package integrations
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "fmt" |
| 8 | + "math/rand" |
9 | 9 | "net/http"
|
| 10 | + "net/url" |
10 | 11 | "testing"
|
11 | 12 |
|
12 | 13 | "code.gitea.io/gitea/models"
|
@@ -66,40 +67,58 @@ func BenchmarkRepo(b *testing.B) {
|
66 | 67 | }
|
67 | 68 | }
|
68 | 69 |
|
| 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 | + |
69 | 79 | 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) |
74 | 82 |
|
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() |
95 | 98 | 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 | + }) |
98 | 102 | }
|
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 | + }) |
100 | 121 | })
|
101 |
| - }) |
102 |
| - } |
| 122 | + } |
| 123 | + }) |
103 | 124 | }
|
104 |
| - |
105 |
| -//TODO list commits /repos/{owner}/{repo}/commits |
|
0 commit comments