Skip to content

Commit

Permalink
Code to create graphs for issue #40.
Browse files Browse the repository at this point in the history
Use "make benchmark" to run.
  • Loading branch information
kevina committed Jun 30, 2016
1 parent c33fc95 commit 3014194
Show file tree
Hide file tree
Showing 20 changed files with 376 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ deps: gx
gx-go rewrite
go get -t ./...

benchmark:
gx --verbose install --global
gx-go rewrite
cd leveldb && ./run-benchmark.sh
cd flatfs && ./run-benchmark.sh
4 changes: 2 additions & 2 deletions flatfs/flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/jbenet/go-os-rename"
"gx/ipfs/QmaeRR9SpXumU5tYLRkq6x6pfMe8qKzxn4ujBpsTJ2zQG7/go-os-rename"

logging "github.com/ipfs/go-log"
logging "gx/ipfs/QmNQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR/go-log"
)

var log = logging.Logger("flatfs")
Expand Down
2 changes: 1 addition & 1 deletion flatfs/flatfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ipfs/go-datastore/query"
dstest "github.com/ipfs/go-datastore/test"

rand "github.com/dustin/randbo"
rand "gx/ipfs/QmYvsG72GsfLgUeSojXArjnU6L4Wmwk7wuAxtNLuyXcc1T/randbo"
)

func tempdir(t testing.TB) (path string, cleanup func()) {
Expand Down
7 changes: 7 additions & 0 deletions flatfs/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import db "github.com/ipfs/go-datastore/flatfs"

func main() {
db.TimeQuery()
}
19 changes: 19 additions & 0 deletions flatfs/plot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

sort -nk2 res | awk '$1 == "direct" {$2 = "direct"}; 1' > res2

gnuplot <<EOF
set terminal png size 800,500
set grid lw 0.25
set output "plot.png"
set style fill solid 0.25 border -1
set style boxplot outliers pointtype 7
set style data boxplot
set title "flatfs query key-only 10,000 entries"
set ylabel "time (ms)"
set xlabel "buffer size"
plot "res2" using (1):3:(0.5):2 notitle
EOF


1 change: 1 addition & 0 deletions flatfs/run-benchmark.sh
131 changes: 131 additions & 0 deletions flatfs/time_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package flatfs

import (
"io/ioutil"
"os"
"math/rand"
"time"
"fmt"
"path/filepath"
"strings"

ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
)

var N = 10000

func TimeQuery() {
rand.Seed(time.Now().UTC().UnixNano())
path, err := ioutil.TempDir("/tmp", "flatdb_")
if err != nil {
panic(err)
}

d, err := New(path, 5, false)
if err != nil {
panic(err)
}
defer func() {
os.RemoveAll(path)
d.Close()
}()
for n := 0; n < N; n++ {
err := d.Put(ds.NewKey(RandomString(52)), []byte(RandomString(1000)))
if err != nil {
panic(err)
}
}

//TestQuery(d)
//TestQuery(d)
//TestQuery(d)
// TestDirect(d)
// TestIterator(d)
// TestQuery(d)
//TestDirect(d)
//TestDirect(d)
//TestDirect(d)
// TestQuery(d)
// TestDirect(d)
// TestQuery(d)
// TestDirect(d)
// TestQuery(d)
// TestDirect(d)
// TestDirect(d)
// TestDirect(d)
// TestIterator(d)
// TestIterator(d)
// TestIterator(d)
// TestIterator(d)
// TestQuery(d)
// TestQuery(d)
// TestQuery(d)
for i := 0; i <= 100; i++ {
dsq.KeysOnlyBufSize = 1 << uint(rand.Intn(15))
if dsq.KeysOnlyBufSize <= 8192 {
TestQuery(d)
} else {
TestDirect(d)
}
}
}

func TestQuery(d *Datastore) {
start := time.Now()
rs, err := d.Query(dsq.Query{KeysOnly: true})
if err != nil {
panic(err)
}
i := 0
for r := range rs.Next() {
i += int(r.Key[0])
}
elapsed := time.Since(start)
//fmt.Printf("i = %d\n", i)
fmt.Printf("query %d %f\n", dsq.KeysOnlyBufSize, elapsed.Seconds()*1000)
}

func TestDirect(fs *Datastore) {
start := time.Now()
i := 0
err := filepath.Walk(fs.path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Errorf("Walk func in Query got error: %v", err)
return err
}

if !info.Mode().IsRegular() || strings.HasPrefix(info.Name(), ".") {
return nil
}

key, ok := fs.decode(info.Name())
if !ok {
log.Warning("failed to decode entry in flatfs")
return nil
}

i += int(key.String()[0])

return nil
})
if err != nil {
log.Warning("walk failed: ", err)
}
elapsed := time.Since(start)
//fmt.Printf("i = %d\n", i)
fmt.Printf("direct %d %f\n", dsq.KeysOnlyBufSize, elapsed.Seconds()*1000)

}



