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.

96 lines
1.9 KiB

7 years ago
  1. // +build gcc
  2. package db
  3. import (
  4. "bytes"
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. func BenchmarkRandomReadsWrites2(b *testing.B) {
  11. b.StopTimer()
  12. numItems := int64(1000000)
  13. internal := map[int64]int64{}
  14. for i := 0; i < int(numItems); i++ {
  15. internal[int64(i)] = int64(0)
  16. }
  17. db, err := NewCLevelDB(cmn.Fmt("test_%x", cmn.RandStr(12)), "")
  18. if err != nil {
  19. b.Fatal(err.Error())
  20. return
  21. }
  22. fmt.Println("ok, starting")
  23. b.StartTimer()
  24. for i := 0; i < b.N; i++ {
  25. // Write something
  26. {
  27. idx := (int64(cmn.RandInt()) % numItems)
  28. internal[idx] += 1
  29. val := internal[idx]
  30. idxBytes := int642Bytes(int64(idx))
  31. valBytes := int642Bytes(int64(val))
  32. //fmt.Printf("Set %X -> %X\n", idxBytes, valBytes)
  33. db.Set(
  34. idxBytes,
  35. valBytes,
  36. )
  37. }
  38. // Read something
  39. {
  40. idx := (int64(cmn.RandInt()) % numItems)
  41. val := internal[idx]
  42. idxBytes := int642Bytes(int64(idx))
  43. valBytes := db.Get(idxBytes)
  44. //fmt.Printf("Get %X -> %X\n", idxBytes, valBytes)
  45. if val == 0 {
  46. if !bytes.Equal(valBytes, nil) {
  47. b.Errorf("Expected %v for %v, got %X",
  48. nil, idx, valBytes)
  49. break
  50. }
  51. } else {
  52. if len(valBytes) != 8 {
  53. b.Errorf("Expected length 8 for %v, got %X",
  54. idx, valBytes)
  55. break
  56. }
  57. valGot := bytes2Int64(valBytes)
  58. if val != valGot {
  59. b.Errorf("Expected %v for %v, got %v",
  60. val, idx, valGot)
  61. break
  62. }
  63. }
  64. }
  65. }
  66. db.Close()
  67. }
  68. /*
  69. func int642Bytes(i int64) []byte {
  70. buf := make([]byte, 8)
  71. binary.BigEndian.PutUint64(buf, uint64(i))
  72. return buf
  73. }
  74. func bytes2Int64(buf []byte) int64 {
  75. return int64(binary.BigEndian.Uint64(buf))
  76. }
  77. */
  78. func TestCLevelDBBackend(t *testing.T) {
  79. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  80. db := NewDB(name, LevelDBBackend, "")
  81. defer cleanupDBDir("", name)
  82. _, ok := db.(*CLevelDB)
  83. assert.True(t, ok)
  84. }