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.

101 lines
2.0 KiB

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