-
Notifications
You must be signed in to change notification settings - Fork 23
/
benchmark.go
144 lines (122 loc) · 2.79 KB
/
benchmark.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Golang implementation of this project's benchmark.py file.
It should be a pretty direct mirror.
The only questionable decision is the use of reflect.SelectCase,
rather than Go's select statement.
goless' select function is based on reflect.SelectCase,
so this is "idiotmatic goless" (ugh),
but Go programs rarely use reflect.SelectCase and instead use its
select function.
Using the select statement would probably speed things up.
I'm not sure what the right thing to do is,
so I chose the easier thing.
*/
package main
import (
"fmt"
"reflect"
"runtime"
"time"
)
const queueLen int = 10000
type timing float64
func benchChannel(chanSize int) timing {
c := make(chan int, chanSize)
go func() {
for i := 0; i <= queueLen; i++ {
c <- 0
}
close(c)
}()
start := time.Now()
for i := 0; i < queueLen; i++ {
<-c
}
elapsed := time.Since(start)
return timing(elapsed.Seconds())
}
func benchChannels() {
tookSync := benchChannel(0)
writeResult("chan_sync", tookSync)
tookAsync := benchChannel(queueLen + 1)
writeResult("chan_async", tookAsync)
tookBuff := benchChannel(1000)
writeResult("chan_buff", tookBuff)
}
func benchSelect(useDefault bool) timing {
c := make(chan int, 0)
go func() {
for {
c <- 0
<-c
}
}()
start := time.Now()
if useDefault {
for i := 0; i <= queueLen; i++ {
select {
case c <- 0: // pass
case <-c: // pass
case c <- 0: // pass
case <-c: //pass
}
}
} else {
for i := 0; i <= queueLen; i++ {
select {
case c <- 0: // pass
case <-c: // pass
case c <- 0: // pass
case <-c: // pass
default: // pass
}
}
}
elapsed := time.Since(start)
return timing(elapsed.Seconds())
}
func benchReflectSelect(useDefault bool) timing {
c := make(chan int, 0)
caseCnt := 4
if useDefault {
caseCnt = 5
}
cases := make([]reflect.SelectCase, caseCnt)
cases[0] = reflect.SelectCase{Dir: reflect.SelectSend, Chan: reflect.ValueOf(c), Send: reflect.ValueOf(0)}
cases[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(c)}
cases[2] = cases[0]
cases[3] = cases[1]
if useDefault {
cases[4] = reflect.SelectCase{Dir: reflect.SelectDefault}
}
go func() {
for {
c <- 0
<-c
}
}()
start := time.Now()
for i := 0; i <= queueLen; i++ {
reflect.Select(cases)
}
elapsed := time.Since(start)
return timing(elapsed.Seconds())
}
func benchSelects() {
var took timing
took = benchSelect(false)
writeResult("select", took)
took = benchSelect(true)
writeResult("select_default", took)
took = benchReflectSelect(false)
writeResult("r-select", took)
took = benchReflectSelect(true)
writeResult("r-select_default", took)
}
func writeResult(benchName string, elapsed timing) {
fmt.Printf("go %s %s %.5f\n", runtime.Compiler, benchName, elapsed)
}
func main() {
benchChannels()
benchSelects()
}