| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package semaphore_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/sync/semaphore" |
| 13 |
) |
| 14 |
|
| 15 |
// weighted is an interface matching a subset of *Weighted. It allows |
| 16 |
// alternate implementations for testing and benchmarking. |
| 17 |
type weighted interface { |
| 18 |
Acquire(context.Context, int64) error |
| 19 |
TryAcquire(int64) bool |
| 20 |
Release(int64) |
| 21 |
} |
| 22 |
|
| 23 |
// semChan implements Weighted using a channel for |
| 24 |
// comparing against the condition variable-based implementation. |
| 25 |
type semChan chan struct{} |
| 26 |
|
| 27 |
func newSemChan(n int64) semChan { |
| 28 |
return semChan(make(chan struct{}, n)) |
| 29 |
} |
| 30 |
|
| 31 |
func (s semChan) Acquire(_ context.Context, n int64) error { |
| 32 |
for i := int64(0); i < n; i++ { |
| 33 |
s <- struct{}{} |
| 34 |
} |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (s semChan) TryAcquire(n int64) bool { |
| 39 |
if int64(len(s))+n > int64(cap(s)) { |
| 40 |
return false |
| 41 |
} |
| 42 |
|
| 43 |
for i := int64(0); i < n; i++ { |
| 44 |
s <- struct{}{} |
| 45 |
} |
| 46 |
return true |
| 47 |
} |
| 48 |
|
| 49 |
func (s semChan) Release(n int64) { |
| 50 |
for i := int64(0); i < n; i++ { |
| 51 |
<-s |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// acquireN calls Acquire(size) on sem N times and then calls Release(size) N times. |
| 56 |
func acquireN(b *testing.B, sem weighted, size int64, N int) { |
| 57 |
b.ResetTimer() |
| 58 |
for i := 0; i < b.N; i++ { |
| 59 |
for j := 0; j < N; j++ { |
| 60 |
sem.Acquire(context.Background(), size) |
| 61 |
} |
| 62 |
for j := 0; j < N; j++ { |
| 63 |
sem.Release(size) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// tryAcquireN calls TryAcquire(size) on sem N times and then calls Release(size) N times. |
| 69 |
func tryAcquireN(b *testing.B, sem weighted, size int64, N int) { |
| 70 |
b.ResetTimer() |
| 71 |
for i := 0; i < b.N; i++ { |
| 72 |
for j := 0; j < N; j++ { |
| 73 |
if !sem.TryAcquire(size) { |
| 74 |
b.Fatalf("TryAcquire(%v) = false, want true", size) |
| 75 |
} |
| 76 |
} |
| 77 |
for j := 0; j < N; j++ { |
| 78 |
sem.Release(size) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func BenchmarkNewSeq(b *testing.B) { |
| 84 |
for _, cap := range []int64{1, 128} { |
| 85 |
b.Run(fmt.Sprintf("Weighted-%d", cap), func(b *testing.B) { |
| 86 |
for i := 0; i < b.N; i++ { |
| 87 |
_ = semaphore.NewWeighted(cap) |
| 88 |
} |
| 89 |
}) |
| 90 |
b.Run(fmt.Sprintf("semChan-%d", cap), func(b *testing.B) { |
| 91 |
for i := 0; i < b.N; i++ { |
| 92 |
_ = newSemChan(cap) |
| 93 |
} |
| 94 |
}) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func BenchmarkAcquireSeq(b *testing.B) { |
| 99 |
for _, c := range []struct { |
| 100 |
cap, size int64 |
| 101 |
N int |
| 102 |
}{ |
| 103 |
{1, 1, 1}, |
| 104 |
{2, 1, 1}, |
| 105 |
{16, 1, 1}, |
| 106 |
{128, 1, 1}, |
| 107 |
{2, 2, 1}, |
| 108 |
{16, 2, 8}, |
| 109 |
{128, 2, 64}, |
| 110 |
{2, 1, 2}, |
| 111 |
{16, 8, 2}, |
| 112 |
{128, 64, 2}, |
| 113 |
} { |
| 114 |
for _, w := range []struct { |
| 115 |
name string |
| 116 |
w weighted |
| 117 |
}{ |
| 118 |
{"Weighted", semaphore.NewWeighted(c.cap)}, |
| 119 |
{"semChan", newSemChan(c.cap)}, |
| 120 |
} { |
| 121 |
b.Run(fmt.Sprintf("%s-acquire-%d-%d-%d", w.name, c.cap, c.size, c.N), func(b *testing.B) { |
| 122 |
acquireN(b, w.w, c.size, c.N) |
| 123 |
}) |
| 124 |
b.Run(fmt.Sprintf("%s-tryAcquire-%d-%d-%d", w.name, c.cap, c.size, c.N), func(b *testing.B) { |
| 125 |
tryAcquireN(b, w.w, c.size, c.N) |
| 126 |
}) |
| 127 |
} |
| 128 |
} |
| 129 |
} |