-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_test.go
More file actions
184 lines (160 loc) · 3.51 KB
/
basic_test.go
File metadata and controls
184 lines (160 loc) · 3.51 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2025 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/FishGoddess/goes"
//"github.com/panjf2000/ants/v2"
//"github.com/sourcegraph/conc/pool"
)
const (
limit = 64
workers = limit
timeLoop = 50_0000
)
func bench(num *uint32) {
atomic.AddUint32(num, 1)
}
// go test -v -run=none -bench=^BenchmarkLimiter$ -benchmem -benchtime=1s
func BenchmarkLimiter(b *testing.B) {
limiter := goes.NewLimiter(limit)
num := uint32(0)
f := func() {
bench(&num)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
limiter.Go(f)
}
})
limiter.Wait()
b.Logf("num is %d", num)
}
// go test -v -run=none -bench=^BenchmarkLimiterTime$ -benchmem -benchtime=1s
func BenchmarkLimiterTime(b *testing.B) {
limiter := goes.NewLimiter(limit)
num := uint32(0)
f := func() {
bench(&num)
}
beginTime := time.Now()
for range timeLoop {
limiter.Go(f)
}
limiter.Wait()
cost := time.Since(beginTime)
b.Logf("num is %d, cost is %s", num, cost)
}
// go test -v -run=none -bench=^BenchmarkExecutor$ -benchmem -benchtime=1s
func BenchmarkExecutor(b *testing.B) {
ctx := context.Background()
executor := goes.NewExecutor(workers)
num := uint32(0)
task := func() {
bench(&num)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
executor.Submit(ctx, task)
}
})
executor.Close()
b.Logf("num is %d", num)
}
// go test -v -run=none -bench=^BenchmarkExecutorTime$ -benchmem -benchtime=1s
func BenchmarkExecutorTime(b *testing.B) {
ctx := context.Background()
executor := goes.NewExecutor(workers)
num := uint32(0)
task := func() {
bench(&num)
}
beginTime := time.Now()
for range timeLoop {
executor.Submit(ctx, task)
}
executor.Close()
cost := time.Since(beginTime)
b.Logf("num is %d, cost is %s", num, cost)
}
// // go test -v -run=none -bench=^BenchmarkAntsPool$ -benchmem -benchtime=1s
// func BenchmarkAntsPool(b *testing.B) {
// pool, _ := ants.NewPool(workers)
//
// num := uint32(0)
// task := func() {
// bench(&num)
// }
//
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// pool.Submit(task)
// }
// })
//
// pool.Release()
// b.Logf("num is %d", num)
// }
//
// // go test -v -run=none -bench=^BenchmarkAntsPoolTime$ -benchmem -benchtime=1s
// func BenchmarkAntsPoolTime(b *testing.B) {
// pool, _ := ants.NewPool(workers)
//
// num := uint32(0)
// task := func() {
// bench(&num)
// }
//
// beginTime := time.Now()
// for range timeLoop {
// pool.Submit(task)
// }
//
// pool.Release()
//
// cost := time.Since(beginTime)
// b.Logf("num is %d, cost is %s", num, cost)
// }
//
// // // go test -v -run=none -bench=^BenchmarkConcPool$ -benchmem -benchtime=1s
// func BenchmarkConcPool(b *testing.B) {
// pool := pool.New().WithMaxGoroutines(workers)
//
// num := uint32(0)
// task := func() {
// bench(&num)
// }
//
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// pool.Go(task)
// }
// })
//
// pool.Wait()
// b.Logf("num is %d", num)
// }
//
// // go test -v -run=none -bench=^BenchmarkConcPoolTime$ -benchmem -benchtime=1s
// func BenchmarkConcPoolTime(b *testing.B) {
// pool := pool.New().WithMaxGoroutines(workers)
//
// num := uint32(0)
// task := func() {
// bench(&num)
// }
//
// beginTime := time.Now()
// for range timeLoop {
// pool.Go(task)
// }
//
// pool.Wait()
//
// cost := time.Since(beginTime)
// b.Logf("num is %d, cost is %s", num, cost)
// }