watchable_store_bench_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 CoreOS, Inc.
  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 storage
  15. import (
  16. "math/rand"
  17. "os"
  18. "testing"
  19. "github.com/coreos/etcd/storage/backend"
  20. )
  21. // Benchmarks on cancel function performance for unsynced watchers
  22. // in a WatchableStore. It creates k*N watchers to populate unsynced
  23. // with a reasonably large number of watchers. And measures the time it
  24. // takes to cancel N watchers out of k*N watchers. The performance is
  25. // expected to differ depending on the unsynced member implementation.
  26. // TODO: k is an arbitrary constant. We need to figure out what factor
  27. // we should put to simulate the real-world use cases.
  28. func BenchmarkWatchableStoreUnsyncedCancel(b *testing.B) {
  29. be, tmpPath := backend.NewDefaultTmpBackend()
  30. s := NewStore(be)
  31. // manually create watchableStore instead of newWatchableStore
  32. // because newWatchableStore periodically calls syncWatchersLoop
  33. // method to sync watchers in unsynced map. We want to keep watchers
  34. // in unsynced for this benchmark.
  35. ws := &watchableStore{
  36. store: s,
  37. unsynced: make(map[*watcher]struct{}),
  38. // to make the test not crash from assigning to nil map.
  39. // 'synced' doesn't get populated in this test.
  40. synced: make(map[string]map[*watcher]struct{}),
  41. }
  42. defer func() {
  43. ws.store.Close()
  44. os.Remove(tmpPath)
  45. }()
  46. // Put a key so that we can spawn watchers on that key
  47. // (testKey in this test). This increases the rev to 1,
  48. // and later we can we set the watcher's startRev to 1,
  49. // and force watchers to be in unsynced.
  50. testKey := []byte("foo")
  51. testValue := []byte("bar")
  52. s.Put(testKey, testValue, NoLease)
  53. w := ws.NewWatchStream()
  54. const k int = 2
  55. benchSampleN := b.N
  56. watcherN := k * benchSampleN
  57. watchIDs := make([]WatchID, watcherN)
  58. for i := 0; i < watcherN; i++ {
  59. // non-0 value to keep watchers in unsynced
  60. watchIDs[i] = w.Watch(testKey, true, 1)
  61. }
  62. // random-cancel N watchers to make it not biased towards
  63. // data structures with an order, such as slice.
  64. ix := rand.Perm(watcherN)
  65. b.ResetTimer()
  66. b.ReportAllocs()
  67. // cancel N watchers
  68. for _, idx := range ix[:benchSampleN] {
  69. if err := w.Cancel(watchIDs[idx]); err != nil {
  70. b.Error(err)
  71. }
  72. }
  73. }
  74. func BenchmarkWatchableStoreSyncedCancel(b *testing.B) {
  75. be, tmpPath := backend.NewDefaultTmpBackend()
  76. s := newWatchableStore(be)
  77. defer func() {
  78. s.store.Close()
  79. os.Remove(tmpPath)
  80. }()
  81. // Put a key so that we can spawn watchers on that key
  82. testKey := []byte("foo")
  83. testValue := []byte("bar")
  84. s.Put(testKey, testValue, NoLease)
  85. w := s.NewWatchStream()
  86. // put 1 million watchers on the same key
  87. const watcherN = 1000000
  88. watchIDs := make([]WatchID, watcherN)
  89. for i := 0; i < watcherN; i++ {
  90. // 0 for startRev to keep watchers in synced
  91. watchIDs[i] = w.Watch(testKey, true, 0)
  92. }
  93. // randomly cancel watchers to make it not biased towards
  94. // data structures with an order, such as slice.
  95. ix := rand.Perm(watcherN)
  96. b.ResetTimer()
  97. b.ReportAllocs()
  98. for _, idx := range ix {
  99. if err := w.Cancel(watchIDs[idx]); err != nil {
  100. b.Error(err)
  101. }
  102. }
  103. }