lessor_bench_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lease
  15. import (
  16. "os"
  17. "testing"
  18. "go.etcd.io/etcd/mvcc/backend"
  19. "go.uber.org/zap"
  20. )
  21. func BenchmarkLessorFindExpired1(b *testing.B) { benchmarkLessorFindExpired(1, b) }
  22. func BenchmarkLessorFindExpired10(b *testing.B) { benchmarkLessorFindExpired(10, b) }
  23. func BenchmarkLessorFindExpired100(b *testing.B) { benchmarkLessorFindExpired(100, b) }
  24. func BenchmarkLessorFindExpired1000(b *testing.B) { benchmarkLessorFindExpired(1000, b) }
  25. func BenchmarkLessorFindExpired10000(b *testing.B) { benchmarkLessorFindExpired(10000, b) }
  26. func BenchmarkLessorFindExpired100000(b *testing.B) { benchmarkLessorFindExpired(100000, b) }
  27. func BenchmarkLessorFindExpired1000000(b *testing.B) { benchmarkLessorFindExpired(1000000, b) }
  28. func BenchmarkLessorGrant1(b *testing.B) { benchmarkLessorGrant(1, b) }
  29. func BenchmarkLessorGrant10(b *testing.B) { benchmarkLessorGrant(10, b) }
  30. func BenchmarkLessorGrant100(b *testing.B) { benchmarkLessorGrant(100, b) }
  31. func BenchmarkLessorGrant1000(b *testing.B) { benchmarkLessorGrant(1000, b) }
  32. func BenchmarkLessorGrant10000(b *testing.B) { benchmarkLessorGrant(10000, b) }
  33. func BenchmarkLessorGrant100000(b *testing.B) { benchmarkLessorGrant(100000, b) }
  34. func BenchmarkLessorGrant1000000(b *testing.B) { benchmarkLessorGrant(1000000, b) }
  35. func BenchmarkLessorRenew1(b *testing.B) { benchmarkLessorRenew(1, b) }
  36. func BenchmarkLessorRenew10(b *testing.B) { benchmarkLessorRenew(10, b) }
  37. func BenchmarkLessorRenew100(b *testing.B) { benchmarkLessorRenew(100, b) }
  38. func BenchmarkLessorRenew1000(b *testing.B) { benchmarkLessorRenew(1000, b) }
  39. func BenchmarkLessorRenew10000(b *testing.B) { benchmarkLessorRenew(10000, b) }
  40. func BenchmarkLessorRenew100000(b *testing.B) { benchmarkLessorRenew(100000, b) }
  41. func BenchmarkLessorRenew1000000(b *testing.B) { benchmarkLessorRenew(1000000, b) }
  42. func BenchmarkLessorRevoke1(b *testing.B) { benchmarkLessorRevoke(1, b) }
  43. func BenchmarkLessorRevoke10(b *testing.B) { benchmarkLessorRevoke(10, b) }
  44. func BenchmarkLessorRevoke100(b *testing.B) { benchmarkLessorRevoke(100, b) }
  45. func BenchmarkLessorRevoke1000(b *testing.B) { benchmarkLessorRevoke(1000, b) }
  46. func BenchmarkLessorRevoke10000(b *testing.B) { benchmarkLessorRevoke(10000, b) }
  47. func BenchmarkLessorRevoke100000(b *testing.B) { benchmarkLessorRevoke(100000, b) }
  48. func BenchmarkLessorRevoke1000000(b *testing.B) { benchmarkLessorRevoke(1000000, b) }
  49. func benchmarkLessorFindExpired(size int, b *testing.B) {
  50. lg := zap.NewNop()
  51. be, tmpPath := backend.NewDefaultTmpBackend()
  52. le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
  53. defer le.Stop()
  54. defer cleanup(be, tmpPath)
  55. le.Promote(0)
  56. for i := 0; i < size; i++ {
  57. le.Grant(LeaseID(i), int64(100+i))
  58. }
  59. le.mu.Lock() //Stop the findExpiredLeases call in the runloop
  60. defer le.mu.Unlock()
  61. b.ResetTimer()
  62. for i := 0; i < b.N; i++ {
  63. le.findExpiredLeases(1000)
  64. }
  65. }
  66. func benchmarkLessorGrant(size int, b *testing.B) {
  67. lg := zap.NewNop()
  68. be, tmpPath := backend.NewDefaultTmpBackend()
  69. le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
  70. defer le.Stop()
  71. defer cleanup(be, tmpPath)
  72. for i := 0; i < size; i++ {
  73. le.Grant(LeaseID(i), int64(100+i))
  74. }
  75. b.ResetTimer()
  76. for i := 0; i < b.N; i++ {
  77. le.Grant(LeaseID(i+size), int64(100+i+size))
  78. }
  79. }
  80. func benchmarkLessorRevoke(size int, b *testing.B) {
  81. lg := zap.NewNop()
  82. be, tmpPath := backend.NewDefaultTmpBackend()
  83. le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
  84. defer le.Stop()
  85. defer cleanup(be, tmpPath)
  86. for i := 0; i < size; i++ {
  87. le.Grant(LeaseID(i), int64(100+i))
  88. }
  89. for i := 0; i < b.N; i++ {
  90. le.Grant(LeaseID(i+size), int64(100+i+size))
  91. }
  92. b.ResetTimer()
  93. for i := 0; i < b.N; i++ {
  94. le.Revoke(LeaseID(i + size))
  95. }
  96. }
  97. func benchmarkLessorRenew(size int, b *testing.B) {
  98. lg := zap.NewNop()
  99. be, tmpPath := backend.NewDefaultTmpBackend()
  100. le := newLessor(lg, be, LessorConfig{MinLeaseTTL: minLeaseTTL})
  101. defer le.Stop()
  102. defer cleanup(be, tmpPath)
  103. for i := 0; i < size; i++ {
  104. le.Grant(LeaseID(i), int64(100+i))
  105. }
  106. b.ResetTimer()
  107. for i := 0; i < b.N; i++ {
  108. le.Renew(LeaseID(i))
  109. }
  110. }
  111. func cleanup(b backend.Backend, path string) {
  112. b.Close()
  113. os.Remove(path)
  114. }