dagwriter_test.go 2.45 KB
Newer Older
1
package io
2 3 4 5 6 7

import (
	"testing"

	"io"

8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9
	bs "github.com/jbenet/go-ipfs/blockservice"
10
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	mdag "github.com/jbenet/go-ipfs/merkledag"
)

type datasource struct {
	i int
}

func (d *datasource) Read(b []byte) (int, error) {
	for i, _ := range b {
		b[i] = byte(d.i % 256)
		d.i++
	}
	return len(b), nil
}

func (d *datasource) Matches(t *testing.T, r io.Reader, length int) bool {
	b := make([]byte, 100)
	i := 0
	for {
		n, err := r.Read(b)
		if err != nil && err != io.EOF {
			t.Fatal(err)
		}
		for _, v := range b[:n] {
			if v != byte(i%256) {
				t.Fatalf("Buffers differed at byte: %d (%d != %d)", i, v, (i % 256))
			}
			i++
		}
		if err == io.EOF {
			break
		}
	}
	if i != length {
		t.Fatalf("Incorrect length. (%d != %d)", i, length)
	}
	return true
}

func TestDagWriter(t *testing.T) {
	dstore := ds.NewMapDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57
	dag := &mdag.DAGService{Blocks: bserv}
	dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
58 59 60 61 62 63 64 65 66 67 68 69 70 71

	nbytes := int64(1024 * 1024 * 2)
	n, err := io.CopyN(dw, &datasource{}, nbytes)
	if err != nil {
		t.Fatal(err)
	}

	if n != nbytes {
		t.Fatal("Copied incorrect amount of bytes!")
	}

	dw.Close()

	node := dw.GetNode()
72
	read, err := NewDagReader(node, dag)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	if err != nil {
		t.Fatal(err)
	}

	d := &datasource{}
	if !d.Matches(t, read, int(nbytes)) {
		t.Fatal("Failed to validate!")
	}
}

func TestMassiveWrite(t *testing.T) {
	t.SkipNow()
	dstore := ds.NewNullDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91
	dag := &mdag.DAGService{Blocks: bserv}
	dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
92 93 94 95 96 97 98 99 100 101 102

	nbytes := int64(1024 * 1024 * 1024 * 16)
	n, err := io.CopyN(dw, &datasource{}, nbytes)
	if err != nil {
		t.Fatal(err)
	}
	if n != nbytes {
		t.Fatal("Incorrect copy size.")
	}
	dw.Close()
}
Jeromy's avatar
Jeromy committed
103 104 105 106 107 108 109

func BenchmarkDagWriter(b *testing.B) {
	dstore := ds.NewNullDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		b.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	dag := &mdag.DAGService{Blocks: bserv}
Jeromy's avatar
Jeromy committed
111 112

	b.ResetTimer()
113
	nbytes := int64(100000)
Jeromy's avatar
Jeromy committed
114 115
	for i := 0; i < b.N; i++ {
		b.SetBytes(nbytes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116
		dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
Jeromy's avatar
Jeromy committed
117 118 119 120 121 122 123 124 125 126 127
		n, err := io.CopyN(dw, &datasource{}, nbytes)
		if err != nil {
			b.Fatal(err)
		}
		if n != nbytes {
			b.Fatal("Incorrect copy size.")
		}
		dw.Close()
	}

}