SHARE
TWEET

Untitled

a guest Oct 20th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "math/rand"
  5.     "os"
  6.     "time"
  7.  
  8.     "github.com/dgraph-io/badger"
  9.     "github.com/sirupsen/logrus"
  10. )
  11.  
  12. var logger *logrus.Logger
  13. var log *logrus.Entry
  14.  
  15. func init() {
  16.     logger = logrus.New()
  17.     logger.SetLevel(logrus.DebugLevel)
  18.     logger.SetFormatter(&logrus.JSONFormatter{
  19.         TimestampFormat: time.RFC3339,
  20.     })
  21.     log = logger.WithFields(logrus.Fields{"ns": "main"})
  22. }
  23.  
  24. func write(tx *badger.WriteBatch, n int, vsize int) {
  25.     for j := 0; j <= n; j++ {
  26.         val := make([]byte, vsize)
  27.         _, err := rand.Read(val)
  28.         if err != nil {
  29.             log.Error("generate value:", err)
  30.             continue
  31.         }
  32.         key := make([]byte, 32)
  33.         _, err = rand.Read(key)
  34.         if err != nil {
  35.             log.Error("generate key:", err)
  36.             continue
  37.         }
  38.         err = tx.Set(key, val)
  39.         if err != nil {
  40.             log.Error("set:", err)
  41.             continue
  42.         }
  43.         if j%100 == 0 {
  44.             log.Infof("set %d items", j)
  45.         }
  46.     }
  47. }
  48.  
  49. func main() {
  50.     sub := logger.WithFields(logrus.Fields{"ns": "badger"})
  51.     path := "./badger.db.2"
  52.     opts := badger.DefaultOptions(path)
  53.     opts = opts.WithLogger(sub)
  54.     db, err := badger.Open(opts)
  55.     if err != nil {
  56.         log.Error("open badger:", err)
  57.         os.Exit(1)
  58.     }
  59.     bt := db.NewWriteBatch()
  60.     defer bt.Cancel()
  61.     // 20 million 10 kb values
  62.     write(bt, 2000000, 10000)
  63.     // 20,000  16 mb values
  64.     write(bt, 20000, 16000000)
  65.     log.Infof("flush")
  66.     err = bt.Flush()
  67.     if err != nil {
  68.         log.Error("flush err:", err)
  69.     }
  70.     log.Infof("done")
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top