initial bench and generate command structure

pull/34/head
Steven Normore 2014-04-09 11:40:46 +00:00 committed by Ben Johnson
parent 6903c7447b
commit 6957c9d534
4 changed files with 96 additions and 0 deletions

43
cmd/bolt/bench.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"os"
"github.com/boltdb/bolt"
)
// Run benchmarks on a given dataset.
func Bench() {
path := "bench"
if _, err := os.Stat(path); os.IsNotExist(err) {
fatal(err)
return
}
db, err := bolt.Open(path, 0600)
if err != nil {
fatal(err)
return
}
defer db.Close()
bucketName := "widgets"
key := "key1"
value := "value1"
err = db.Update(func(tx *bolt.Tx) error {
// Find bucket.
b := tx.Bucket(bucketName)
if b == nil {
fatalf("bucket not found: %s", bucketName)
return nil
}
// Set value for a given key.
return b.Put([]byte(key), []byte(value))
})
if err != nil {
fatal(err)
return
}
}

32
cmd/bolt/generate.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// Generate data for benchmarks.
func Generate(numEvents int, destPath string) {
f, err := os.Create(destPath)
if err != nil {
fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
fatal(err)
}
}()
w := bufio.NewWriter(f)
for i := 0; i < numEvents; i++ {
if _, err := w.Write([]byte(fmt.Sprintf("key%d:%s\n", i, strings.Repeat("0", 64)))); err != nil {
fatal(err)
}
}
if err = w.Flush(); err != nil {
fatal(err)
}
}

View File

@ -0,0 +1 @@
package main

View File

@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"strconv"
"github.com/codegangsta/cli"
)
@ -90,6 +91,25 @@ func NewApp() *cli.App {
Check(path)
},
},
{
Name: "generate",
Usage: "Generate data for benchmarks",
Action: func(c *cli.Context) {
numEvents, err := strconv.Atoi(c.Args().Get(0))
if err != nil {
fatal(err)
}
destPath := c.Args().Get(1)
Generate(numEvents, destPath)
},
},
{
Name: "bench",
Usage: "Run benchmarks on a given dataset",
Action: func(c *cli.Context) {
Bench()
},
},
}
return app
}