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.

113 lines
3.5 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "math/rand"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. func BenchmarkRepo(b *testing.B) {
  13. samples := []struct {
  14. url string
  15. name string
  16. skipShort bool
  17. }{
  18. {url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
  19. {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
  20. {url: "https://github.com/moby/moby.git", name: "moby", skipShort: true},
  21. {url: "https://github.com/golang/go.git", name: "go", skipShort: true},
  22. {url: "https://github.com/torvalds/linux.git", name: "linux", skipShort: true},
  23. }
  24. defer prepareTestEnv(b)()
  25. session := loginUser(b, "user2")
  26. b.ResetTimer()
  27. for _, s := range samples {
  28. b.Run(s.name, func(b *testing.B) {
  29. if testing.Short() && s.skipShort {
  30. b.Skip("skipping test in short mode.")
  31. }
  32. b.Run("Migrate", func(b *testing.B) {
  33. for i := 0; i < b.N; i++ {
  34. testRepoMigrate(b, session, s.url, s.name)
  35. }
  36. })
  37. b.Run("Access", func(b *testing.B) {
  38. var branches []*api.Branch
  39. b.Run("APIBranchList", func(b *testing.B) {
  40. for i := 0; i < b.N; i++ {
  41. req := NewRequestf(b, "GET", "/api/v1/repos/%s/%s/branches", "user2", s.name)
  42. resp := session.MakeRequest(b, req, http.StatusOK)
  43. b.StopTimer()
  44. if len(branches) == 0 {
  45. DecodeJSON(b, resp, &branches) //Store for next phase
  46. }
  47. b.StartTimer()
  48. }
  49. })
  50. branchCount := len(branches)
  51. b.Run("WebViewCommit", func(b *testing.B) {
  52. for i := 0; i < b.N; i++ {
  53. req := NewRequestf(b, "GET", "/%s/%s/commit/%s", "user2", s.name, branches[i%branchCount].Commit.ID)
  54. session.MakeRequest(b, req, http.StatusOK)
  55. }
  56. })
  57. })
  58. })
  59. }
  60. }
  61. //StringWithCharset random string (from https://www.calhoun.io/creating-random-strings-in-go/)
  62. func StringWithCharset(length int, charset string) string {
  63. b := make([]byte, length)
  64. for i := range b {
  65. b[i] = charset[rand.Intn(len(charset))]
  66. }
  67. return string(b)
  68. }
  69. func BenchmarkRepoBranchCommit(b *testing.B) {
  70. samples := []int64{1, 3, 15, 16}
  71. defer prepareTestEnv(b)()
  72. b.ResetTimer()
  73. for _, repoID := range samples {
  74. b.StopTimer()
  75. repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
  76. b.StartTimer()
  77. b.Run(repo.Name, func(b *testing.B) {
  78. owner := models.AssertExistsAndLoadBean(b, &models.User{ID: repo.OwnerID}).(*models.User)
  79. session := loginUser(b, owner.LoginName)
  80. b.ResetTimer()
  81. b.Run("Create", func(b *testing.B) {
  82. for i := 0; i < b.N; i++ {
  83. b.StopTimer()
  84. branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  85. b.StartTimer()
  86. testCreateBranch(b, session, owner.LoginName, repo.Name, "branch/master", branchName, http.StatusFound)
  87. }
  88. })
  89. b.Run("Access", func(b *testing.B) {
  90. var branches []*api.Branch
  91. req := NewRequestf(b, "GET", "/api/v1/%s/branches", repo.FullName())
  92. resp := session.MakeRequest(b, req, http.StatusOK)
  93. DecodeJSON(b, resp, &branches)
  94. branchCount := len(branches)
  95. b.ResetTimer() //We measure from here
  96. for i := 0; i < b.N; i++ {
  97. req := NewRequestf(b, "GET", "/%s/%s/commits/%s", owner.Name, repo.Name, branches[i%branchCount].Name)
  98. session.MakeRequest(b, req, http.StatusOK)
  99. }
  100. })
  101. })
  102. }
  103. }
  104. //TODO list commits /repos/{owner}/{repo}/commits