backend_bench_test.go 633 B

123456789101112131415161718192021222324252627282930313233343536
  1. package backend
  2. import (
  3. "crypto/rand"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. func BenchmarkBackendPut(b *testing.B) {
  9. backend := New("test", 100*time.Millisecond, 10000)
  10. defer backend.Close()
  11. defer os.Remove("test")
  12. // prepare keys
  13. keys := make([][]byte, b.N)
  14. for i := 0; i < b.N; i++ {
  15. keys[i] = make([]byte, 64)
  16. rand.Read(keys[i])
  17. }
  18. value := make([]byte, 128)
  19. rand.Read(value)
  20. batchTx := backend.BatchTx()
  21. batchTx.Lock()
  22. batchTx.UnsafeCreateBucket([]byte("test"))
  23. batchTx.Unlock()
  24. b.ResetTimer()
  25. for i := 0; i < b.N; i++ {
  26. batchTx.Lock()
  27. batchTx.UnsafePut([]byte("test"), keys[i], value)
  28. batchTx.Unlock()
  29. }
  30. }