-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_loop.go
104 lines (99 loc) · 1.92 KB
/
select_loop.go
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
package main
import (
"flag"
"fmt"
"prof"
"sync"
)
import "time"
var nsec = flag.Int("nsec", 2, "Time to run in seconds")
var cases = flag.Int("cases", 0, "Number of case statements (0=no select loop)")
func main() {
flag.Parse()
var wg sync.WaitGroup
wg.Add(1)
m := make(map[int]int)
for i := 0; i < 100000; i++ {
m[i] = i
}
n := 0
p := prof.StartProfile()
start := time.Now()
go func() {
switch *cases {
case -1:
duration := time.Now().Add(time.Duration(*nsec) * time.Second)
for duration.After(time.Now()) {
_ = m[n%100000]
n = n + 1
}
wg.Done()
return
case 0:
duration := time.Now().Add(time.Duration(*nsec) * time.Second)
for {
select {
default:
_ = m[n%100000]
n = n + 1
if time.Now().After(duration) {
wg.Done()
return
}
}
}
case 1:
done := time.NewTicker(time.Duration(*nsec) * time.Second).C
for {
select {
case <-done:
wg.Done()
return
default:
_ = m[n%100000]
n = n + 1
}
}
case 2:
done := time.NewTicker(time.Duration(*nsec) * time.Second).C
tm := time.NewTicker(time.Duration(*nsec) * time.Millisecond * 100).C
for {
select {
case <-done:
wg.Done()
return
case <-tm:
_ = m[n%100000]
n = n + 1
default:
_ = m[n%100000]
n = n + 1
}
}
case 3:
done := time.NewTicker(time.Duration(*nsec) * time.Second).C
tm := time.NewTicker(time.Duration(*nsec) * time.Millisecond * 100).C
tm2 := time.NewTicker(time.Duration(*nsec) * time.Millisecond * 101).C
for {
select {
case <-done:
wg.Done()
return
case <-tm:
_ = m[n%100000]
n = n + 1
case <-tm2:
_ = m[n%100000]
n = n + 1
default:
_ = m[n%100000]
n = n + 1
}
}
}
}()
wg.Wait()
end := time.Since(start)
p.Stop()
fmt.Printf("cases: %v \tnitr: %v \ttime: %v \trate: %v\n", *cases, n, end, float64(n)/end.Seconds())
}