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. const k int = 2
  29. benchSampleSize := b.N
  30. watcherSize := k * benchSampleSize
  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. s := &watchableStore{
  36. store: newStore(tmpPath),
  37. unsynced: make(map[*watching]struct{}),
  38. // For previous implementation, use:
  39. // unsynced: make([]*watching, 0),
  40. // to make the test not crash from assigning to nil map.
  41. // 'synced' doesn't get populated in this test.
  42. synced: make(map[string]map[*watching]struct{}),
  43. }
  44. defer func() {
  45. s.store.Close()
  46. os.Remove(tmpPath)
  47. }()
  48. // Put a key so that we can spawn watchers on that key
  49. // (testKey in this test). This increases the rev to 1,
  50. // and later we can we set the watcher's startRev to 1,
  51. // and force watchers to be in unsynced.
  52. testKey := []byte("foo")
  53. testValue := []byte("bar")
  54. s.Put(testKey, testValue)
  55. w := s.NewWatcher()
  56. cancels := make([]CancelFunc, watcherSize)
  57. for i := 0; i < watcherSize; i++ {
  58. // non-0 value to keep watchers in unsynced
  59. cancel := w.Watch(testKey, true, 1)
  60. cancels[i] = cancel
  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(watcherSize)
  65. b.ResetTimer()
  66. b.ReportAllocs()
  67. // cancel N watchers
  68. for _, idx := range ix[:benchSampleSize] {
  69. cancels[idx]()
  70. }
  71. }
  72. func BenchmarkWatchableStoreSyncedCancel(b *testing.B) {
  73. s := newWatchableStore(tmpPath)
  74. defer func() {
  75. s.store.Close()
  76. os.Remove(tmpPath)
  77. }()
  78. // Put a key so that we can spawn watchers on that key
  79. testKey := []byte("foo")
  80. testValue := []byte("bar")
  81. s.Put(testKey, testValue)
  82. w := s.NewWatcher()
  83. // put 1 million watchers on the same key
  84. const watcherSize = 1000000
  85. cancels := make([]CancelFunc, watcherSize)
  86. for i := 0; i < watcherSize; i++ {
  87. // 0 for startRev to keep watchers in synced
  88. cancel := w.Watch(testKey, true, 0)
  89. cancels[i] = cancel
  90. }
  91. // randomly cancel watchers to make it not biased towards
  92. // data structures with an order, such as slice.
  93. ix := rand.Perm(watcherSize)
  94. b.ResetTimer()
  95. b.ReportAllocs()
  96. for _, idx := range ix {
  97. cancels[idx]()
  98. }
  99. }