lessor_bench_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "github.com/coreos/etcd/mvcc/backend"
  19. )
  20. func BenchmarkLessorFindExpired1(b *testing.B) { benchmarkLessorFindExpired(1, b) }
  21. func BenchmarkLessorFindExpired10(b *testing.B) { benchmarkLessorFindExpired(10, b) }
  22. func BenchmarkLessorFindExpired100(b *testing.B) { benchmarkLessorFindExpired(100, b) }
  23. func BenchmarkLessorFindExpired1000(b *testing.B) { benchmarkLessorFindExpired(1000, b) }
  24. func BenchmarkLessorFindExpired10000(b *testing.B) { benchmarkLessorFindExpired(10000, b) }
  25. func BenchmarkLessorFindExpired100000(b *testing.B) { benchmarkLessorFindExpired(100000, b) }
  26. func BenchmarkLessorFindExpired1000000(b *testing.B) { benchmarkLessorFindExpired(1000000, b) }
  27. func BenchmarkLessorGrant1(b *testing.B) { benchmarkLessorGrant(1, b) }
  28. func BenchmarkLessorGrant10(b *testing.B) { benchmarkLessorGrant(10, b) }
  29. func BenchmarkLessorGrant100(b *testing.B) { benchmarkLessorGrant(100, b) }
  30. func BenchmarkLessorGrant1000(b *testing.B) { benchmarkLessorGrant(1000, b) }
  31. func BenchmarkLessorGrant10000(b *testing.B) { benchmarkLessorGrant(10000, b) }
  32. func BenchmarkLessorGrant100000(b *testing.B) { benchmarkLessorGrant(100000, b) }
  33. func BenchmarkLessorGrant1000000(b *testing.B) { benchmarkLessorGrant(1000000, b) }
  34. func BenchmarkLessorRenew1(b *testing.B) { benchmarkLessorRenew(1, b) }
  35. func BenchmarkLessorRenew10(b *testing.B) { benchmarkLessorRenew(10, b) }
  36. func BenchmarkLessorRenew100(b *testing.B) { benchmarkLessorRenew(100, b) }
  37. func BenchmarkLessorRenew1000(b *testing.B) { benchmarkLessorRenew(1000, b) }
  38. func BenchmarkLessorRenew10000(b *testing.B) { benchmarkLessorRenew(10000, b) }
  39. func BenchmarkLessorRenew100000(b *testing.B) { benchmarkLessorRenew(100000, b) }
  40. func BenchmarkLessorRenew1000000(b *testing.B) { benchmarkLessorRenew(1000000, b) }
  41. func BenchmarkLessorRevoke1(b *testing.B) { benchmarkLessorRevoke(1, b) }
  42. func BenchmarkLessorRevoke10(b *testing.B) { benchmarkLessorRevoke(10, b) }
  43. func BenchmarkLessorRevoke100(b *testing.B) { benchmarkLessorRevoke(100, b) }
  44. func BenchmarkLessorRevoke1000(b *testing.B) { benchmarkLessorRevoke(1000, b) }
  45. func BenchmarkLessorRevoke10000(b *testing.B) { benchmarkLessorRevoke(10000, b) }
  46. func BenchmarkLessorRevoke100000(b *testing.B) { benchmarkLessorRevoke(100000, b) }
  47. func BenchmarkLessorRevoke1000000(b *testing.B) { benchmarkLessorRevoke(1000000, b) }
  48. func benchmarkLessorFindExpired(size int, b *testing.B) {
  49. be, tmpPath := backend.NewDefaultTmpBackend()
  50. le := newLessor(be, minLeaseTTL)
  51. defer le.Stop()
  52. defer cleanup(be, tmpPath)
  53. le.Promote(0)
  54. for i := 0; i < size; i++ {
  55. le.Grant(LeaseID(i), int64(100+i))
  56. }
  57. le.mu.Lock() //Stop the findExpiredLeases call in the runloop
  58. defer le.mu.Unlock()
  59. b.ResetTimer()
  60. for i := 0; i < b.N; i++ {
  61. le.findExpiredLeases(1000)
  62. }
  63. }
  64. func benchmarkLessorGrant(size int, b *testing.B) {
  65. be, tmpPath := backend.NewDefaultTmpBackend()
  66. le := newLessor(be, minLeaseTTL)
  67. defer le.Stop()
  68. defer cleanup(be, tmpPath)
  69. for i := 0; i < size; i++ {
  70. le.Grant(LeaseID(i), int64(100+i))
  71. }
  72. b.ResetTimer()
  73. for i := 0; i < b.N; i++ {
  74. le.Grant(LeaseID(i+size), int64(100+i+size))
  75. }
  76. }
  77. func benchmarkLessorRevoke(size int, b *testing.B) {
  78. be, tmpPath := backend.NewDefaultTmpBackend()
  79. le := newLessor(be, minLeaseTTL)
  80. defer le.Stop()
  81. defer cleanup(be, tmpPath)
  82. for i := 0; i < size; i++ {
  83. le.Grant(LeaseID(i), int64(100+i))
  84. }
  85. for i := 0; i < b.N; i++ {
  86. le.Grant(LeaseID(i+size), int64(100+i+size))
  87. }
  88. b.ResetTimer()
  89. for i := 0; i < b.N; i++ {
  90. le.Revoke(LeaseID(i + size))
  91. }
  92. }
  93. func benchmarkLessorRenew(size int, b *testing.B) {
  94. be, tmpPath := backend.NewDefaultTmpBackend()
  95. le := newLessor(be, minLeaseTTL)
  96. defer le.Stop()
  97. defer cleanup(be, tmpPath)
  98. for i := 0; i < size; i++ {
  99. le.Grant(LeaseID(i), int64(100+i))
  100. }
  101. b.ResetTimer()
  102. for i := 0; i < b.N; i++ {
  103. le.Renew(LeaseID(i))
  104. }
  105. }
  106. func cleanup(b backend.Backend, path string) {
  107. b.Close()
  108. os.Remove(path)
  109. }