mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Add more bench (#3161)
* Improve makefile + Add benchs * Apply recommendations of @ethantkoenig
This commit is contained in:
		
				
					committed by
					
						
						Lauris BH
					
				
			
			
				
	
			
			
			
						parent
						
							a995ad90e1
						
					
				
				
					commit
					cc7b8e3379
				
			
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -183,15 +183,15 @@ test-pgsql: integrations.test generate-ini
 | 
			
		||||
 | 
			
		||||
.PHONY: bench-sqlite
 | 
			
		||||
bench-sqlite: integrations.sqlite.test
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.bench .
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
 | 
			
		||||
 | 
			
		||||
.PHONY: bench-mysql
 | 
			
		||||
bench-mysql: integrations.test generate-ini
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.bench .
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
 | 
			
		||||
 | 
			
		||||
.PHONY: bench-pgsql
 | 
			
		||||
bench-pgsql: integrations.test generate-ini
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.bench .
 | 
			
		||||
	GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.cpuprofile=cpu.out -test.run DontRunTests -test.bench .
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.PHONY: integration-test-coverage
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										113
									
								
								integrations/benchmarks_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								integrations/benchmarks_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
			
		||||
// Copyright 2017 The Gitea Authors. All rights reserved.
 | 
			
		||||
// Use of this source code is governed by a MIT-style
 | 
			
		||||
// license that can be found in the LICENSE file.
 | 
			
		||||
 | 
			
		||||
package integrations
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	api "code.gitea.io/sdk/gitea"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func BenchmarkRepo(b *testing.B) {
 | 
			
		||||
	samples := []struct {
 | 
			
		||||
		url       string
 | 
			
		||||
		name      string
 | 
			
		||||
		skipShort bool
 | 
			
		||||
	}{
 | 
			
		||||
		{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
 | 
			
		||||
		{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
 | 
			
		||||
		{url: "https://github.com/moby/moby.git", name: "moby", skipShort: true},
 | 
			
		||||
		{url: "https://github.com/golang/go.git", name: "go", skipShort: true},
 | 
			
		||||
		{url: "https://github.com/torvalds/linux.git", name: "linux", skipShort: true},
 | 
			
		||||
	}
 | 
			
		||||
	prepareTestEnv(b)
 | 
			
		||||
	session := loginUser(b, "user2")
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	for _, s := range samples {
 | 
			
		||||
		b.Run(s.name, func(b *testing.B) {
 | 
			
		||||
			if testing.Short() && s.skipShort {
 | 
			
		||||
				b.Skip("skipping test in short mode.")
 | 
			
		||||
			}
 | 
			
		||||
			b.Run("Migrate", func(b *testing.B) {
 | 
			
		||||
				for i := 0; i < b.N; i++ {
 | 
			
		||||
					testRepoMigrate(b, session, s.url, s.name)
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
			b.Run("Access", func(b *testing.B) {
 | 
			
		||||
				var branches []*api.Branch
 | 
			
		||||
				b.Run("APIBranchList", func(b *testing.B) {
 | 
			
		||||
					for i := 0; i < b.N; i++ {
 | 
			
		||||
						req := NewRequestf(b, "GET", "/api/v1/repos/%s/%s/branches", "user2", s.name)
 | 
			
		||||
						resp := session.MakeRequest(b, req, http.StatusOK)
 | 
			
		||||
						b.StopTimer()
 | 
			
		||||
						if len(branches) == 0 {
 | 
			
		||||
							DecodeJSON(b, resp, &branches) //Store for next phase
 | 
			
		||||
						}
 | 
			
		||||
						b.StartTimer()
 | 
			
		||||
					}
 | 
			
		||||
				})
 | 
			
		||||
				branchCount := len(branches)
 | 
			
		||||
				b.Run("WebViewCommit", func(b *testing.B) {
 | 
			
		||||
					for i := 0; i < b.N; i++ {
 | 
			
		||||
						req := NewRequestf(b, "GET", "/%s/%s/commit/%s", "user2", s.name, branches[i%branchCount].Commit.ID)
 | 
			
		||||
						session.MakeRequest(b, req, http.StatusOK)
 | 
			
		||||
					}
 | 
			
		||||
				})
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
 | 
			
		||||
func StringWithCharset(length int, charset string) string {
 | 
			
		||||
	b := make([]byte, length)
 | 
			
		||||
	for i := range b {
 | 
			
		||||
		b[i] = charset[rand.Intn(len(charset))]
 | 
			
		||||
	}
 | 
			
		||||
	return string(b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkRepoBranchCommit(b *testing.B) {
 | 
			
		||||
	samples := []int64{1, 3, 15, 16}
 | 
			
		||||
	prepareTestEnv(b)
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	for _, repoID := range samples {
 | 
			
		||||
		b.StopTimer()
 | 
			
		||||
		repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
 | 
			
		||||
		b.StartTimer()
 | 
			
		||||
		b.Run(repo.Name, func(b *testing.B) {
 | 
			
		||||
			owner := models.AssertExistsAndLoadBean(b, &models.User{ID: repo.OwnerID}).(*models.User)
 | 
			
		||||
			session := loginUser(b, owner.LoginName)
 | 
			
		||||
			b.ResetTimer()
 | 
			
		||||
			b.Run("Create", func(b *testing.B) {
 | 
			
		||||
				for i := 0; i < b.N; i++ {
 | 
			
		||||
					b.StopTimer()
 | 
			
		||||
					branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
 | 
			
		||||
					b.StartTimer()
 | 
			
		||||
					testCreateBranch(b, session, owner.LoginName, repo.Name, "branch/master", branchName, http.StatusFound)
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
			b.Run("Access", func(b *testing.B) {
 | 
			
		||||
				var branches []*api.Branch
 | 
			
		||||
				req := NewRequestf(b, "GET", "/api/v1/%s/branches", repo.FullName())
 | 
			
		||||
				resp := session.MakeRequest(b, req, http.StatusOK)
 | 
			
		||||
				DecodeJSON(b, resp, &branches)
 | 
			
		||||
				branchCount := len(branches)
 | 
			
		||||
				b.ResetTimer() //We measure from here
 | 
			
		||||
				for i := 0; i < b.N; i++ {
 | 
			
		||||
					req := NewRequestf(b, "GET", "/%s/%s/commits/%s", owner.Name, repo.Name, branches[i%branchCount])
 | 
			
		||||
					session.MakeRequest(b, req, http.StatusOK)
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//TODO list commits /repos/{owner}/{repo}/commits
 | 
			
		||||
@@ -16,7 +16,7 @@ import (
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
 | 
			
		||||
func testCreateBranch(t testing.TB, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
 | 
			
		||||
	var csrf string
 | 
			
		||||
	if expectedStatus == http.StatusNotFound {
 | 
			
		||||
		csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
 | 
			
		||||
 
 | 
			
		||||
@@ -40,29 +40,3 @@ func TestRepoMigrate(t *testing.T) {
 | 
			
		||||
	session := loginUser(t, "user2")
 | 
			
		||||
	testRepoMigrate(t, session, "https://github.com/go-gitea/git.git", "git")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkRepoMigrate(b *testing.B) {
 | 
			
		||||
	samples := []struct {
 | 
			
		||||
		url  string
 | 
			
		||||
		name string
 | 
			
		||||
	}{
 | 
			
		||||
		{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
 | 
			
		||||
		{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
 | 
			
		||||
		{url: "https://github.com/moby/moby.git", name: "moby"},
 | 
			
		||||
		{url: "https://github.com/golang/go.git", name: "go"},
 | 
			
		||||
		{url: "https://github.com/torvalds/linux.git", name: "linux"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	prepareTestEnv(b)
 | 
			
		||||
	session := loginUser(b, "user2")
 | 
			
		||||
	b.ResetTimer()
 | 
			
		||||
 | 
			
		||||
	for _, s := range samples {
 | 
			
		||||
		b.Run(s.name, func(b *testing.B) {
 | 
			
		||||
			for i := 0; i < b.N; i++ {
 | 
			
		||||
				testRepoMigrate(b, session, s.url, s.name)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -115,7 +115,7 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BeanExists for testing, check if a bean exists
 | 
			
		||||
func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
 | 
			
		||||
func BeanExists(t testing.TB, bean interface{}, conditions ...interface{}) bool {
 | 
			
		||||
	exists, err := loadBeanIfExists(bean, conditions...)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
	return exists
 | 
			
		||||
@@ -123,7 +123,7 @@ func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool
 | 
			
		||||
 | 
			
		||||
// AssertExistsAndLoadBean assert that a bean exists and load it from the test
 | 
			
		||||
// database
 | 
			
		||||
func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {
 | 
			
		||||
func AssertExistsAndLoadBean(t testing.TB, bean interface{}, conditions ...interface{}) interface{} {
 | 
			
		||||
	exists, err := loadBeanIfExists(bean, conditions...)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
	assert.True(t, exists,
 | 
			
		||||
@@ -133,7 +133,7 @@ func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...inter
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetCount get the count of a bean
 | 
			
		||||
func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
 | 
			
		||||
func GetCount(t testing.TB, bean interface{}, conditions ...interface{}) int {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
	whereConditions(sess, conditions)
 | 
			
		||||
@@ -143,7 +143,7 @@ func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AssertNotExistsBean assert that a bean does not exist in the test database
 | 
			
		||||
func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
 | 
			
		||||
func AssertNotExistsBean(t testing.TB, bean interface{}, conditions ...interface{}) {
 | 
			
		||||
	exists, err := loadBeanIfExists(bean, conditions...)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
	assert.False(t, exists)
 | 
			
		||||
@@ -158,18 +158,18 @@ func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ..
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AssertSuccessfulInsert assert that beans is successfully inserted
 | 
			
		||||
func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
 | 
			
		||||
func AssertSuccessfulInsert(t testing.TB, beans ...interface{}) {
 | 
			
		||||
	_, err := x.Insert(beans...)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AssertCount assert the count of a bean
 | 
			
		||||
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
 | 
			
		||||
func AssertCount(t testing.TB, bean interface{}, expected interface{}) {
 | 
			
		||||
	assert.EqualValues(t, expected, GetCount(t, bean))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AssertInt64InRange assert value is in range [low, high]
 | 
			
		||||
func AssertInt64InRange(t *testing.T, low, high, value int64) {
 | 
			
		||||
func AssertInt64InRange(t testing.TB, low, high, value int64) {
 | 
			
		||||
	assert.True(t, value >= low && value <= high,
 | 
			
		||||
		"Expected value in range [%d, %d], found %d", low, high, value)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user