This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore_benchmark_test.go
93 lines (80 loc) · 1.57 KB
/
semaphore_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package semaphore_test
import (
"sync"
"testing"
semaphore "github.com/da-moon/go-semaphore"
)
// COMMENT :
// almost identical to hashicorp's lib as far as performance goes
func permitpoolRun(n int) int64 {
var a int64 = 0
sem := newSlowSemaphore()
wg := sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
for j := 0; j < n; j++ {
sem.Signal()
sem.Wait()
}
wg.Done()
}()
}
wg.Wait()
return a
}
func semaphoreRun(n int) int64 {
var a int64 = 0
sem := semaphore.NewCountingSemaphore()
wg := sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
for j := 0; j < n; j++ {
sem.Signal()
sem.Wait()
}
wg.Done()
}()
}
wg.Wait()
return a
}
func semaphoreBench(c int, b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
semaphoreRun(c)
}
}
func permitpoolBench(c int, b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
permitpoolRun(c)
}
}
func BenchmarkSlowSemaphorePermitpool200(b *testing.B) {
permitpoolBench(200, b)
}
func BenchmarkSemaphore200(b *testing.B) {
semaphoreBench(200, b)
}
func BenchmarkSlowSemaphorePermitpool500(b *testing.B) {
permitpoolBench(500, b)
}
func BenchmarkSemaphore500(b *testing.B) {
semaphoreBench(500, b)
}
func BenchmarkSlowSemaphorePermitpool1000(b *testing.B) {
permitpoolBench(1000, b)
}
func BenchmarkSemaphore1000(b *testing.B) {
semaphoreBench(1000, b)
}
func BenchmarkSlowSemaphorePermitpool3000(b *testing.B) {
permitpoolBench(3000, b)
}
func BenchmarkSemaphore3000(b *testing.B) {
semaphoreBench(3000, b)
}