kvstore_bench_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 CoreOS, Inc.
  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 storage
  15. import (
  16. "log"
  17. "os"
  18. "testing"
  19. )
  20. func BenchmarkStorePut(b *testing.B) {
  21. s := newStore(tmpPath)
  22. defer os.Remove(tmpPath)
  23. // arbitrary number of bytes
  24. bytesN := 64
  25. keys := createBytesSlice(bytesN, b.N)
  26. vals := createBytesSlice(bytesN, b.N)
  27. b.ResetTimer()
  28. for i := 0; i < b.N; i++ {
  29. s.Put(keys[i], vals[i])
  30. }
  31. }
  32. // BenchmarkStoreTxnPut benchmarks the Put operation
  33. // with transaction begin and end, where transaction involves
  34. // some synchronization operations, such as mutex locking.
  35. func BenchmarkStoreTxnPut(b *testing.B) {
  36. s := newStore(tmpPath)
  37. defer cleanup(s, tmpPath)
  38. // arbitrary number of bytes
  39. bytesN := 64
  40. keys := createBytesSlice(bytesN, b.N)
  41. vals := createBytesSlice(bytesN, b.N)
  42. b.ResetTimer()
  43. for i := 0; i < b.N; i++ {
  44. id := s.TxnBegin()
  45. if _, err := s.TxnPut(id, keys[i], vals[i]); err != nil {
  46. log.Fatalf("txn put error: %v", err)
  47. }
  48. s.TxnEnd(id)
  49. }
  50. }