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