func RandomString(strlen int) string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}

2 changes: 1 addition & 1 deletion key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"path"
"strings"

"github.com/satori/go.uuid"
"gx/ipfs/QmcyaFHbyiZfoX5GTpcqqCPYmbjYNAhRDekXSJPFHdYNSV/go.uuid"

dsq "github.com/ipfs/go-datastore/query"
)
Expand Down
54 changes: 50 additions & 4 deletions leveldb/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package leveldb
import (
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
"github.com/jbenet/goprocess"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
"gx/ipfs/QmQopLATEYMNg7dVqZRNDfeE2S1yKy8zrRh5xnYiuqeZBn/goprocess"
"gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb"
"gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/iterator"
"gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/opt"
"gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/util"
)

type datastore struct {
Expand Down Expand Up @@ -157,4 +158,49 @@ func (d *datastore) Close() (err error) {
return d.DB.Close()
}

type IterQuery struct {
Prefix string // namespaces the query to results whose keys have Prefix
KeysOnly bool // return only keys.
}

func (d *datastore) Iterate(prefix string) Iterator {
if prefix == "" {
return &iter{d.DB.NewIterator(nil, nil)}
} else {
return &iter{d.DB.NewIterator(util.BytesPrefix([]byte(prefix)), nil)}
}
}

type Iterator interface {
Next() bool
Key() string
Value() interface{}
Error() error
Close() error
}

type iter struct {
iter iterator.Iterator
}

// func (i *iter) Next() bool {
// more := i.iter.Next()
// if !more {
// i.Result = dsq.Result{dsq.Entry{"", nil}, nil}
// return false
// } else if i.KeysOnly {
// i.Result = dsq.Result{dsq.Entry{string(i.iter.Key()), nil}, nil}
// return true
// } else {
// i.Result = dsq.Result{dsq.Entry{string(i.iter.Key()), i.iter.Value()}, nil}
// return true
// }
// }

func (i *iter) Next() bool { return i.iter.Next() }
func (i *iter) Key() string { return string(i.iter.Key()) }
func (i *iter) Value() interface{} { return i.iter.Value() }
func (i *iter) Error() error { return nil }
func (i *iter) Close() error { i.iter.Release(); return nil }

func (d *datastore) IsThreadSafe() {}
5 changes: 4 additions & 1 deletion leveldb/ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io/ioutil"
"os"
"testing"
"math/rand"
"time"

ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
Expand All @@ -25,7 +27,7 @@ var testcases = map[string]string{
//
// d, close := newDS(t)
// defer close()
func newDS(t *testing.T) (*datastore, func()) {
func newDS(t testing.TB) (*datastore, func()) {
path, err := ioutil.TempDir("/tmp", "testing_leveldb_")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -122,3 +124,4 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
}
}
}

7 changes: 7 additions & 0 deletions leveldb/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import leveldb "github.com/ipfs/go-datastore/leveldb"

func main() {
leveldb.TimeQuery()
}
19 changes: 19 additions & 0 deletions leveldb/plot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

sort -nk2 res | awk '$1 == "direct" {$2 = "direct"}; 1' > res2

gnuplot <<EOF
set terminal png size 800,500
set grid lw 0.25
set output "plot.png"
set style fill solid 0.25 border -1
set style boxplot outliers pointtype 7
set style data boxplot
set title "leveldb query key-only 100,000 entries"
set ylabel "time (ms)"
set xlabel "buffer size"
plot "res2" using (1):3:(0.5):2 notitle
EOF


6 changes: 6 additions & 0 deletions leveldb/run-benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -x

go run main/main.go > res
./plot.sh
Loading

0 comments on commit 3014194

Please sign in to comment.