kvstore_bench_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "sync/atomic"
  17. "testing"
  18. "github.com/coreos/etcd/lease"
  19. "github.com/coreos/etcd/mvcc/backend"
  20. )
  21. type fakeConsistentIndex uint64
  22. func (i *fakeConsistentIndex) ConsistentIndex() uint64 {
  23. return atomic.LoadUint64((*uint64)(i))
  24. }
  25. func BenchmarkStorePut(b *testing.B) {
  26. var i fakeConsistentIndex
  27. be, tmpPath := backend.NewDefaultTmpBackend()
  28. s := NewStore(be, &lease.FakeLessor{}, &i)
  29. defer cleanup(s, be, tmpPath)
  30. // arbitrary number of bytes
  31. bytesN := 64
  32. keys := createBytesSlice(bytesN, b.N)
  33. vals := createBytesSlice(bytesN, b.N)
  34. b.ResetTimer()
  35. for i := 0; i < b.N; i++ {
  36. s.Put(keys[i], vals[i], lease.NoLease)
  37. }
  38. }
  39. func BenchmarkStoreRangeKey1(b *testing.B) { benchmarkStoreRange(b, 1) }
  40. func BenchmarkStoreRangeKey100(b *testing.B) { benchmarkStoreRange(b, 100) }
  41. func benchmarkStoreRange(b *testing.B, n int) {
  42. var i fakeConsistentIndex
  43. be, tmpPath := backend.NewDefaultTmpBackend()
  44. s := NewStore(be, &lease.FakeLessor{}, &i)
  45. defer cleanup(s, be, tmpPath)
  46. // 64 byte key/val
  47. keys, val := createBytesSlice(64, n), createBytesSlice(64, 1)
  48. for i := range keys {
  49. s.Put(keys[i], val[0], lease.NoLease)
  50. }
  51. // Force into boltdb tx instead of backend read tx.
  52. s.Commit()
  53. var begin, end []byte
  54. if n == 1 {
  55. begin, end = keys[0], nil
  56. } else {
  57. begin, end = []byte{}, []byte{}
  58. }
  59. b.ReportAllocs()
  60. b.ResetTimer()
  61. for i := 0; i < b.N; i++ {
  62. s.Range(begin, end, RangeOptions{})
  63. }
  64. }
  65. func BenchmarkConsistentIndex(b *testing.B) {
  66. fci := fakeConsistentIndex(10)
  67. be, tmpPath := backend.NewDefaultTmpBackend()
  68. s := NewStore(be, &lease.FakeLessor{}, &fci)
  69. defer cleanup(s, be, tmpPath)
  70. tx := s.b.BatchTx()
  71. tx.Lock()
  72. s.saveIndex(tx)
  73. tx.Unlock()
  74. b.ReportAllocs()
  75. b.ResetTimer()
  76. for i := 0; i < b.N; i++ {
  77. s.ConsistentIndex()
  78. }
  79. }
  80. // BenchmarkStoreTxnPutUpdate is same as above, but instead updates single key
  81. func BenchmarkStorePutUpdate(b *testing.B) {
  82. var i fakeConsistentIndex
  83. be, tmpPath := backend.NewDefaultTmpBackend()
  84. s := NewStore(be, &lease.FakeLessor{}, &i)
  85. defer cleanup(s, be, tmpPath)
  86. // arbitrary number of bytes
  87. keys := createBytesSlice(64, 1)
  88. vals := createBytesSlice(1024, 1)
  89. b.ResetTimer()
  90. for i := 0; i < b.N; i++ {
  91. s.Put(keys[0], vals[0], lease.NoLease)
  92. }
  93. }
  94. // BenchmarkStoreTxnPut benchmarks the Put operation
  95. // with transaction begin and end, where transaction involves
  96. // some synchronization operations, such as mutex locking.
  97. func BenchmarkStoreTxnPut(b *testing.B) {
  98. var i fakeConsistentIndex
  99. be, tmpPath := backend.NewDefaultTmpBackend()
  100. s := NewStore(be, &lease.FakeLessor{}, &i)
  101. defer cleanup(s, be, tmpPath)
  102. // arbitrary number of bytes
  103. bytesN := 64
  104. keys := createBytesSlice(bytesN, b.N)
  105. vals := createBytesSlice(bytesN, b.N)
  106. b.ResetTimer()
  107. b.ReportAllocs()
  108. for i := 0; i < b.N; i++ {
  109. txn := s.Write()
  110. txn.Put(keys[i], vals[i], lease.NoLease)
  111. txn.End()
  112. }
  113. }
  114. // benchmarkStoreRestore benchmarks the restore operation
  115. func benchmarkStoreRestore(revsPerKey int, b *testing.B) {
  116. var i fakeConsistentIndex
  117. be, tmpPath := backend.NewDefaultTmpBackend()
  118. s := NewStore(be, &lease.FakeLessor{}, &i)
  119. // use closure to capture 's' to pick up the reassignment
  120. defer func() { cleanup(s, be, tmpPath) }()
  121. // arbitrary number of bytes
  122. bytesN := 64
  123. keys := createBytesSlice(bytesN, b.N)
  124. vals := createBytesSlice(bytesN, b.N)
  125. for i := 0; i < b.N; i++ {
  126. for j := 0; j < revsPerKey; j++ {
  127. txn := s.Write()
  128. txn.Put(keys[i], vals[i], lease.NoLease)
  129. txn.End()
  130. }
  131. }
  132. s.Close()
  133. b.ReportAllocs()
  134. b.ResetTimer()
  135. s = NewStore(be, &lease.FakeLessor{}, &i)
  136. }
  137. func BenchmarkStoreRestoreRevs1(b *testing.B) {
  138. benchmarkStoreRestore(1, b)
  139. }
  140. func BenchmarkStoreRestoreRevs10(b *testing.B) {
  141. benchmarkStoreRestore(10, b)
  142. }
  143. func BenchmarkStoreRestoreRevs20(b *testing.B) {
  144. benchmarkStoreRestore(20, b)
  145. }