code.gitea.io/gitea@v1.22.3/tests/integration/benchmarks_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"math/rand"
     8  	"net/http"
     9  	"net/url"
    10  	"testing"
    11  
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  	"code.gitea.io/gitea/models/unittest"
    14  	api "code.gitea.io/gitea/modules/structs"
    15  )
    16  
    17  // StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
    18  func StringWithCharset(length int, charset string) string {
    19  	b := make([]byte, length)
    20  	for i := range b {
    21  		b[i] = charset[rand.Intn(len(charset))]
    22  	}
    23  	return string(b)
    24  }
    25  
    26  func BenchmarkRepoBranchCommit(b *testing.B) {
    27  	onGiteaRun(b, func(b *testing.B, u *url.URL) {
    28  		samples := []int64{1, 2, 3}
    29  		b.ResetTimer()
    30  
    31  		for _, repoID := range samples {
    32  			b.StopTimer()
    33  			repo := unittest.AssertExistsAndLoadBean(b, &repo_model.Repository{ID: repoID})
    34  			b.StartTimer()
    35  			b.Run(repo.Name, func(b *testing.B) {
    36  				session := loginUser(b, "user2")
    37  				b.ResetTimer()
    38  				b.Run("CreateBranch", func(b *testing.B) {
    39  					b.StopTimer()
    40  					branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    41  					b.StartTimer()
    42  					for i := 0; i < b.N; i++ {
    43  						b.Run("new_"+branchName, func(b *testing.B) {
    44  							b.Skip("benchmark broken") // TODO fix
    45  							testAPICreateBranch(b, session, repo.OwnerName, repo.Name, repo.DefaultBranch, "new_"+branchName, http.StatusCreated)
    46  						})
    47  					}
    48  				})
    49  				b.Run("GetBranches", func(b *testing.B) {
    50  					req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
    51  					session.MakeRequest(b, req, http.StatusOK)
    52  				})
    53  				b.Run("AccessCommits", func(b *testing.B) {
    54  					var branches []*api.Branch
    55  					req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
    56  					resp := session.MakeRequest(b, req, http.StatusOK)
    57  					DecodeJSON(b, resp, &branches)
    58  					b.ResetTimer() // We measure from here
    59  					if len(branches) != 0 {
    60  						for i := 0; i < b.N; i++ {
    61  							req := NewRequestf(b, "GET", "/api/v1/repos/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
    62  							session.MakeRequest(b, req, http.StatusOK)
    63  						}
    64  					}
    65  				})
    66  			})
    67  		}
    68  	})
    69  }