kvstore_bench_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. id := s.TxnBegin()
  68. if _, err := s.TxnPut(id, keys[i], vals[i], lease.NoLease); err != nil {
  69. plog.Fatalf("txn put error: %v", err)
  70. }
  71. s.TxnEnd(id)
  72. }
  73. }