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