Skip to content

Commit 606b6a3

Browse files
committed
rework benchmarks
1 parent 8a6190e commit 606b6a3

File tree

2 files changed

+156
-129
lines changed

2 files changed

+156
-129
lines changed

bench/bench_test.go

+155-128
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"testing"
76
"time"
@@ -10,6 +9,10 @@ import (
109
"github.com/sourcegraph/conc/pool"
1110
)
1211

12+
const (
13+
benchRoutineCount = 100
14+
)
15+
1316
func BenchmarkNursery(b *testing.B) {
1417
b.Run("EmptyBlock", func(b *testing.B) {
1518
for i := 0; i < b.N; i++ {
@@ -19,71 +22,79 @@ func BenchmarkNursery(b *testing.B) {
1922
}
2023
})
2124

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 {
5943
n.Go(func() error {
60-
time.Sleep(time.Duration(k%10) * time.Millisecond)
6144
return nil
6245
})
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+
})
8362
}
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")
8495
}
85-
})
86-
}
96+
}
97+
})
8798
}
8899

89100
func BenchmarkSourceGraphConc(b *testing.B) {
@@ -94,84 +105,100 @@ func BenchmarkSourceGraphConc(b *testing.B) {
94105
}
95106
})
96107

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+
})
106114
}
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+
})
120141
}
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+
})
135154
}
136-
})
137-
}
155+
p.Wait()
156+
}
157+
})
138158
}
139159

140160
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+
}()
149167
}
150-
})
151-
}
168+
}
169+
})
152170

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 {
157175
go func() error {
158-
time.Sleep(time.Millisecond)
159176
return nil
160177
}()
161-
}
178+
return nil
179+
}()
162180
}
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+
}()
174191
}
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+
})
177204
}

bench/prof.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515

1616
for {
1717
conc.Block(func(n conc.Nursery) error {
18-
for i := 0; i < 1000; i++ {
18+
for i := 0; i < 10; i++ {
1919
n.Go(func() error {
2020
return nil
2121
})

0 commit comments

Comments
 (0)