Skip to content

Commit

Permalink
Merge pull request #43 from libp2p/feat/background-trimming
Browse files Browse the repository at this point in the history
Background trimming
  • Loading branch information
vyzo authored May 18, 2019
2 parents 4da4a82 + 234c13d commit 2dcf306
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 91 deletions.
51 changes: 51 additions & 0 deletions p2p/net/connmgr/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package connmgr

import (
"math/rand"
"sync"
"testing"

inet "github.com/libp2p/go-libp2p-net"
)

func randomConns(tb testing.TB) (c [5000]inet.Conn) {
for i, _ := range c {
c[i] = randConn(tb, nil)
}
return c
}

func BenchmarkLockContention(b *testing.B) {
conns := randomConns(b)
cm := NewConnManager(1000, 1000, 0)
not := cm.Notifee()

kill := make(chan struct{})
var wg sync.WaitGroup

for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-kill:
return
default:
cm.TagPeer(conns[rand.Intn(len(conns))].RemotePeer(), "another-tag", 1)
}
}
}()
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
rc := conns[rand.Intn(len(conns))]
not.Connected(nil, rc)
cm.TagPeer(rc.RemotePeer(), "tag", 100)
cm.UntagPeer(rc.RemotePeer(), "tag")
not.Disconnected(nil, rc)
}
close(kill)
wg.Wait()
}
Loading

0 comments on commit 2dcf306

Please sign in to comment.