kvstore_bench_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // BenchmarkStoreTxnPutUpdate is same as above, but instead updates single key
  40. func BenchmarkStorePutUpdate(b *testing.B) {
  41. var i fakeConsistentIndex
  42. be, tmpPath := backend.NewDefaultTmpBackend()
  43. s := NewStore(be, &lease.FakeLessor{}, &i)
  44. defer cleanup(s, be, tmpPath)
  45. // arbitrary number of bytes
  46. keys := createBytesSlice(64, 1)
  47. vals := createBytesSlice(1024, 1)
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. s.Put(keys[0], vals[0], lease.NoLease)
  51. }
  52. }
  53. // BenchmarkStoreTxnPut benchmarks the Put operation
  54. // with transaction begin and end, where transaction involves
  55. // some synchronization operations, such as mutex locking.
  56. func BenchmarkStoreTxnPut(b *testing.B) {
  57. var i fakeConsistentIndex
  58. be, tmpPath := backend.NewDefaultTmpBackend()
  59. s := NewStore(be, &lease.FakeLessor{}, &i)
  60. defer cleanup(s, be, tmpPath)
  61. // arbitrary number of bytes
  62. bytesN := 64
  63. keys := createBytesSlice(bytesN, b.N)
  64. vals := createBytesSlice(bytesN, b.N)
  65. b.ResetTimer()
  66. for i := 0; i < b.N; i++ {
  67. txn := s.Write()
  68. txn.Put(keys[i], vals[i], lease.NoLease)
  69. txn.End()
  70. }
  71. }
  72. // benchmarkStoreRestore benchmarks the restore operation
  73. func benchmarkStoreRestore(revsPerKey int, b *testing.B) {
  74. var i fakeConsistentIndex
  75. be, tmpPath := backend.NewDefaultTmpBackend()
  76. s := NewStore(be, &lease.FakeLessor{}, &i)
  77. defer cleanup(s, be, tmpPath)
  78. // arbitrary number of bytes
  79. bytesN := 64
  80. keys := createBytesSlice(bytesN, b.N)
  81. vals := createBytesSlice(bytesN, b.N)
  82. for i := 0; i < b.N; i++ {
  83. for j := 0; j < revsPerKey; j++ {
  84. txn := s.Write()
  85. txn.Put(keys[i], vals[i], lease.NoLease)
  86. txn.End()
  87. }
  88. }
  89. b.ResetTimer()
  90. }
  91. func BenchmarkStoreRestoreRevs1(b *testing.B) {
  92. benchmarkStoreRestore(1, b)
  93. }
  94. func BenchmarkStoreRestoreRevs10(b *testing.B) {
  95. benchmarkStoreRestore(10, b)
  96. }
  97. func BenchmarkStoreRestoreRevs20(b *testing.B) {
  98. benchmarkStoreRestore(20, b)
  99. }