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