Created
May 1, 2018 13:13
-
-
Save manishrjain/647cc6ea41d2c10769a61f8c517dddae to your computer and use it in GitHub Desktop.
Test Badger with Write-Delete Pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"strconv" | |
"sync" | |
"github.com/dgraph-io/badger" | |
) | |
func writeDelete(db *badger.DB, chi chan int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for i := range chi { | |
err := db.Update(func(txn *badger.Txn) error { | |
return txn.Set([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))) | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = db.Update(func(txn *badger.Txn) error { | |
return txn.Delete([]byte(strconv.Itoa(i))) | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if i%1000 == 0 { | |
fmt.Printf("Done %d write-delete pairs\n", i) | |
} | |
} | |
} | |
func main() { | |
go func() { | |
http.ListenAndServe("localhost:8080", nil) | |
}() | |
opt := badger.DefaultOptions | |
opt.Dir = "/home/mrjn/badgertest" | |
opt.ValueDir = opt.Dir | |
db, err := badger.Open(opt) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
chi := make(chan int, 1000) | |
go func() { | |
for i := 0; i < 100000; i++ { | |
chi <- i | |
} | |
close(chi) | |
}() | |
var wg sync.WaitGroup | |
for i := 0; i < 100; i++ { | |
wg.Add(1) | |
go writeDelete(db, chi, &wg) | |
} | |
wg.Wait() | |
fmt.Println("Done all write-delete pairs") | |
db.PurgeOlderVersions() | |
fmt.Println("Purged older versions") | |
db.RunValueLogGC(0.3) | |
fmt.Println("Ran value log GC") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment