rocksdb-api/main.go

113 lines
2.0 KiB
Go
Raw Permalink Normal View History

2020-03-03 10:31:48 +00:00
package main
import (
"log"
"sync"
2020-03-04 04:24:53 +00:00
"time"
"github.com/474420502/focus/compare"
pqueuekey "github.com/474420502/focus/priority_queuekey"
2020-03-03 10:31:48 +00:00
"github.com/tecbot/gorocksdb"
)
2020-03-04 04:24:53 +00:00
func CreateData(gthread int, COUNT int) {
2020-03-03 10:31:48 +00:00
db, cfs := OpenDataBase()
wg := &sync.WaitGroup{}
2020-03-04 04:24:53 +00:00
wg.Add(gthread)
2020-03-03 10:31:48 +00:00
2020-03-04 04:24:53 +00:00
for i := 0; i < gthread; i++ {
go putData(wg, db, cfs, COUNT)
2020-03-03 10:31:48 +00:00
}
wg.Wait()
}
2020-03-04 04:24:53 +00:00
func queryData(wg *sync.WaitGroup, db *gorocksdb.DB, cfs []*gorocksdb.ColumnFamilyHandle, COUNT int) {
2020-03-03 10:31:48 +00:00
defer wg.Done()
2020-03-04 04:24:53 +00:00
now := time.Now().Unix()
2020-03-03 10:31:48 +00:00
ro := gorocksdb.NewDefaultReadOptions()
cf := cfs[len(cfs)-1]
iter := db.NewIteratorCF(ro, cf)
2020-03-04 04:24:53 +00:00
defer iter.Close()
2020-03-03 10:31:48 +00:00
2020-03-04 04:24:53 +00:00
iter.SeekToFirst()
// temp := make([]byte, 8)
// binary.BigEndian.PutUint64(temp, 5)
// iter.Seek(temp)
log.Println(iter.Valid())
2020-03-04 04:24:53 +00:00
pq := pqueuekey.New(compare.ByteArray)
for i := 0; i < COUNT && iter.Valid(); i++ {
2020-03-04 04:24:53 +00:00
bkey := iter.Key().Data()
2020-03-04 04:24:53 +00:00
bvalue := iter.Value().Data()
pq.Push(bkey, bvalue)
2020-03-04 04:24:53 +00:00
// key := string(bkey)
// value := string(bvalue)
// log.Println(binary.BigEndian.Uint64(bkey[0:8]), "\nkey:", key, "\n", "value:", value)
2020-03-03 10:31:48 +00:00
iter.Next()
}
2020-03-04 04:24:53 +00:00
log.Println("size:", pq.Size())
log.Println("qps:", int64(COUNT)/(time.Now().Unix()-now))
piter := pq.Iterator()
for i := 0; piter.Next() && i < 10; i++ {
println("key:", string(piter.Key().([]byte)), "value:", string(piter.Value().([]byte)))
}
2020-03-03 10:31:48 +00:00
}
func QueryData() {
2020-03-04 04:24:53 +00:00
2020-03-03 10:31:48 +00:00
db, cfs := OpenDataBase()
2020-03-04 04:24:53 +00:00
GCOUNT := 10
COUNT := 1000000
now := time.Now().Unix()
2020-03-03 10:31:48 +00:00
wg := &sync.WaitGroup{}
wg.Add(GCOUNT)
for i := 0; i < GCOUNT; i++ {
2020-03-04 04:24:53 +00:00
go queryData(wg, db, cfs, COUNT)
2020-03-03 10:31:48 +00:00
}
wg.Wait()
2020-03-04 04:24:53 +00:00
log.Println("total qps:", int64(COUNT*GCOUNT)/(time.Now().Unix()-now))
2020-03-03 10:31:48 +00:00
}
func main() {
QueryData()
2020-03-04 04:24:53 +00:00
// CreateData(50, 100000) // 10 * 10000
2020-03-03 10:31:48 +00:00
// db.Put(wo, []byte("foo"), []byte("bar"))
// ro := gorocksdb.NewDefaultReadOptions()
// db, err := gorocksdb.OpenDb(opts, "./.rocksdb")
// if err != nil {
// panic(err)
// }
// s, err := db.Get(ro, []byte("foo"))
// if err != nil {
// panic(err)
// }
// log.Println(string(s.Data()))
}