watchable_store_bench_test.go 4.7 KB

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