-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchs_test.go
275 lines (232 loc) · 5.39 KB
/
benchs_test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"crypto/rand"
"fmt"
"net"
"os"
"runtime/pprof"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
)
var portIdx atomic.Int64
var readersCount = 100
const (
defaultReadBufferSize = 8 * 1024 * 1024
defaultWriteBufferSize = 8 * 1024 * 1024
)
func newUDPSocket() (fd int, port int, err error) {
// Create local udp socket on any random port
fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
panic(err)
}
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if err != nil {
panic(err)
}
addr := [4]byte{127, 0, 0, 1}
portBase := 5000
for {
port = int(portIdx.Add(1) + int64(portBase))
err = syscall.Bind(fd, &syscall.SockaddrInet4{
Port: port,
Addr: addr,
})
if err == nil {
break
}
port++
}
err = syscall.SetNonblock(fd, false)
return
}
// DO NOT MODIFY THIS FUNCTION
func testInit(readersCount int, verbose bool) (ports []int, readChan chan []byte, closeChan chan struct{}, err error) {
ports = make([]int, readersCount)
portsChan := make(chan int, readersCount)
readChan = make(chan []byte, readersCount)
closeChan = make(chan struct{}, 1)
// Create readersCount udp sockets to read
wg := sync.WaitGroup{}
for i := 0; i < readersCount; i++ {
wg.Add(1)
go func(threadId int) {
fd, port, err := newUDPSocket()
if err != nil {
return
}
portsChan <- port
buf := make([]byte, 1500)
wg.Done()
for {
select {
case <-closeChan:
return
default:
n, _, err := syscall.Recvfrom(fd, buf, 0)
if err != nil {
// Close the socket
syscall.Close(fd)
return
}
if verbose {
data := string(buf[:n])
fmt.Println("threadId", threadId, "read: ", n, "bytes", "data: ", data)
}
readChan <- buf[:n]
}
}
}(i)
}
wg.Wait()
for i := 0; i < readersCount; i++ {
ports[i] = <-portsChan
}
return
}
// DO NOT MODIFY THIS FUNCTION
func getTestMsg() []byte {
// Generate a 1500 byte random message
buf := make([]byte, 1500)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}
// DO NOT MODIFY THIS FUNCTION
func waitForReaders(readChan chan []byte, b *testing.B) {
// Wait for reader to read
for i := 0; i < readersCount; i++ {
select {
case <-readChan:
case <-time.After(1 * time.Second):
b.Fatal("timeout") // This should not happen
}
}
}
func BenchmarkConnections(b *testing.B) {
b.Run("baseline", func(b *testing.B) {
BenchmarkRawUDP(b)
})
b.Run("Sample", func(b *testing.B) {
BenchmarkSample(b)
})
}
func BenchmarkRawUDP(b *testing.B) {
b.StopTimer()
// Create a file for storing the CPU profile
f, err := os.Create("cpu2.out")
if err != nil {
fmt.Println("Could not create profile file:", err)
return
}
defer f.Close()
// Start CPU profiling
if err := pprof.StartCPUProfile(f); err != nil {
fmt.Println("Could not start profiling:", err)
return
}
defer pprof.StopCPUProfile() // Stop profiling at the end
testPort := 40101
// Create a udp network connection
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: testPort,
})
if err != nil {
b.Fatal(err)
}
ports, readChan, closeChan, err := testInit(readersCount, false)
if err != nil {
b.Fatal(err)
}
_ = readChan
writer := func() {
for i := 0; i < readersCount; i++ {
buf := getTestMsg()
_, err := conn.WriteTo(buf, &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: ports[i],
})
if err != nil {
b.Fatal(err)
}
}
// End of code that you are permitted to modify
waitForReaders(readChan, b)
}
// Sequential test
b.StartTimer()
for i := 0; i < b.N; i++ {
writer()
}
b.StopTimer()
close(closeChan)
}
// using sendTo function and pre-allocated Buffers
func BenchmarkSample(b *testing.B) {
b.StopTimer()
// Create a file for storing the CPU profile
f, err := os.Create("cpu1.out")
if err != nil {
fmt.Println("Could not create profile file:", err)
return
}
defer f.Close()
// Start CPU profiling
if err := pprof.StartCPUProfile(f); err != nil {
fmt.Println("Could not start profiling:", err)
return
}
defer pprof.StopCPUProfile() // Stop profiling at the end
ports, readChan, closeChan, err := testInit(readersCount, false) // DO NOT EDIT THIS LINE
if err != nil {
b.Fatal(err)
}
_ = readChan
fd, _, err := newUDPSocket()
if err != nil {
panic(err)
}
defer func() {
syscall.Close(fd)
}()
remoteAddr := make([]syscall.SockaddrInet4, readersCount)
var addr *net.UDPAddr
var raddr *syscall.SockaddrInet4
for i := 0; i < readersCount; i++ {
addr = &net.UDPAddr{Port: ports[i], IP: net.IPv4(127, 0, 0, 1)}
raddr = &syscall.SockaddrInet4{Port: addr.Port, Addr: [4]byte{addr.IP[12], addr.IP[13], addr.IP[14], addr.IP[15]}}
remoteAddr[i] = *raddr
}
preAllocBuffers := make([][]byte, readersCount)
for i := range preAllocBuffers {
preAllocBuffers[i] = getTestMsg()
}
var wg sync.WaitGroup
writer := func() {
for i := 0; i < readersCount; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
err = syscall.Sendto(fd, preAllocBuffers[i], syscall.MSG_DONTWAIT, &remoteAddr[i])
if err != nil {
panic(err)
}
}(i)
}
wg.Wait()
waitForReaders(readChan, b)
}
// Sequential test
b.StartTimer()
for i := 0; i < b.N; i++ {
writer()
}
b.StopTimer()
close(closeChan)
}