mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 01:34:27 +00:00 
			
		
		
		
	Fix Benchmark tests, remove a broken one & add two new (#15250)
* Benchmark Integration TESTS * CI: add benching-arm64 pipeline * BenchmarkRepo: name test case tests * Fix BenchmarkRepoBranchCommit beside Create new Branch * CI: benching use amd64 * rm total broken "BenchmarkRepo" * dont run benchmark in CI
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -32,6 +32,7 @@ _testmain.go
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
*coverage.out
 | 
					*coverage.out
 | 
				
			||||||
coverage.all
 | 
					coverage.all
 | 
				
			||||||
 | 
					cpu.out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/modules/options/bindata.go
 | 
					/modules/options/bindata.go
 | 
				
			||||||
/modules/options/bindata.go.hash
 | 
					/modules/options/bindata.go.hash
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -151,20 +151,26 @@ func testAPICreateBranches(t *testing.T, giteaURL *url.URL) {
 | 
				
			|||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
		defer resetFixtures(t)
 | 
							defer resetFixtures(t)
 | 
				
			||||||
		session := ctx.Session
 | 
							session := ctx.Session
 | 
				
			||||||
 | 
							testAPICreateBranch(t, session, "user2", "my-noo-repo", test.OldBranch, test.NewBranch, test.ExpectedHTTPStatus)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func testAPICreateBranch(t testing.TB, session *TestSession, user, repo, oldBranch, newBranch string, status int) bool {
 | 
				
			||||||
	token := getTokenForLoggedInUser(t, session)
 | 
						token := getTokenForLoggedInUser(t, session)
 | 
				
			||||||
		req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/my-noo-repo/branches?token="+token, &api.CreateBranchRepoOption{
 | 
						req := NewRequestWithJSON(t, "POST", "/api/v1/repos/"+user+"/"+repo+"/branches?token="+token, &api.CreateBranchRepoOption{
 | 
				
			||||||
			BranchName:    test.NewBranch,
 | 
							BranchName:    newBranch,
 | 
				
			||||||
			OldBranchName: test.OldBranch,
 | 
							OldBranchName: oldBranch,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
		resp := session.MakeRequest(t, req, test.ExpectedHTTPStatus)
 | 
						resp := session.MakeRequest(t, req, status)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var branch api.Branch
 | 
						var branch api.Branch
 | 
				
			||||||
	DecodeJSON(t, resp, &branch)
 | 
						DecodeJSON(t, resp, &branch)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if test.ExpectedHTTPStatus == http.StatusCreated {
 | 
						if status == http.StatusCreated {
 | 
				
			||||||
			assert.EqualValues(t, test.NewBranch, branch.Name)
 | 
							assert.EqualValues(t, newBranch, branch.Name)
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return resp.Result().StatusCode == status
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIBranchProtection(t *testing.T) {
 | 
					func TestAPIBranchProtection(t *testing.T) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -105,6 +105,36 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BenchmarkAPICreateFileSmall(b *testing.B) {
 | 
				
			||||||
 | 
						onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
 | 
				
			||||||
 | 
							b := t.(*testing.B)
 | 
				
			||||||
 | 
							user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)             // owner of the repo1 & repo16
 | 
				
			||||||
 | 
							repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for n := 0; n < b.N; n++ {
 | 
				
			||||||
 | 
								treePath := fmt.Sprintf("update/file%d.txt", n)
 | 
				
			||||||
 | 
								createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BenchmarkAPICreateFileMedium(b *testing.B) {
 | 
				
			||||||
 | 
						data := make([]byte, 10*1024*1024)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
 | 
				
			||||||
 | 
							b := t.(*testing.B)
 | 
				
			||||||
 | 
							user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)             // owner of the repo1 & repo16
 | 
				
			||||||
 | 
							repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							b.ResetTimer()
 | 
				
			||||||
 | 
							for n := 0; n < b.N; n++ {
 | 
				
			||||||
 | 
								treePath := fmt.Sprintf("update/file%d.txt", n)
 | 
				
			||||||
 | 
								copy(data, treePath)
 | 
				
			||||||
 | 
								createFileInBranch(user2, repo1, treePath, repo1.DefaultBranch, treePath)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPICreateFile(t *testing.T) {
 | 
					func TestAPICreateFile(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
 | 
						onGiteaRun(t, func(t *testing.T, u *url.URL) {
 | 
				
			||||||
		user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)               // owner of the repo1 & repo16
 | 
							user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)               // owner of the repo1 & repo16
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,11 +10,11 @@ import (
 | 
				
			|||||||
	api "code.gitea.io/gitea/modules/structs"
 | 
						api "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName string) (*api.FileResponse, error) {
 | 
					func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName, content string) (*api.FileResponse, error) {
 | 
				
			||||||
	opts := &repofiles.UpdateRepoFileOptions{
 | 
						opts := &repofiles.UpdateRepoFileOptions{
 | 
				
			||||||
		OldBranch: branchName,
 | 
							OldBranch: branchName,
 | 
				
			||||||
		TreePath:  treePath,
 | 
							TreePath:  treePath,
 | 
				
			||||||
		Content:   "This is a NEW file",
 | 
							Content:   content,
 | 
				
			||||||
		IsNewFile: true,
 | 
							IsNewFile: true,
 | 
				
			||||||
		Author:    nil,
 | 
							Author:    nil,
 | 
				
			||||||
		Committer: nil,
 | 
							Committer: nil,
 | 
				
			||||||
@@ -23,5 +23,5 @@ func createFileInBranch(user *models.User, repo *models.Repository, treePath, br
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func createFile(user *models.User, repo *models.Repository, treePath string) (*api.FileResponse, error) {
 | 
					func createFile(user *models.User, repo *models.Repository, treePath string) (*api.FileResponse, error) {
 | 
				
			||||||
	return createFileInBranch(user, repo, treePath, repo.DefaultBranch)
 | 
						return createFileInBranch(user, repo, treePath, repo.DefaultBranch, "This is a NEW file")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,63 +7,13 @@ package integrations
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"math/rand"
 | 
						"math/rand"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"net/url"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"code.gitea.io/gitea/models"
 | 
						"code.gitea.io/gitea/models"
 | 
				
			||||||
	api "code.gitea.io/gitea/modules/structs"
 | 
						api "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer 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/)
 | 
					// StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
 | 
				
			||||||
func StringWithCharset(length int, charset string) string {
 | 
					func StringWithCharset(length int, charset string) string {
 | 
				
			||||||
	b := make([]byte, length)
 | 
						b := make([]byte, length)
 | 
				
			||||||
@@ -74,8 +24,10 @@ func StringWithCharset(length int, charset string) string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func BenchmarkRepoBranchCommit(b *testing.B) {
 | 
					func BenchmarkRepoBranchCommit(b *testing.B) {
 | 
				
			||||||
	samples := []int64{1, 3, 15, 16}
 | 
						onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
 | 
				
			||||||
	defer prepareTestEnv(b)()
 | 
							b := t.(*testing.B)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							samples := []int64{1, 2, 3}
 | 
				
			||||||
		b.ResetTimer()
 | 
							b.ResetTimer()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for _, repoID := range samples {
 | 
							for _, repoID := range samples {
 | 
				
			||||||
@@ -83,31 +35,37 @@ func BenchmarkRepoBranchCommit(b *testing.B) {
 | 
				
			|||||||
			repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
 | 
								repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
 | 
				
			||||||
			b.StartTimer()
 | 
								b.StartTimer()
 | 
				
			||||||
			b.Run(repo.Name, func(b *testing.B) {
 | 
								b.Run(repo.Name, func(b *testing.B) {
 | 
				
			||||||
			owner := models.AssertExistsAndLoadBean(b, &models.User{ID: repo.OwnerID}).(*models.User)
 | 
									session := loginUser(b, "user2")
 | 
				
			||||||
			session := loginUser(b, owner.LoginName)
 | 
					 | 
				
			||||||
				b.ResetTimer()
 | 
									b.ResetTimer()
 | 
				
			||||||
			b.Run("Create", func(b *testing.B) {
 | 
									b.Run("CreateBranch", func(b *testing.B) {
 | 
				
			||||||
				for i := 0; i < b.N; i++ {
 | 
					 | 
				
			||||||
					b.StopTimer()
 | 
										b.StopTimer()
 | 
				
			||||||
					branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
 | 
										branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
 | 
				
			||||||
					b.StartTimer()
 | 
										b.StartTimer()
 | 
				
			||||||
					testCreateBranch(b, session, owner.LoginName, repo.Name, "branch/master", branchName, http.StatusFound)
 | 
										for i := 0; i < b.N; i++ {
 | 
				
			||||||
 | 
											b.Run("new_"+branchName, func(b *testing.B) {
 | 
				
			||||||
 | 
												b.Skip("benchmark broken") // TODO fix
 | 
				
			||||||
 | 
												testAPICreateBranch(b, session, repo.OwnerName, repo.Name, repo.DefaultBranch, "new_"+branchName, http.StatusCreated)
 | 
				
			||||||
 | 
											})
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
			b.Run("Access", func(b *testing.B) {
 | 
									b.Run("GetBranches", func(b *testing.B) {
 | 
				
			||||||
 | 
										req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
 | 
				
			||||||
 | 
										session.MakeRequest(b, req, http.StatusOK)
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									b.Run("AccessCommits", func(b *testing.B) {
 | 
				
			||||||
					var branches []*api.Branch
 | 
										var branches []*api.Branch
 | 
				
			||||||
				req := NewRequestf(b, "GET", "/api/v1/%s/branches", repo.FullName())
 | 
										req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
 | 
				
			||||||
					resp := session.MakeRequest(b, req, http.StatusOK)
 | 
										resp := session.MakeRequest(b, req, http.StatusOK)
 | 
				
			||||||
					DecodeJSON(b, resp, &branches)
 | 
										DecodeJSON(b, resp, &branches)
 | 
				
			||||||
				branchCount := len(branches)
 | 
					 | 
				
			||||||
					b.ResetTimer() //We measure from here
 | 
										b.ResetTimer() //We measure from here
 | 
				
			||||||
 | 
										if len(branches) != 0 {
 | 
				
			||||||
						for i := 0; i < b.N; i++ {
 | 
											for i := 0; i < b.N; i++ {
 | 
				
			||||||
					req := NewRequestf(b, "GET", "/%s/%s/commits/%s", owner.Name, repo.Name, branches[i%branchCount].Name)
 | 
												req := NewRequestf(b, "GET", "/api/v1/repos/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
 | 
				
			||||||
							session.MakeRequest(b, req, http.StatusOK)
 | 
												session.MakeRequest(b, req, http.StatusOK)
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
//TODO list commits /repos/{owner}/{repo}/commits
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -76,7 +76,7 @@ func allowLFSFilters() []string {
 | 
				
			|||||||
	return filteredLFSGlobalArgs[:j]
 | 
						return filteredLFSGlobalArgs[:j]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
 | 
					func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
 | 
				
			||||||
	if len(prepare) == 0 || prepare[0] {
 | 
						if len(prepare) == 0 || prepare[0] {
 | 
				
			||||||
		defer prepareTestEnv(t, 1)()
 | 
							defer prepareTestEnv(t, 1)()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -108,6 +108,12 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
 | 
				
			|||||||
	callback(t, u)
 | 
						callback(t, u)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
 | 
				
			||||||
 | 
						onGiteaRunTB(t, func(t testing.TB, u *url.URL) {
 | 
				
			||||||
 | 
							callback(t.(*testing.T), u)
 | 
				
			||||||
 | 
						}, prepare...)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
					func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
 | 
				
			||||||
	return func(t *testing.T) {
 | 
						return func(t *testing.T) {
 | 
				
			||||||
		assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
 | 
							assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user