watchable_store_bench_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2015 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 mvcc
  15. import (
  16. "math/rand"
  17. "os"
  18. "testing"
  19. "go.etcd.io/etcd/lease"
  20. "go.etcd.io/etcd/mvcc/backend"
  21. "go.uber.org/zap"
  22. )
  23. func BenchmarkWatchableStorePut(b *testing.B) {
  24. be, tmpPath := backend.NewDefaultTmpBackend()
  25. s := New(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  26. defer cleanup(s, be, tmpPath)
  27. // arbitrary number of bytes
  28. bytesN := 64
  29. keys := createBytesSlice(bytesN, b.N)
  30. vals := createBytesSlice(bytesN, b.N)
  31. b.ResetTimer()
  32. b.ReportAllocs()
  33. for i := 0; i < b.N; i++ {
  34. s.Put(keys[i], vals[i], lease.NoLease)
  35. }
  36. }
  37. // BenchmarkWatchableStoreTxnPut benchmarks the Put operation
  38. // with transaction begin and end, where transaction involves
  39. // some synchronization operations, such as mutex locking.
  40. func BenchmarkWatchableStoreTxnPut(b *testing.B) {
  41. var i fakeConsistentIndex
  42. be, tmpPath := backend.NewDefaultTmpBackend()
  43. s := New(zap.NewExample(), be, &lease.FakeLessor{}, &i)
  44. defer cleanup(s, be, tmpPath)
  45. // arbitrary number of bytes
  46. bytesN := 64
  47. keys := createBytesSlice(bytesN, b.N)
  48. vals := createBytesSlice(bytesN, b.N)
  49. b.ResetTimer()
  50. b.ReportAllocs()
  51. for i := 0; i < b.N; i++ {
  52. txn := s.Write()
  53. txn.Put(keys[i], vals[i], lease.NoLease)
  54. txn.End()
  55. }
  56. }
  57. // BenchmarkWatchableStoreWatchPutSync benchmarks the case of
  58. // many synced watchers receiving a Put notification.
  59. func BenchmarkWatchableStoreWatchPutSync(b *testing.B) {
  60. benchmarkWatchableStoreWatchPut(b, true)
  61. }
  62. // BenchmarkWatchableStoreWatchPutUnsync benchmarks the case of
  63. // many unsynced watchers receiving a Put notification.
  64. func BenchmarkWatchableStoreWatchPutUnsync(b *testing.B) {
  65. benchmarkWatchableStoreWatchPut(b, false)
  66. }
  67. func benchmarkWatchableStoreWatchPut(b *testing.B, synced bool) {
  68. be, tmpPath := backend.NewDefaultTmpBackend()
  69. s := newWatchableStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  70. defer cleanup(s, be, tmpPath)
  71. k := []byte("testkey")
  72. v := []byte("testval")
  73. rev := int64(0)
  74. if !synced {
  75. // non-0 value to keep watchers in unsynced
  76. rev = 1
  77. }
  78. w := s.NewWatchStream()
  79. defer w.Close()
  80. watchIDs := make([]WatchID, b.N)
  81. for i := range watchIDs {
  82. watchIDs[i], _ = w.Watch(0, k, nil, rev)
  83. }
  84. b.ResetTimer()
  85. b.ReportAllocs()
  86. // trigger watchers
  87. s.Put(k, v, lease.NoLease)
  88. for range watchIDs {
  89. <-w.Chan()
  90. }
  91. select {
  92. case wc := <-w.Chan():
  93. b.Fatalf("unexpected data %v", wc)
  94. default:
  95. }
  96. }
  97. // Benchmarks on cancel function performance for unsynced watchers
  98. // in a WatchableStore. It creates k*N watchers to populate unsynced
  99. // with a reasonably large number of watchers. And measures the time it
  100. // takes to cancel N watchers out of k*N watchers. The performance is
  101. // expected to differ depending on the unsynced member implementation.
  102. // TODO: k is an arbitrary constant. We need to figure out what factor
  103. // we should put to simulate the real-world use cases.
  104. func BenchmarkWatchableStoreUnsyncedCancel(b *testing.B) {
  105. be, tmpPath := backend.NewDefaultTmpBackend()
  106. s := NewStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  107. // manually create watchableStore instead of newWatchableStore
  108. // because newWatchableStore periodically calls syncWatchersLoop
  109. // method to sync watchers in unsynced map. We want to keep watchers
  110. // in unsynced for this benchmark.
  111. ws := &watchableStore{
  112. store: s,
  113. unsynced: newWatcherGroup(),
  114. // to make the test not crash from assigning to nil map.
  115. // 'synced' doesn't get populated in this test.
  116. synced: newWatcherGroup(),
  117. }
  118. defer func() {
  119. ws.store.Close()
  120. os.Remove(tmpPath)
  121. }()
  122. // Put a key so that we can spawn watchers on that key
  123. // (testKey in this test). This increases the rev to 1,
  124. // and later we can we set the watcher's startRev to 1,
  125. // and force watchers to be in unsynced.
  126. testKey := []byte("foo")
  127. testValue := []byte("bar")
  128. s.Put(testKey, testValue, lease.NoLease)
  129. w := ws.NewWatchStream()
  130. const k int = 2
  131. benchSampleN := b.N
  132. watcherN := k * benchSampleN
  133. watchIDs := make([]WatchID, watcherN)
  134. for i := 0; i < watcherN; i++ {
  135. // non-0 value to keep watchers in unsynced
  136. watchIDs[i], _ = w.Watch(0, testKey, nil, 1)
  137. }
  138. // random-cancel N watchers to make it not biased towards
  139. // data structures with an order, such as slice.
  140. ix := rand.Perm(watcherN)
  141. b.ResetTimer()
  142. b.ReportAllocs()
  143. // cancel N watchers
  144. for _, idx := range ix[:benchSampleN] {
  145. if err := w.Cancel(watchIDs[idx]); err != nil {
  146. b.Error(err)
  147. }
  148. }
  149. }
  150. func BenchmarkWatchableStoreSyncedCancel(b *testing.B) {
  151. be, tmpPath := backend.NewDefaultTmpBackend()
  152. s := newWatchableStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  153. defer func() {
  154. s.store.Close()
  155. os.Remove(tmpPath)
  156. }()
  157. // Put a key so that we can spawn watchers on that key
  158. testKey := []byte("foo")
  159. testValue := []byte("bar")
  160. s.Put(testKey, testValue, lease.NoLease)
  161. w := s.NewWatchStream()
  162. // put 1 million watchers on the same key
  163. const watcherN = 1000000
  164. watchIDs := make([]WatchID, watcherN)
  165. for i := 0; i < watcherN; i++ {
  166. // 0 for startRev to keep watchers in synced
  167. watchIDs[i], _ = w.Watch(0, testKey, nil, 0)
  168. }
  169. // randomly cancel watchers to make it not biased towards
  170. // data structures with an order, such as slice.
  171. ix := rand.Perm(watcherN)
  172. b.ResetTimer()
  173. b.ReportAllocs()
  174. for _, idx := range ix {
  175. if err := w.Cancel(watchIDs[idx]); err != nil {
  176. b.Error(err)
  177. }
  178. }
  179. }