|
| 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 |
0 commit comments