| 1 | package integrationtest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "math" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/ipfs/boxo/bootstrap" |
| 12 | "github.com/ipfs/boxo/files" |
| 13 | "github.com/ipfs/kubo/core" |
| 14 | "github.com/ipfs/kubo/core/coreapi" |
| 15 | mock "github.com/ipfs/kubo/core/mock" |
| 16 | "github.com/ipfs/kubo/thirdparty/unit" |
| 17 | testutil "github.com/libp2p/go-libp2p-testing/net" |
| 18 | "github.com/libp2p/go-libp2p/core/peer" |
| 19 | mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" |
| 20 | ) |
| 21 | |
| 22 | func BenchmarkCat1MB(b *testing.B) { benchmarkVarCat(b, unit.MB*1) } |
| 23 | func BenchmarkCat2MB(b *testing.B) { benchmarkVarCat(b, unit.MB*2) } |
| 24 | func BenchmarkCat4MB(b *testing.B) { benchmarkVarCat(b, unit.MB*4) } |
| 25 | |
| 26 | func benchmarkVarCat(b *testing.B, size int64) { |
| 27 | data := RandomBytes(size) |
| 28 | b.SetBytes(size) |
| 29 | for n := 0; n < b.N; n++ { |
| 30 | err := benchCat(b, data, instant) |
| 31 | if err != nil { |
| 32 | b.Fatal(err) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error { |
| 38 | b.StopTimer() |
| 39 | ctx, cancel := context.WithCancel(context.Background()) |
| 40 | defer cancel() |
| 41 | |
| 42 | // create network |
| 43 | mn := mocknet.New() |
| 44 | mn.SetLinkDefaults(mocknet.LinkOptions{ |
| 45 | Latency: conf.NetworkLatency, |
| 46 | // TODO add to conf. This is tricky because we want 0 values to be functional. |
| 47 | Bandwidth: math.MaxInt32, |
| 48 | }) |
| 49 | |
| 50 | adder, err := core.NewNode(ctx, &core.BuildCfg{ |
| 51 | Online: true, |
| 52 | Host: mock.MockHostOption(mn), |
| 53 | }) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | defer adder.Close() |
| 58 | |
| 59 | catter, err := core.NewNode(ctx, &core.BuildCfg{ |
| 60 | Online: true, |
| 61 | Host: mock.MockHostOption(mn), |
| 62 | }) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | defer catter.Close() |
| 67 | |
| 68 | adderAPI, err := coreapi.NewCoreAPI(adder) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | catterAPI, err := coreapi.NewCoreAPI(catter) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | err = mn.LinkAll() |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | |
| 83 | bs1 := []peer.AddrInfo{adder.Peerstore.PeerInfo(adder.Identity)} |
| 84 | bs2 := []peer.AddrInfo{catter.Peerstore.PeerInfo(catter.Identity)} |
| 85 | |
| 86 | if err := catter.Bootstrap(bootstrap.BootstrapConfigWithPeers(bs1)); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if err := adder.Bootstrap(bootstrap.BootstrapConfigWithPeers(bs2)); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | added, err := adderAPI.Unixfs().Add(ctx, files.NewBytesFile(data)) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | b.StartTimer() |
| 99 | readerCatted, err := catterAPI.Unixfs().Get(ctx, added) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | // verify |
| 105 | var bufout bytes.Buffer |
| 106 | _, err = io.Copy(&bufout, readerCatted.(io.Reader)) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | if !bytes.Equal(bufout.Bytes(), data) { |
| 111 | return errors.New("catted data does not match added data") |
| 112 | } |
| 113 | return nil |
| 114 | } |