mirror of
https://github.com/jech/galene.git
synced 2024-11-25 01:55:57 +01:00
Add packetcache benchmarks.
This commit is contained in:
parent
4d2bd6e473
commit
d723d20ee6
1 changed files with 106 additions and 7 deletions
|
@ -3,6 +3,7 @@ package packetcache
|
|||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
|
@ -203,3 +204,101 @@ func TestBitmapPacket(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCachePutGet(b *testing.B) {
|
||||
n := 10
|
||||
chans := make([]chan uint16, n)
|
||||
for i := range chans {
|
||||
chans[i] = make(chan uint16, 8)
|
||||
}
|
||||
|
||||
cache := New(96)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(chans))
|
||||
|
||||
for i := range chans {
|
||||
go func(ch <-chan uint16) {
|
||||
defer wg.Done()
|
||||
buf := make([]byte, BufSize)
|
||||
for {
|
||||
seqno, ok := <-ch
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
l := cache.Get(seqno, buf)
|
||||
if l == 0 {
|
||||
b.Errorf("Couldn't get %v", seqno)
|
||||
}
|
||||
}
|
||||
}(chans[i])
|
||||
}
|
||||
|
||||
buf := make([]byte, 1200)
|
||||
|
||||
b.SetBytes(1200)
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
seqno := uint16(i)
|
||||
cache.Store(seqno, buf)
|
||||
for _, ch := range chans {
|
||||
ch <- seqno
|
||||
}
|
||||
}
|
||||
for _, ch := range chans {
|
||||
close(ch)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
type is struct {
|
||||
index, seqno uint16
|
||||
}
|
||||
|
||||
func BenchmarkCachePutGetAt(b *testing.B) {
|
||||
n := 10
|
||||
chans := make([]chan is, n)
|
||||
for i := range chans {
|
||||
chans[i] = make(chan is, 8)
|
||||
}
|
||||
|
||||
cache := New(96)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(chans))
|
||||
|
||||
for i := range chans {
|
||||
go func(ch <-chan is) {
|
||||
defer wg.Done()
|
||||
buf := make([]byte, BufSize)
|
||||
for {
|
||||
is, ok := <-ch
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
l := cache.GetAt(is.seqno, is.index, buf)
|
||||
if l == 0 {
|
||||
b.Errorf("Couldn't get %v", is)
|
||||
}
|
||||
}
|
||||
}(chans[i])
|
||||
}
|
||||
|
||||
buf := make([]byte, 1200)
|
||||
|
||||
b.SetBytes(1200)
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
seqno := uint16(i)
|
||||
_, index := cache.Store(seqno, buf)
|
||||
for _, ch := range chans {
|
||||
ch <- is{index, seqno}
|
||||
}
|
||||
}
|
||||
for _, ch := range chans {
|
||||
close(ch)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue