]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: benchmark the waitpool to compare it to the prior channels
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Feb 2021 15:54:45 +0000 (16:54 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Feb 2021 15:59:29 +0000 (16:59 +0100)
Here is the old implementation:

    type WaitPool struct {
        c chan interface{}
    }

    func NewWaitPool(max uint32, new func() interface{}) *WaitPool {
        p := &WaitPool{c: make(chan interface{}, max)}
        for i := uint32(0); i < max; i++ {
            p.c <- new()
        }
        return p
    }

    func (p *WaitPool) Get() interface{} {
        return <- p.c
    }

    func (p *WaitPool) Put(x interface{}) {
        p.c <- x
    }

It performs worse than the new one:

    name         old time/op  new time/op  delta
    WaitPool-16  16.4µs ± 5%  15.1µs ± 3%  -7.86%  (p=0.008 n=5+5)

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
device/pools_test.go

index e6cbac51add08a2eb8bc1509c93823e1d1c1638c..6caf7e7f1df9ba7641dcbec15630320b2a5615aa 100644 (file)
@@ -58,3 +58,26 @@ func TestWaitPool(t *testing.T) {
                t.Errorf("Actual maximum count (%d) != ideal maximum count (%d)", max, p.max)
        }
 }
+
+func BenchmarkWaitPool(b *testing.B) {
+       var wg sync.WaitGroup
+       trials := int32(b.N)
+       workers := runtime.NumCPU() + 2
+       if workers-4 <= 0 {
+               b.Skip("Not enough cores")
+       }
+       p := NewWaitPool(uint32(workers-4), func() interface{} { return make([]byte, 16) })
+       wg.Add(workers)
+       b.ResetTimer()
+       for i := 0; i < workers; i++ {
+               go func() {
+                       defer wg.Done()
+                       for atomic.AddInt32(&trials, -1) > 0 {
+                               x := p.Get()
+                               time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond)
+                               p.Put(x)
+                       }
+               }()
+       }
+       wg.Wait()
+}
\ No newline at end of file