kvstore_bench_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. for i := 0; i < b.N; i++ {
  82. txn := s.Write()
  83. txn.Put(keys[i], vals[i], lease.NoLease)
  84. txn.End()
  85. }
  86. }
  87. // benchmarkStoreRestore benchmarks the restore operation
  88. func benchmarkStoreRestore(revsPerKey int, b *testing.B) {
  89. var i fakeConsistentIndex
  90. be, tmpPath := backend.NewDefaultTmpBackend()
  91. s := NewStore(be, &lease.FakeLessor{}, &i)
  92. // use closure to capture 's' to pick up the reassignment
  93. defer func() { cleanup(s, be, tmpPath) }()
  94. // arbitrary number of bytes
  95. bytesN := 64
  96. keys := createBytesSlice(bytesN, b.N)
  97. vals := createBytesSlice(bytesN, b.N)
  98. for i := 0; i < b.N; i++ {
  99. for j := 0; j < revsPerKey; j++ {
  100. txn := s.Write()
  101. txn.Put(keys[i], vals[i], lease.NoLease)
  102. txn.End()
  103. }
  104. }
  105. s.Close()
  106. b.ReportAllocs()
  107. b.ResetTimer()
  108. s = NewStore(be, &lease.FakeLessor{}, &i)
  109. }
  110. func BenchmarkStoreRestoreRevs1(b *testing.B) {
  111. benchmarkStoreRestore(1, b)
  112. }
  113. func BenchmarkStoreRestoreRevs10(b *testing.B) {
  114. benchmarkStoreRestore(10, b)
  115. }
  116. func BenchmarkStoreRestoreRevs20(b *testing.B) {
  117. benchmarkStoreRestore(20, b)
  118. }