You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

benchmarks_test.go 2.1KB

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