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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 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. onGiteaRunTB(b, func(t testing.TB, u *url.URL) {
  23. b := t.(*testing.B)
  24. samples := []int64{1, 2, 3}
  25. b.ResetTimer()
  26. for _, repoID := range samples {
  27. b.StopTimer()
  28. repo := models.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository)
  29. b.StartTimer()
  30. b.Run(repo.Name, func(b *testing.B) {
  31. session := loginUser(b, "user2")
  32. b.ResetTimer()
  33. b.Run("CreateBranch", func(b *testing.B) {
  34. b.StopTimer()
  35. branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  36. b.StartTimer()
  37. for i := 0; i < b.N; i++ {
  38. b.Run("new_"+branchName, func(b *testing.B) {
  39. b.Skip("benchmark broken") // TODO fix
  40. testAPICreateBranch(b, session, repo.OwnerName, repo.Name, repo.DefaultBranch, "new_"+branchName, http.StatusCreated)
  41. })
  42. }
  43. })
  44. b.Run("GetBranches", func(b *testing.B) {
  45. req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
  46. session.MakeRequest(b, req, http.StatusOK)
  47. })
  48. b.Run("AccessCommits", func(b *testing.B) {
  49. var branches []*api.Branch
  50. req := NewRequestf(b, "GET", "/api/v1/repos/%s/branches", repo.FullName())
  51. resp := session.MakeRequest(b, req, http.StatusOK)
  52. DecodeJSON(b, resp, &branches)
  53. b.ResetTimer() //We measure from here
  54. if len(branches) != 0 {
  55. for i := 0; i < b.N; i++ {
  56. req := NewRequestf(b, "GET", "/api/v1/repos/%s/commits?sha=%s", repo.FullName(), branches[i%len(branches)].Name)
  57. session.MakeRequest(b, req, http.StatusOK)
  58. }
  59. }
  60. })
  61. })
  62. }
  63. })
  64. }