store_bench_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "runtime"
  18. "testing"
  19. )
  20. func BenchmarkStoreSet128Bytes(b *testing.B) {
  21. benchStoreSet(b, 128, nil)
  22. }
  23. func BenchmarkStoreSet1024Bytes(b *testing.B) {
  24. benchStoreSet(b, 1024, nil)
  25. }
  26. func BenchmarkStoreSet4096Bytes(b *testing.B) {
  27. benchStoreSet(b, 4096, nil)
  28. }
  29. func BenchmarkStoreSetWithJson128Bytes(b *testing.B) {
  30. benchStoreSet(b, 128, json.Marshal)
  31. }
  32. func BenchmarkStoreSetWithJson1024Bytes(b *testing.B) {
  33. benchStoreSet(b, 1024, json.Marshal)
  34. }
  35. func BenchmarkStoreSetWithJson4096Bytes(b *testing.B) {
  36. benchStoreSet(b, 4096, json.Marshal)
  37. }
  38. func BenchmarkStoreDelete(b *testing.B) {
  39. b.StopTimer()
  40. s := newStore()
  41. kvs, _ := generateNRandomKV(b.N, 128)
  42. memStats := new(runtime.MemStats)
  43. runtime.GC()
  44. runtime.ReadMemStats(memStats)
  45. for i := 0; i < b.N; i++ {
  46. _, err := s.Set(kvs[i][0], false, kvs[i][1], Permanent)
  47. if err != nil {
  48. panic(err)
  49. }
  50. }
  51. setMemStats := new(runtime.MemStats)
  52. runtime.GC()
  53. runtime.ReadMemStats(setMemStats)
  54. b.StartTimer()
  55. for i := range kvs {
  56. s.Delete(kvs[i][0], false, false)
  57. }
  58. b.StopTimer()
  59. // clean up
  60. e, err := s.Get("/", false, false)
  61. if err != nil {
  62. panic(err)
  63. }
  64. for _, n := range e.Node.Nodes {
  65. _, err := s.Delete(n.Key, true, true)
  66. if err != nil {
  67. panic(err)
  68. }
  69. }
  70. s.WatcherHub.EventHistory = nil
  71. deleteMemStats := new(runtime.MemStats)
  72. runtime.GC()
  73. runtime.ReadMemStats(deleteMemStats)
  74. fmt.Printf("\nBefore set Alloc: %v; After set Alloc: %v, After delete Alloc: %v\n",
  75. memStats.Alloc/1000, setMemStats.Alloc/1000, deleteMemStats.Alloc/1000)
  76. }
  77. func BenchmarkWatch(b *testing.B) {
  78. b.StopTimer()
  79. s := newStore()
  80. kvs, _ := generateNRandomKV(b.N, 128)
  81. b.StartTimer()
  82. memStats := new(runtime.MemStats)
  83. runtime.GC()
  84. runtime.ReadMemStats(memStats)
  85. for i := 0; i < b.N; i++ {
  86. w, _ := s.Watch(kvs[i][0], false, false, 0)
  87. e := newEvent("set", kvs[i][0], uint64(i+1), uint64(i+1))
  88. s.WatcherHub.notify(e)
  89. <-w.EventChan
  90. s.CurrentIndex++
  91. }
  92. s.WatcherHub.EventHistory = nil
  93. afterMemStats := new(runtime.MemStats)
  94. runtime.GC()
  95. runtime.ReadMemStats(afterMemStats)
  96. fmt.Printf("\nBefore Alloc: %v; After Alloc: %v\n",
  97. memStats.Alloc/1000, afterMemStats.Alloc/1000)
  98. }
  99. func BenchmarkWatchWithSet(b *testing.B) {
  100. b.StopTimer()
  101. s := newStore()
  102. kvs, _ := generateNRandomKV(b.N, 128)
  103. b.StartTimer()
  104. for i := 0; i < b.N; i++ {
  105. w, _ := s.Watch(kvs[i][0], false, false, 0)
  106. s.Set(kvs[i][0], false, "test", Permanent)
  107. <-w.EventChan
  108. }
  109. }
  110. func BenchmarkWatchWithSetBatch(b *testing.B) {
  111. b.StopTimer()
  112. s := newStore()
  113. kvs, _ := generateNRandomKV(b.N, 128)
  114. b.StartTimer()
  115. watchers := make([]*Watcher, b.N)
  116. for i := 0; i < b.N; i++ {
  117. watchers[i], _ = s.Watch(kvs[i][0], false, false, 0)
  118. }
  119. for i := 0; i < b.N; i++ {
  120. s.Set(kvs[i][0], false, "test", Permanent)
  121. }
  122. for i := 0; i < b.N; i++ {
  123. <-watchers[i].EventChan
  124. }
  125. }
  126. func BenchmarkWatchOneKey(b *testing.B) {
  127. s := newStore()
  128. watchers := make([]*Watcher, b.N)
  129. for i := 0; i < b.N; i++ {
  130. watchers[i], _ = s.Watch("/foo", false, false, 0)
  131. }
  132. s.Set("/foo", false, "", Permanent)
  133. for i := 0; i < b.N; i++ {
  134. <-watchers[i].EventChan
  135. }
  136. }
  137. func benchStoreSet(b *testing.B, valueSize int, process func(interface{}) ([]byte, error)) {
  138. s := newStore()
  139. b.StopTimer()
  140. kvs, size := generateNRandomKV(b.N, valueSize)
  141. b.StartTimer()
  142. for i := 0; i < b.N; i++ {
  143. resp, err := s.Set(kvs[i][0], false, kvs[i][1], Permanent)
  144. if err != nil {
  145. panic(err)
  146. }
  147. if process != nil {
  148. _, err = process(resp)
  149. if err != nil {
  150. panic(err)
  151. }
  152. }
  153. }
  154. kvs = nil
  155. b.StopTimer()
  156. memStats := new(runtime.MemStats)
  157. runtime.GC()
  158. runtime.ReadMemStats(memStats)
  159. fmt.Printf("\nAlloc: %vKB; Data: %vKB; Kvs: %v; Alloc/Data:%v\n",
  160. memStats.Alloc/1000, size/1000, b.N, memStats.Alloc/size)
  161. }
  162. func generateNRandomKV(n int, valueSize int) ([][]string, uint64) {
  163. var size uint64
  164. kvs := make([][]string, n)
  165. bytes := make([]byte, valueSize)
  166. for i := 0; i < n; i++ {
  167. kvs[i] = make([]string, 2)
  168. kvs[i][0] = fmt.Sprintf("/%010d/%010d/%010d", n, n, n)
  169. kvs[i][1] = string(bytes)
  170. size = size + uint64(len(kvs[i][0])) + uint64(len(kvs[i][1]))
  171. }
  172. return kvs, size
  173. }