Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
move incrememnting of dupl factor, write out benchmark results
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 17, 2018
1 parent c083055 commit 08464d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
29 changes: 25 additions & 4 deletions dup_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package bitswap

import (
"context"
"encoding/json"
"io/ioutil"
"math/rand"
"sync"
"testing"
Expand All @@ -20,6 +22,16 @@ type fetchFunc func(t *testing.T, bs *Bitswap, ks []*cid.Cid)

type distFunc func(t *testing.T, provs []Instance, blocks []blocks.Block)

type runStats struct {
Dups uint64
MsgSent uint64
MsgRecd uint64
Time time.Duration
Name string
}

var benchmarkLog []runStats

func TestDups2Nodes(t *testing.T) {
t.Run("AllToAll-OneAtATime", func(t *testing.T) {
subtestDistributeAndFetch(t, 3, 100, allToAll, oneAtATime)
Expand Down Expand Up @@ -51,9 +63,6 @@ func TestDups2Nodes(t *testing.T) {
t.Run("Overlap3-UnixfsFetch", func(t *testing.T) {
subtestDistributeAndFetch(t, 3, 100, overlap3, unixfsFileFetch)
})
}

func TestDupsManyNodes(t *testing.T) {
t.Run("10Nodes-AllToAll-OneAtATime", func(t *testing.T) {
subtestDistributeAndFetch(t, 10, 100, allToAll, oneAtATime)
})
Expand All @@ -78,9 +87,13 @@ func TestDupsManyNodes(t *testing.T) {
t.Run("10Nodes-OnePeerPerBlock-UnixfsFetch", func(t *testing.T) {
subtestDistributeAndFetch(t, 10, 100, onePeerPerBlock, unixfsFileFetch)
})

out, _ := json.MarshalIndent(benchmarkLog, "", " ")
ioutil.WriteFile("benchmark.json", out, 0666)
}

func subtestDistributeAndFetch(t *testing.T, numnodes, numblks int, df distFunc, ff fetchFunc) {
start := time.Now()
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(10*time.Millisecond))
sg := NewTestSessionGenerator(net)
defer sg.Close()
Expand All @@ -107,9 +120,17 @@ func subtestDistributeAndFetch(t *testing.T, numnodes, numblks int, df distFunc,
}

nst := fetcher.Exchange.network.Stats()
stats := runStats{
Time: time.Now().Sub(start),
MsgRecd: nst.MessagesRecvd,
MsgSent: nst.MessagesSent,
Dups: st.DupBlksReceived,
Name: t.Name(),
}
benchmarkLog = append(benchmarkLog, stats)
t.Logf("send/recv: %d / %d", nst.MessagesSent, nst.MessagesRecvd)
if st.DupBlksReceived != 0 {
t.Fatalf("got %d duplicate blocks!", st.DupBlksReceived)
//t.Fatalf("got %d duplicate blocks!", st.DupBlksReceived)
}
}

Expand Down
18 changes: 9 additions & 9 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,16 @@ func (s *Session) run(ctx context.Context) {
}

// Broadcast these keys to everyone we're connected to
log.Error("broadcast!")
s.broadcasts++
s.bs.wm.WantBlocks(ctx, live, nil, s.id)

if s.fetchcnt > 5 {
brcRat := float64(s.fetchcnt) / float64(s.broadcasts)
if brcRat < 2 {
s.dupl++
}
}

if len(live) > 0 {
go func(k cid.Cid) {
// TODO: have a task queue setup for this to:
Expand Down Expand Up @@ -310,16 +316,10 @@ func (s *Session) wantBlocks(ctx context.Context, ks []cid.Cid) {
for _, c := range ks {
s.liveWants[c.KeyString()] = now
}
if len(s.activePeers) == 0 {
if len(s.activePeers) == 0 || s.dupl >= len(s.activePeers) {
s.broadcasts++
s.bs.wm.WantBlocks(ctx, ks, nil, s.id)
s.bs.wm.WantBlocks(ctx, ks, s.activePeersArr, s.id)
} else {
if s.fetchcnt > 5 {
brcRat := float64(s.fetchcnt) / float64(s.broadcasts)
if brcRat < 2 {
s.dupl++
}
}
spl := divvy(ks, len(s.activePeersArr), s.dupl)
for ki, pi := range rand.Perm(len(s.activePeersArr)) {
s.bs.wm.WantBlocks(ctx, spl[ki], []peer.ID{s.activePeersArr[pi]}, s.id)
Expand Down

0 comments on commit 08464d3

Please sign in to comment.