Skip to content

Commit

Permalink
feat: add latency benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam Le committed Jan 13, 2024
1 parent 9c7cd07 commit a5d9560
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ func randStringRunes(n int) string {
}

func main() {
bench("./test", 1_000_000, 8, 128)
bench("./test", 1_000_000, 16, 512)
bench("./test", 10_000_000, 8, 128)
throughput("./test", 1_000_000, 8, 128)
throughput("./test", 1_000_000, 16, 512)
throughput("./test", 10_000_000, 8, 128)

latency("./test", 8, 128)
}

func bench(dirName string, numKeys int, keySize, valSize int) {
func throughput(dirName string, numKeys int, keySize, valSize int) {
db, err := initDB(dirName)
if err != nil {
log.Fatalf("initialize database failed: %v", err)
Expand Down Expand Up @@ -65,6 +67,35 @@ func bench(dirName string, numKeys int, keySize, valSize int) {
os.RemoveAll(dirName)
}

func latency(dirName string, keySize, valSize int) {
db, err := initDB(dirName)
if err != nil {
log.Fatalf("initialize database failed: %v", err)
}
defer db.Close()

now := time.Now()

key := []byte(randStringRunes(keySize))
val := []byte(randStringRunes(valSize))
err = db.Put(key, val)
if err != nil {
log.Fatalf("put failed: %v", err)
}

fmt.Printf("Put one key/value pair takes: %v\n", time.Since(now).String())

now = time.Now()
_, err = db.Get(key)
if err != nil {
log.Fatalf("get failed: %v", err)
}

fmt.Printf("Get one key/value pair takes: %v\n", time.Since(now).String())

os.RemoveAll(dirName)
}

func initDB(dirName string) (*gobitcask.Bitcask, error) {
db, err := gobitcask.New(
gobitcask.WithDirName(dirName),
Expand Down

0 comments on commit a5d9560

Please sign in to comment.