watchable_store_bench_test.go 3.4 KB

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