kvstore_bench_test.go 3.6 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. "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 BenchmarkConsistentIndex(b *testing.B) {
  40. fci := fakeConsistentIndex(10)
  41. be, tmpPath := backend.NewDefaultTmpBackend()
  42. s := NewStore(be, &lease.FakeLessor{}, &fci)
  43. defer cleanup(s, be, tmpPath)
  44. tx := s.b.BatchTx()
  45. tx.Lock()
  46. s.saveIndex(tx)
  47. tx.Unlock()
  48. b.ReportAllocs()
  49. b.ResetTimer()
  50. for i := 0; i < b.N; i++ {
  51. s.ConsistentIndex()
  52. }
  53. }
  54. // BenchmarkStoreTxnPutUpdate is same as above, but instead updates single key
  55. func BenchmarkStorePutUpdate(b *testing.B) {
  56. var i fakeConsistentIndex
  57. be, tmpPath := backend.NewDefaultTmpBackend()
  58. s := NewStore(be, &lease.FakeLessor{}, &i)
  59. defer cleanup(s, be, tmpPath)
  60. // arbitrary number of bytes
  61. keys := createBytesSlice(64, 1)
  62. vals := createBytesSlice(1024, 1)
  63. b.ResetTimer()
  64. for i := 0; i < b.N; i++ {
  65. s.Put(keys[0], vals[0], lease.NoLease)
  66. }
  67. }
  68. // BenchmarkStoreTxnPut benchmarks the Put operation
  69. // with transaction begin and end, where transaction involves
  70. // some synchronization operations, such as mutex locking.
  71. func BenchmarkStoreTxnPut(b *testing.B) {
  72. var i fakeConsistentIndex
  73. be, tmpPath := backend.NewDefaultTmpBackend()
  74. s := NewStore(be, &lease.FakeLessor{}, &i)
  75. defer cleanup(s, be, tmpPath)
  76. // arbitrary number of bytes
  77. bytesN := 64
  78. keys := createBytesSlice(bytesN, b.N)
  79. vals := createBytesSlice(bytesN, b.N)
  80. b.ResetTimer()
  81. b.ReportAllocs()
  82. for i := 0; i < b.N; i++ {
  83. txn := s.Write()
  84. txn.Put(keys[i], vals[i], lease.NoLease)
  85. txn.End()
  86. }
  87. }
  88. // benchmarkStoreRestore benchmarks the restore operation
  89. func benchmarkStoreRestore(revsPerKey int, b *testing.B) {
  90. var i fakeConsistentIndex
  91. be, tmpPath := backend.NewDefaultTmpBackend()
  92. s := NewStore(be, &lease.FakeLessor{}, &i)
  93. // use closure to capture 's' to pick up the reassignment
  94. defer func() { cleanup(s, be, tmpPath) }()
  95. // arbitrary number of bytes
  96. bytesN := 64
  97. keys := createBytesSlice(bytesN, b.N)
  98. vals := createBytesSlice(bytesN, b.N)
  99. for i := 0; i < b.N; i++ {
  100. for j := 0; j < revsPerKey; j++ {
  101. txn := s.Write()
  102. txn.Put(keys[i], vals[i], lease.NoLease)
  103. txn.End()
  104. }
  105. }
  106. s.Close()
  107. b.ReportAllocs()
  108. b.ResetTimer()
  109. s = NewStore(be, &lease.FakeLessor{}, &i)
  110. }
  111. func BenchmarkStoreRestoreRevs1(b *testing.B) {
  112. benchmarkStoreRestore(1, b)
  113. }
  114. func BenchmarkStoreRestoreRevs10(b *testing.B) {
  115. benchmarkStoreRestore(10, b)
  116. }
  117. func BenchmarkStoreRestoreRevs20(b *testing.B) {
  118. benchmarkStoreRestore(20, b)
  119. }