OSDN Git Service

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