watchable_store_bench_test.go 3.9 KB

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