watchable_store_bench_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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: newStore(tmpPath),
  34. unsynced: make(map[*watching]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[*watching]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)
  50. w := s.NewWatcher()
  51. const k int = 2
  52. benchSampleN := b.N
  53. watcherN := k * benchSampleN
  54. cancels := make([]CancelFunc, watcherN)
  55. for i := 0; i < watcherN; i++ {
  56. // non-0 value to keep watchers in unsynced
  57. _, cancel := w.Watch(testKey, true, 1)
  58. cancels[i] = cancel
  59. }
  60. // random-cancel N watchers to make it not biased towards
  61. // data structures with an order, such as slice.
  62. ix := rand.Perm(watcherN)
  63. b.ResetTimer()
  64. b.ReportAllocs()
  65. // cancel N watchers
  66. for _, idx := range ix[:benchSampleN] {
  67. cancels[idx]()
  68. }
  69. }
  70. func BenchmarkWatchableStoreSyncedCancel(b *testing.B) {
  71. s := newWatchableStore(tmpPath)
  72. defer func() {
  73. s.store.Close()
  74. os.Remove(tmpPath)
  75. }()
  76. // Put a key so that we can spawn watchers on that key
  77. testKey := []byte("foo")
  78. testValue := []byte("bar")
  79. s.Put(testKey, testValue)
  80. w := s.NewWatcher()
  81. // put 1 million watchers on the same key
  82. const watcherN = 1000000
  83. cancels := make([]CancelFunc, watcherN)
  84. for i := 0; i < watcherN; i++ {
  85. // 0 for startRev to keep watchers in synced
  86. _, cancel := w.Watch(testKey, true, 0)
  87. cancels[i] = cancel
  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. cancels[idx]()
  96. }
  97. }