kvstore_bench_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // BenchmarkStoreTxnPut benchmarks the Put operation
  40. // with transaction begin and end, where transaction involves
  41. // some synchronization operations, such as mutex locking.
  42. func BenchmarkStoreTxnPut(b *testing.B) {
  43. var i fakeConsistentIndex
  44. be, tmpPath := backend.NewDefaultTmpBackend()
  45. s := NewStore(be, &lease.FakeLessor{}, &i)
  46. defer cleanup(s, be, tmpPath)
  47. // arbitrary number of bytes
  48. bytesN := 64
  49. keys := createBytesSlice(bytesN, b.N)
  50. vals := createBytesSlice(bytesN, b.N)
  51. b.ResetTimer()
  52. for i := 0; i < b.N; i++ {
  53. id := s.TxnBegin()
  54. if _, err := s.TxnPut(id, keys[i], vals[i], lease.NoLease); err != nil {
  55. plog.Fatalf("txn put error: %v", err)
  56. }
  57. s.TxnEnd(id)
  58. }
  59. }