watchable_store_bench_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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[*watcher]struct{}),
  38. // For previous implementation, use:
  39. // unsynced: make([]*watcher, 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][]*watcher),
  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. cancels := make([]CancelFunc, watcherSize)
  56. for i := 0; i < watcherSize; i++ {
  57. // non-0 value to keep watchers in unsynced
  58. _, cancel := s.Watcher(testKey, true, 1)
  59. cancels[i] = cancel
  60. }
  61. // random-cancel N watchers to make it not biased towards
  62. // data structures with an order, such as slice.
  63. ix := rand.Perm(watcherSize)
  64. b.ResetTimer()
  65. b.ReportAllocs()
  66. // cancel N watchers
  67. for _, idx := range ix[:benchSampleSize] {
  68. cancels[idx]()
  69. }
  70. }