Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

benchmarks_test.go 2.2KB

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