watchable_store_bench_test.go 5.3 KB

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