Skip to content

Commit

Permalink
fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Feb 4, 2015
1 parent bc79ae1 commit 414bdc7
Showing 1 changed file with 78 additions and 28 deletions.
106 changes: 78 additions & 28 deletions importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (
u "github.com/jbenet/go-ipfs/util"
)

func getBalancedDag(t testing.TB, size int64) (*dag.Node, dag.DAGService) {
func getBalancedDag(t testing.TB, size int64, blksize int) (*dag.Node, dag.DAGService) {
ds := mdtest.Mock(t)
r := io.LimitReader(u.NewTimeSeededRand(), size)
nd, err := BuildDagFromReader(r, ds, nil, chunk.DefaultSplitter)
nd, err := BuildDagFromReader(r, ds, nil, &chunk.SizeSplitter{blksize})
if err != nil {
t.Fatal(err)
}
return nd, ds
}

func getTrickleDag(t testing.TB, size int64) (*dag.Node, dag.DAGService) {
func getTrickleDag(t testing.TB, size int64, blksize int) (*dag.Node, dag.DAGService) {
ds := mdtest.Mock(t)
r := io.LimitReader(u.NewTimeSeededRand(), size)
nd, err := BuildTrickleDagFromReader(r, ds, nil, chunk.DefaultSplitter)
nd, err := BuildTrickleDagFromReader(r, ds, nil, &chunk.SizeSplitter{blksize})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -60,42 +60,92 @@ func TestBalancedDag(t *testing.T) {
}
}

func BenchmarkBalancedRead(b *testing.B) {
func BenchmarkBalancedReadSmallBlock(b *testing.B) {
b.StopTimer()
nd, ds := getBalancedDag(b, int64(b.N))
nbytes := int64(10000000)
nd, ds := getBalancedDag(b, nbytes, 4096)

read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
b.StartTimer()
for i := 0; i < b.N; i++ {
read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
}
n, err := io.Copy(ioutil.Discard, read)
if err != nil {
b.Fatal(err)
}
if n != nbytes {
b.Fatal("Failed to read correct amount")
}
}
b.SetBytes(nbytes)
}

func BenchmarkTrickleReadSmallBlock(b *testing.B) {
b.StopTimer()
nbytes := int64(10000000)
nd, ds := getTrickleDag(b, nbytes, 4096)

b.StartTimer()
b.SetBytes(int64(b.N))
n, err := io.Copy(ioutil.Discard, read)
if err != nil {
b.Fatal(err)
}
if n != int64(b.N) {
b.Fatal("Failed to read correct amount")
for i := 0; i < b.N; i++ {
read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
}

n, err := io.Copy(ioutil.Discard, read)
if err != nil {
b.Fatal(err)
}
if n != nbytes {
b.Fatal("Failed to read correct amount")
}
}
b.SetBytes(nbytes)
}

func BenchmarkTrickleRead(b *testing.B) {
func BenchmarkBalancedReadFull(b *testing.B) {
b.StopTimer()
nd, ds := getTrickleDag(b, int64(b.N))
nbytes := int64(10000000)
nd, ds := getBalancedDag(b, nbytes, chunk.DefaultBlockSize)

read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
b.StartTimer()
for i := 0; i < b.N; i++ {
read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
}
n, err := io.Copy(ioutil.Discard, read)
if err != nil {
b.Fatal(err)
}
if n != nbytes {
b.Fatal("Failed to read correct amount")
}
}
b.SetBytes(nbytes)
}

func BenchmarkTrickleReadFull(b *testing.B) {
b.StopTimer()
nbytes := int64(10000000)
nd, ds := getTrickleDag(b, nbytes, chunk.DefaultBlockSize)

b.StartTimer()
b.SetBytes(int64(b.N))
n, err := io.Copy(new(bytes.Buffer), read)
if err != nil {
b.Fatal(err)
}
if n != int64(b.N) {
b.Fatal("Failed to read correct amount")
for i := 0; i < b.N; i++ {
read, err := uio.NewDagReader(context.TODO(), nd, ds)
if err != nil {
b.Fatal(err)
}

n, err := io.Copy(ioutil.Discard, read)
if err != nil {
b.Fatal(err)
}
if n != nbytes {
b.Fatal("Failed to read correct amount")
}
}
b.SetBytes(nbytes)
}

0 comments on commit 414bdc7

Please sign in to comment.