@cryptotaxi247 / kubo / commits / 8540f2da8

Add benchmark for just the time it takes to cat

Jeromy committed Apr 6, 2015 at 16:57 UTC 8540f2da80e7c0c7454dceccb5dd6afae7d12d8f
1 file changed +95
test/integration/bench_cat_test.go new
+95
@@ -0,0 +1,95 @@
1 +package integrationtest
2 +
3 +import (
4 + "bytes"
5 + "io"
6 + "math"
7 + "testing"
8 +
9 + context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + "github.com/ipfs/go-ipfs/core"
11 + coreunix "github.com/ipfs/go-ipfs/core/coreunix"
12 + mocknet "github.com/ipfs/go-ipfs/p2p/net/mock"
13 + "github.com/ipfs/go-ipfs/p2p/peer"
14 + "github.com/ipfs/go-ipfs/thirdparty/unit"
15 + errors "github.com/ipfs/go-ipfs/util/debugerror"
16 + testutil "github.com/ipfs/go-ipfs/util/testutil"
17 +)
18 +
19 +func BenchmarkCat1MB(b *testing.B) { benchmarkVarCat(b, unit.MB*1) }
20 +func BenchmarkCat2MB(b *testing.B) { benchmarkVarCat(b, unit.MB*2) }
21 +func BenchmarkCat4MB(b *testing.B) { benchmarkVarCat(b, unit.MB*4) }
22 +
23 +func benchmarkVarCat(b *testing.B, size int64) {
24 + data := RandomBytes(size)
25 + b.SetBytes(size)
26 + for n := 0; n < b.N; n++ {
27 + err := benchCat(b, data, instant)
28 + if err != nil {
29 + b.Fatal(err)
30 + }
31 + }
32 +}
33 +
34 +func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
35 + b.StopTimer()
36 + ctx, cancel := context.WithCancel(context.Background())
37 + defer cancel()
38 + const numPeers = 2
39 +
40 + // create network
41 + mn, err := mocknet.FullMeshLinked(ctx, numPeers)
42 + if err != nil {
43 + return errors.Wrap(err)
44 + }
45 + mn.SetLinkDefaults(mocknet.LinkOptions{
46 + Latency: conf.NetworkLatency,
47 + // TODO add to conf. This is tricky because we want 0 values to be functional.
48 + Bandwidth: math.MaxInt32,
49 + })
50 +
51 + peers := mn.Peers()
52 + if len(peers) < numPeers {
53 + return errors.New("test initialization error")
54 + }
55 +
56 + adder, err := core.NewIPFSNode(ctx, core.ConfigOption(MocknetTestRepo(peers[0], mn.Host(peers[0]), conf, core.DHTOption)))
57 + if err != nil {
58 + return err
59 + }
60 + defer adder.Close()
61 + catter, err := core.NewIPFSNode(ctx, core.ConfigOption(MocknetTestRepo(peers[1], mn.Host(peers[1]), conf, core.DHTOption)))
62 + if err != nil {
63 + return err
64 + }
65 + defer catter.Close()
66 +
67 + bs1 := []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)}
68 + bs2 := []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)}
69 +
70 + if err := catter.Bootstrap(core.BootstrapConfigWithPeers(bs1)); err != nil {
71 + return err
72 + }
73 + if err := adder.Bootstrap(core.BootstrapConfigWithPeers(bs2)); err != nil {
74 + return err
75 + }
76 +
77 + added, err := coreunix.Add(adder, bytes.NewReader(data))
78 + if err != nil {
79 + return err
80 + }
81 +
82 + b.StartTimer()
83 + readerCatted, err := coreunix.Cat(catter, added)
84 + if err != nil {
85 + return err
86 + }
87 +
88 + // verify
89 + var bufout bytes.Buffer
90 + io.Copy(&bufout, readerCatted)
91 + if 0 != bytes.Compare(bufout.Bytes(), data) {
92 + return errors.New("catted data does not match added data")
93 + }
94 + return nil
95 +}