Source file
test/chanlinear.go
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "fmt"
15 "runtime"
16 "time"
17 )
18
19
20
21 func checkLinear(typ string, tries int, f func(n int)) {
22
23
24
25
26
27 timeF := func(n int) time.Duration {
28 t1 := time.Now()
29 f(n)
30 return time.Since(t1)
31 }
32
33 t0 := time.Now()
34
35 n := tries
36 fails := 0
37 for {
38 runtime.GC()
39 t1 := timeF(n)
40 runtime.GC()
41 t2 := timeF(2 * n)
42
43
44 if t2 < 3*t1 {
45 if false {
46 fmt.Println(typ, "\t", time.Since(t0))
47 }
48 return
49 }
50
51
52
53
54 if t1 < 1*time.Second {
55 n *= 2
56 continue
57 }
58
59
60
61 if fails++; fails >= 5 {
62 panic(fmt.Sprintf("%s: too slow: %d channels: %v; %d channels: %v\n",
63 typ, n, t1, 2*n, t2))
64 }
65 }
66 }
67
68 func main() {
69 checkLinear("chanSelect", 1000, func(n int) {
70 const messages = 10
71 c := make(chan bool)
72 var a []chan bool
73 for i := 0; i < n; i++ {
74 d := make(chan bool)
75 a = append(a, d)
76 go func() {
77 for j := 0; j < messages; j++ {
78
79 select {
80 case <-c:
81 case <-d:
82 }
83 }
84 }()
85 }
86 for i := 0; i < messages; i++ {
87
88
89 for _, d := range a {
90 d <- true
91 }
92 }
93 })
94 }
95
View as plain text