watchable_store_bench_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "github.com/coreos/etcd/lease"
  20. "github.com/coreos/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. // BenchmarkWatchableStoreWatchSyncPut benchmarks the case of
  58. // many synced watchers receiving a Put notification.
  59. func BenchmarkWatchableStoreWatchSyncPut(b *testing.B) {
  60. be, tmpPath := backend.NewDefaultTmpBackend()
  61. s := newWatchableStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  62. defer cleanup(s, be, tmpPath)
  63. k := []byte("testkey")
  64. v := []byte("testval")
  65. w := s.NewWatchStream()
  66. defer w.Close()
  67. watchIDs := make([]WatchID, b.N)
  68. for i := range watchIDs {
  69. // non-0 value to keep watchers in unsynced
  70. watchIDs[i], _ = w.Watch(0, k, nil, 1)
  71. }
  72. b.ResetTimer()
  73. b.ReportAllocs()
  74. // trigger watchers
  75. s.Put(k, v, lease.NoLease)
  76. for range watchIDs {
  77. <-w.Chan()
  78. }
  79. select {
  80. case wc := <-w.Chan():
  81. b.Fatalf("unexpected data %v", wc)
  82. default:
  83. }
  84. }
  85. // Benchmarks on cancel function performance for unsynced watchers
  86. // in a WatchableStore. It creates k*N watchers to populate unsynced
  87. // with a reasonably large number of watchers. And measures the time it
  88. // takes to cancel N watchers out of k*N watchers. The performance is
  89. // expected to differ depending on the unsynced member implementation.
  90. // TODO: k is an arbitrary constant. We need to figure out what factor
  91. // we should put to simulate the real-world use cases.
  92. func BenchmarkWatchableStoreUnsyncedCancel(b *testing.B) {
  93. be, tmpPath := backend.NewDefaultTmpBackend()
  94. s := NewStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  95. // manually create watchableStore instead of newWatchableStore
  96. // because newWatchableStore periodically calls syncWatchersLoop
  97. // method to sync watchers in unsynced map. We want to keep watchers
  98. // in unsynced for this benchmark.
  99. ws := &watchableStore{
  100. store: s,
  101. unsynced: newWatcherGroup(),
  102. // to make the test not crash from assigning to nil map.
  103. // 'synced' doesn't get populated in this test.
  104. synced: newWatcherGroup(),
  105. }
  106. defer func() {
  107. ws.store.Close()
  108. os.Remove(tmpPath)
  109. }()
  110. // Put a key so that we can spawn watchers on that key
  111. // (testKey in this test). This increases the rev to 1,
  112. // and later we can we set the watcher's startRev to 1,
  113. // and force watchers to be in unsynced.
  114. testKey := []byte("foo")
  115. testValue := []byte("bar")
  116. s.Put(testKey, testValue, lease.NoLease)
  117. w := ws.NewWatchStream()
  118. const k int = 2
  119. benchSampleN := b.N
  120. watcherN := k * benchSampleN
  121. watchIDs := make([]WatchID, watcherN)
  122. for i := 0; i < watcherN; i++ {
  123. // non-0 value to keep watchers in unsynced
  124. watchIDs[i], _ = w.Watch(0, testKey, nil, 1)
  125. }
  126. // random-cancel N watchers to make it not biased towards
  127. // data structures with an order, such as slice.
  128. ix := rand.Perm(watcherN)
  129. b.ResetTimer()
  130. b.ReportAllocs()
  131. // cancel N watchers
  132. for _, idx := range ix[:benchSampleN] {
  133. if err := w.Cancel(watchIDs[idx]); err != nil {
  134. b.Error(err)
  135. }
  136. }
  137. }
  138. func BenchmarkWatchableStoreSyncedCancel(b *testing.B) {
  139. be, tmpPath := backend.NewDefaultTmpBackend()
  140. s := newWatchableStore(zap.NewExample(), be, &lease.FakeLessor{}, nil)
  141. defer func() {
  142. s.store.Close()
  143. os.Remove(tmpPath)
  144. }()
  145. // Put a key so that we can spawn watchers on that key
  146. testKey := []byte("foo")
  147. testValue := []byte("bar")
  148. s.Put(testKey, testValue, lease.NoLease)
  149. w := s.NewWatchStream()
  150. // put 1 million watchers on the same key
  151. const watcherN = 1000000
  152. watchIDs := make([]WatchID, watcherN)
  153. for i := 0; i < watcherN; i++ {
  154. // 0 for startRev to keep watchers in synced
  155. watchIDs[i], _ = w.Watch(0, testKey, nil, 0)
  156. }
  157. // randomly cancel watchers to make it not biased towards
  158. // data structures with an order, such as slice.
  159. ix := rand.Perm(watcherN)
  160. b.ResetTimer()
  161. b.ReportAllocs()
  162. for _, idx := range ix {
  163. if err := w.Cancel(watchIDs[idx]); err != nil {
  164. b.Error(err)
  165. }
  166. }
  167. }