You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.4 KiB

  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. package memdb
  7. import (
  8. "encoding/binary"
  9. "math/rand"
  10. "testing"
  11. "github.com/syndtr/goleveldb/leveldb/comparer"
  12. )
  13. func BenchmarkPut(b *testing.B) {
  14. buf := make([][4]byte, b.N)
  15. for i := range buf {
  16. binary.LittleEndian.PutUint32(buf[i][:], uint32(i))
  17. }
  18. b.ResetTimer()
  19. p := New(comparer.DefaultComparer, 0)
  20. for i := range buf {
  21. p.Put(buf[i][:], nil)
  22. }
  23. }
  24. func BenchmarkPutRandom(b *testing.B) {
  25. buf := make([][4]byte, b.N)
  26. for i := range buf {
  27. binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int()))
  28. }
  29. b.ResetTimer()
  30. p := New(comparer.DefaultComparer, 0)
  31. for i := range buf {
  32. p.Put(buf[i][:], nil)
  33. }
  34. }
  35. func BenchmarkGet(b *testing.B) {
  36. buf := make([][4]byte, b.N)
  37. for i := range buf {
  38. binary.LittleEndian.PutUint32(buf[i][:], uint32(i))
  39. }
  40. p := New(comparer.DefaultComparer, 0)
  41. for i := range buf {
  42. p.Put(buf[i][:], nil)
  43. }
  44. b.ResetTimer()
  45. for i := range buf {
  46. p.Get(buf[i][:])
  47. }
  48. }
  49. func BenchmarkGetRandom(b *testing.B) {
  50. buf := make([][4]byte, b.N)
  51. for i := range buf {
  52. binary.LittleEndian.PutUint32(buf[i][:], uint32(i))
  53. }
  54. p := New(comparer.DefaultComparer, 0)
  55. for i := range buf {
  56. p.Put(buf[i][:], nil)
  57. }
  58. b.ResetTimer()
  59. for i := 0; i < b.N; i++ {
  60. p.Get(buf[rand.Int()%b.N][:])
  61. }
  62. }