backend_bench_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 backend
  15. import (
  16. "crypto/rand"
  17. "os"
  18. "testing"
  19. "time"
  20. )
  21. func BenchmarkBackendPut(b *testing.B) {
  22. backend := New("test", 100*time.Millisecond, 10000)
  23. defer backend.Close()
  24. defer os.Remove("test")
  25. // prepare keys
  26. keys := make([][]byte, b.N)
  27. for i := 0; i < b.N; i++ {
  28. keys[i] = make([]byte, 64)
  29. rand.Read(keys[i])
  30. }
  31. value := make([]byte, 128)
  32. rand.Read(value)
  33. batchTx := backend.BatchTx()
  34. batchTx.Lock()
  35. batchTx.UnsafeCreateBucket([]byte("test"))
  36. batchTx.Unlock()
  37. b.ResetTimer()
  38. for i := 0; i < b.N; i++ {
  39. batchTx.Lock()
  40. batchTx.UnsafePut([]byte("test"), keys[i], value)
  41. batchTx.Unlock()
  42. }
  43. }