1
1
package main
2
2
3
3
import (
4
- "fmt"
5
4
"io"
6
5
"testing"
7
6
"time"
@@ -10,6 +9,10 @@ import (
10
9
"github.com/sourcegraph/conc/pool"
11
10
)
12
11
12
+ const (
13
+ benchRoutineCount = 100
14
+ )
15
+
13
16
func BenchmarkNursery (b * testing.B ) {
14
17
b .Run ("EmptyBlock" , func (b * testing.B ) {
15
18
for i := 0 ; i < b .N ; i ++ {
@@ -19,71 +22,79 @@ func BenchmarkNursery(b *testing.B) {
19
22
}
20
23
})
21
24
22
- for _ , routine := range []int {10 , 1000 , 100000 } {
23
- b .Run (fmt .Sprintf ("WithRoutines/%d/NoWork" , routine ), func (b * testing.B ) {
24
- for i := 0 ; i < b .N ; i ++ {
25
- conc .Block (func (n conc.Nursery ) error {
26
- for j := 0 ; j < routine ; j ++ {
27
- n .Go (func () error {
28
- return nil
29
- })
30
- }
31
- return nil
32
- })
33
- }
34
- })
35
- }
36
-
37
- for _ , routine := range []int {10 , 1000 , 100000 } {
38
- b .Run (fmt .Sprintf ("WithRoutines/%d/1msWork" , routine ), func (b * testing.B ) {
39
- for i := 0 ; i < b .N ; i ++ {
40
- conc .Block (func (n conc.Nursery ) error {
41
- for j := 0 ; j < routine ; j ++ {
42
- n .Go (func () error {
43
- time .Sleep (time .Millisecond )
44
- return nil
45
- })
46
- }
47
- return nil
48
- })
49
- }
50
- })
51
- }
52
-
53
- for _ , routine := range []int {10 , 1000 , 100000 } {
54
- b .Run (fmt .Sprintf ("WithRoutines/%d/1-10msWork" , routine ), func (b * testing.B ) {
55
- for i := 0 ; i < b .N ; i ++ {
56
- conc .Block (func (n conc.Nursery ) error {
57
- for j := 0 ; j < routine ; j ++ {
58
- k := j
25
+ b .Run ("WithRoutines/NoWork" , func (b * testing.B ) {
26
+ for i := 0 ; i < b .N ; i ++ {
27
+ conc .Block (func (n conc.Nursery ) error {
28
+ for j := 0 ; j < benchRoutineCount ; j ++ {
29
+ n .Go (func () error {
30
+ return nil
31
+ })
32
+ }
33
+ return nil
34
+ })
35
+ }
36
+ })
37
+
38
+ b .Run ("WithRoutines/Nested/NoWork" , func (b * testing.B ) {
39
+ for i := 0 ; i < b .N ; i ++ {
40
+ conc .Block (func (n conc.Nursery ) error {
41
+ for j := 0 ; j < benchRoutineCount ; j ++ {
42
+ n .Go (func () error {
59
43
n .Go (func () error {
60
- time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
61
44
return nil
62
45
})
63
- }
64
- return nil
65
- })
66
- }
67
- })
68
- }
69
-
70
- for _ , routine := range []int {10 , 1000 , 100000 } {
71
- b .Run (fmt .Sprintf ("WithRoutines/%d/Error" , routine ), func (b * testing.B ) {
72
- for i := 0 ; i < b .N ; i ++ {
73
- err := conc .Block (func (n conc.Nursery ) error {
74
- for j := 0 ; j < routine ; j ++ {
75
- n .Go (func () error {
76
- return io .EOF
77
- })
78
- }
79
- return nil
80
- })
81
- if err == nil {
82
- b .Fatal ("block returned nil error" )
46
+ return nil
47
+ })
48
+ }
49
+ return nil
50
+ })
51
+ }
52
+ })
53
+
54
+ b .Run ("WithRoutines/1msWork" , func (b * testing.B ) {
55
+ for i := 0 ; i < b .N ; i ++ {
56
+ conc .Block (func (n conc.Nursery ) error {
57
+ for j := 0 ; j < benchRoutineCount ; j ++ {
58
+ n .Go (func () error {
59
+ time .Sleep (time .Millisecond )
60
+ return nil
61
+ })
83
62
}
63
+ return nil
64
+ })
65
+ }
66
+ })
67
+
68
+ b .Run ("WithRoutines/1-10msWork" , func (b * testing.B ) {
69
+ for i := 0 ; i < b .N ; i ++ {
70
+ conc .Block (func (n conc.Nursery ) error {
71
+ for j := 0 ; j < benchRoutineCount ; j ++ {
72
+ k := j
73
+ n .Go (func () error {
74
+ time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
75
+ return nil
76
+ })
77
+ }
78
+ return nil
79
+ })
80
+ }
81
+ })
82
+
83
+ b .Run ("WithRoutines/Error" , func (b * testing.B ) {
84
+ for i := 0 ; i < b .N ; i ++ {
85
+ err := conc .Block (func (n conc.Nursery ) error {
86
+ for j := 0 ; j < benchRoutineCount ; j ++ {
87
+ n .Go (func () error {
88
+ return io .EOF
89
+ })
90
+ }
91
+ return nil
92
+ }, conc .WithIgnoreErrors ())
93
+ if err != nil {
94
+ b .Fatal ("block returned non-nil error" )
84
95
}
85
- })
86
- }
96
+ }
97
+ })
87
98
}
88
99
89
100
func BenchmarkSourceGraphConc (b * testing.B ) {
@@ -94,84 +105,100 @@ func BenchmarkSourceGraphConc(b *testing.B) {
94
105
}
95
106
})
96
107
97
- for _ , routine := range []int {10 , 1000 , 100000 } {
98
- b .Run (fmt .Sprintf ("WithRoutines/%d/NoWork" , routine ), func (b * testing.B ) {
99
- for i := 0 ; i < b .N ; i ++ {
100
- var p pool.Pool
101
- for j := 0 ; j < routine ; j ++ {
102
- p .Go (func () {
103
- })
104
- }
105
- p .Wait ()
108
+ b .Run ("WithRoutines/NoWork" , func (b * testing.B ) {
109
+ for i := 0 ; i < b .N ; i ++ {
110
+ var p pool.Pool
111
+ for j := 0 ; j < benchRoutineCount ; j ++ {
112
+ p .Go (func () {
113
+ })
106
114
}
107
- })
108
- }
109
-
110
- for _ , routine := range []int {10 , 1000 , 100000 } {
111
- b .Run (fmt .Sprintf ("WithRoutines/%d/1msWork" , routine ), func (b * testing.B ) {
112
- for i := 0 ; i < b .N ; i ++ {
113
- var p pool.Pool
114
- for j := 0 ; j < routine ; j ++ {
115
- p .Go (func () {
116
- time .Sleep (time .Millisecond )
117
- })
118
- }
119
- p .Wait ()
115
+ p .Wait ()
116
+ }
117
+ })
118
+
119
+ // for _, routine := range []int{10 /* 1000, 100000 */} {
120
+ // b.Run(fmt.Sprintf("WithRoutines/Nested%d/NoWork", routine), func(b *testing.B) {
121
+ // for i := 0; i < b.N; i++ {
122
+ // var p pool.Pool
123
+ // for j := 0; j < routine; j++ {
124
+ // p.Go(func() {
125
+ // p.Go(func() {
126
+ // })
127
+ // })
128
+ // }
129
+ // p.Wait()
130
+ // }
131
+ // })
132
+ // }
133
+
134
+ b .Run ("WithRoutines/1msWork" , func (b * testing.B ) {
135
+ for i := 0 ; i < b .N ; i ++ {
136
+ var p pool.Pool
137
+ for j := 0 ; j < benchRoutineCount ; j ++ {
138
+ p .Go (func () {
139
+ time .Sleep (time .Millisecond )
140
+ })
120
141
}
121
- })
122
- }
123
-
124
- for _ , routine := range []int {10 , 1000 , 100000 } {
125
- b .Run (fmt .Sprintf ("WithRoutines/%d/1-10msWork" , routine ), func (b * testing.B ) {
126
- for i := 0 ; i < b .N ; i ++ {
127
- var p pool.Pool
128
- for j := 0 ; j < routine ; j ++ {
129
- k := j
130
- p .Go (func () {
131
- time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
132
- })
133
- }
134
- p .Wait ()
142
+ p .Wait ()
143
+ }
144
+ })
145
+
146
+ b .Run ("WithRoutines/1-10msWork" , func (b * testing.B ) {
147
+ for i := 0 ; i < b .N ; i ++ {
148
+ var p pool.Pool
149
+ for j := 0 ; j < benchRoutineCount ; j ++ {
150
+ k := j
151
+ p .Go (func () {
152
+ time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
153
+ })
135
154
}
136
- })
137
- }
155
+ p .Wait ()
156
+ }
157
+ })
138
158
}
139
159
140
160
func BenchmarkGo (b * testing.B ) {
141
- for _ , routine := range []int {10 , 1000 , 100000 } {
142
- b .Run (fmt .Sprintf ("WithRoutines/%d/NoWork" , routine ), func (b * testing.B ) {
143
- for i := 0 ; i < b .N ; i ++ {
144
- for j := 0 ; j < routine ; j ++ {
145
- go func () error {
146
- return nil
147
- }()
148
- }
161
+ b .Run ("WithRoutines/NoWork" , func (b * testing.B ) {
162
+ for i := 0 ; i < b .N ; i ++ {
163
+ for j := 0 ; j < benchRoutineCount ; j ++ {
164
+ go func () error {
165
+ return nil
166
+ }()
149
167
}
150
- })
151
- }
168
+ }
169
+ })
152
170
153
- for _ , routine := range [] int { 10 , 1000 , 100000 } {
154
- b . Run ( fmt . Sprintf ( "WithRoutines/%d/1msWork" , routine ), func ( b * testing. B ) {
155
- for i := 0 ; i < b . N ; i ++ {
156
- for j := 0 ; j < routine ; j ++ {
171
+ b . Run ( "WithRoutines/Nested/NoWork" , func ( b * testing. B ) {
172
+ for i := 0 ; i < b . N ; i ++ {
173
+ for j := 0 ; j < benchRoutineCount ; j ++ {
174
+ go func () error {
157
175
go func () error {
158
- time .Sleep (time .Millisecond )
159
176
return nil
160
177
}()
161
- }
178
+ return nil
179
+ }()
162
180
}
163
- })
164
- }
165
-
166
- for _ , routine := range []int {10 , 1000 , 100000 } {
167
- b .Run (fmt .Sprintf ("WithRoutines/%d/1-10msWork" , routine ), func (b * testing.B ) {
168
- for i := 0 ; i < b .N ; i ++ {
169
- for j := 0 ; j < routine ; j ++ {
170
- go func (k int ) {
171
- time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
172
- }(j )
173
- }
181
+ }
182
+ })
183
+
184
+ b .Run ("WithRoutines/1msWork" , func (b * testing.B ) {
185
+ for i := 0 ; i < b .N ; i ++ {
186
+ for j := 0 ; j < benchRoutineCount ; j ++ {
187
+ go func () error {
188
+ time .Sleep (time .Millisecond )
189
+ return nil
190
+ }()
174
191
}
175
- })
176
- }
192
+ }
193
+ })
194
+
195
+ b .Run ("WithRoutines/1-10msWork" , func (b * testing.B ) {
196
+ for i := 0 ; i < b .N ; i ++ {
197
+ for j := 0 ; j < benchRoutineCount ; j ++ {
198
+ go func (k int ) {
199
+ time .Sleep (time .Duration (k % 10 ) * time .Millisecond )
200
+ }(j )
201
+ }
202
+ }
203
+ })
177
204
}
0 commit comments