-
Notifications
You must be signed in to change notification settings - Fork 45
/
benchmark_test.go
473 lines (406 loc) · 9.83 KB
/
benchmark_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package muxado
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"net"
"sync"
"testing"
"time"
"github.com/hashicorp/yamux"
"golang.org/x/crypto/ssh"
)
type muxSession interface {
OpenStream() (muxStream, error)
AcceptStream() (muxStream, error)
Wait() (error, error, []byte)
}
type muxStream interface {
Read([]byte) (int, error)
Write([]byte) (int, error)
//CloseWrite() error
Close() error
}
func BenchmarkPayload1BStreams1(b *testing.B) {
testCase(b, 1, 1)
}
func BenchmarkPayload1KBStreams1(b *testing.B) {
testCase(b, 1024, 1)
}
func BenchmarkPayload1MBStreams1(b *testing.B) {
testCase(b, 1024*1024, 1)
}
func BenchmarkPayload32MBStreams1(b *testing.B) {
testCase(b, 32*1024*1024, 1)
}
func BenchmarkPayload1BStreams4(b *testing.B) {
testCase(b, 1, 4)
}
func BenchmarkPayload1KBStreams4(b *testing.B) {
testCase(b, 1024, 4)
}
func BenchmarkPayload1MBStreams4(b *testing.B) {
testCase(b, 1024*1024, 4)
}
func BenchmarkPayload32MBStreams4(b *testing.B) {
testCase(b, 32*1024*1024, 4)
}
func BenchmarkPayload1BStreams8(b *testing.B) {
testCase(b, 1, 8)
}
func BenchmarkPayload1KBStreams8(b *testing.B) {
testCase(b, 1024, 8)
}
func BenchmarkPayload1MBStreams8(b *testing.B) {
testCase(b, 1024*1024, 8)
}
func BenchmarkPayload32MBStreams8(b *testing.B) {
testCase(b, 32*1024*1024, 8)
}
func BenchmarkPayload1BStreams64(b *testing.B) {
testCase(b, 1, 64)
}
func BenchmarkPayload1KBStreams64(b *testing.B) {
testCase(b, 1024, 64)
}
func BenchmarkPayload1MBStreams64(b *testing.B) {
testCase(b, 1024*1024, 64)
}
func BenchmarkPayload32MBStreams64(b *testing.B) {
testCase(b, 32*1024*1024, 64)
}
func BenchmarkPayload1BStreams256(b *testing.B) {
testCase(b, 1, 256)
}
func BenchmarkPayload1KBStreams256(b *testing.B) {
testCase(b, 1024, 256)
}
func BenchmarkPayload1MBStreams256(b *testing.B) {
testCase(b, 1024*1024, 256)
}
func testCase(b *testing.B, payloadSize int64, concurrency int) {
done := make(chan int)
c, s := tlsTransport()
sessFactory := newMuxadoAdaptor
//sessFactory := newYamuxAdaptor
//sessFactory := newSSHAdaptor
go func() { server(b, sessFactory(s, true), payloadSize, concurrency, done) }()
go client(b, sessFactory(c, false), payloadSize)
<-done
}
func server(b *testing.B, sess muxSession, payloadSize int64, concurrency int, done chan int) {
go wait(b, sess, "server")
p := new(alot)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
wg.Add(concurrency)
start := make(chan int)
for c := 0; c < concurrency; c++ {
go func() {
<-start
str, err := sess.OpenStream()
if err != nil {
panic(err)
}
go func() {
_, err := io.CopyN(ioutil.Discard, str, payloadSize)
if err != nil {
panic(err)
}
wg.Done()
str.Close()
}()
n, err := io.CopyN(str, p, payloadSize)
if n != payloadSize {
b.Errorf("Server failed to send full payload. Got %d, expected %d", n, payloadSize)
}
if err != nil {
panic(err)
}
}()
}
close(start)
wg.Wait()
}
close(done)
}
func client(b *testing.B, sess muxSession, expectedSize int64) {
go wait(b, sess, "client")
for {
str, err := sess.AcceptStream()
if err != nil {
panic(err)
}
go func(s muxStream) {
n, err := io.CopyN(s, s, expectedSize)
if err != nil {
panic(err)
}
s.Close()
if n != expectedSize {
b.Errorf("stream with wrong size: %d, expected %d", n, expectedSize)
}
}(str)
}
}
func wait(b *testing.B, sess muxSession, name string) {
localErr, remoteErr, _ := sess.Wait()
localCode, _ := GetError(localErr)
remoteCode, _ := GetError(remoteErr)
fmt.Printf("'%s' session died with local err %v (code 0x%x), and remote err %v (code 0x%x)\n", name, localErr, localCode, remoteErr, remoteCode)
if localCode != NoError || remoteCode != NoError {
b.Errorf("bad session shutdown")
}
}
var sourceBuf = bytes.Repeat([]byte("0123456789"), 12800)
type alot struct{}
func (a *alot) Read(p []byte) (int, error) {
copy(p, sourceBuf)
return len(p), nil
}
func tcpTransport() (net.Conn, net.Conn) {
l, port := listener()
defer l.Close()
c := make(chan net.Conn)
s := make(chan net.Conn)
go func() {
conn, err := l.Accept()
if err != nil {
panic(err)
}
s <- conn
}()
go func() {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
panic(err)
}
c <- conn
}()
return <-c, <-s
}
func tlsTransport() (net.Conn, net.Conn) {
c, s := tcpTransport()
_, ca, err := genCert("Snakeoil CA", nil)
if err != nil {
panic(err)
}
roots := x509.NewCertPool()
roots.AddCert(ca)
clientTLSConf := &tls.Config{RootCAs: roots}
if err != nil {
panic(err)
}
serverCert, _, err := genCert("snakeoil.dev", ca)
if err != nil {
panic(err)
}
return tls.Client(c, clientTLSConf), tls.Server(s, &tls.Config{Certificates: []tls.Certificate{*serverCert}})
}
func genCert(cn string, parent *x509.Certificate) (*tls.Certificate, *x509.Certificate, error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: cn,
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment,
BasicConstraintsValid: true,
DNSNames: []string{cn},
}
if parent == nil {
parent = &template
}
certBytes, err := x509.CreateCertificate(rand.Reader, &template, parent, &key.PublicKey, key)
if err != nil {
return nil, nil, err
}
x509Certs, err := x509.ParseCertificates(certBytes)
if err != nil {
return nil, nil, err
}
return &tls.Certificate{
Certificate: [][]byte{certBytes},
PrivateKey: key,
}, x509Certs[0], nil
}
func listener() (net.Listener, int) {
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port := l.Addr().(*net.TCPAddr).Port
return l, port
}
type duplexPipe struct {
*io.PipeReader
*io.PipeWriter
}
func (dp *duplexPipe) Close() error {
dp.PipeReader.Close()
dp.PipeWriter.Close()
return nil
}
func memTransport() (io.ReadWriteCloser, io.ReadWriteCloser) {
rd1, wr1 := io.Pipe()
rd2, wr2 := io.Pipe()
client := &duplexPipe{rd1, wr2}
server := &duplexPipe{rd2, wr1}
return client, server
}
type muxadoAdaptor struct {
Session
}
func (a *muxadoAdaptor) OpenStream() (muxStream, error) {
return a.Session.OpenStream()
}
func (a *muxadoAdaptor) AcceptStream() (muxStream, error) {
return a.Session.AcceptStream()
}
func newMuxadoAdaptor(rwc io.ReadWriteCloser, isServer bool) muxSession {
newSess := Client
if isServer {
newSess = Server
}
return &muxadoAdaptor{newSess(rwc, new(Config))}
}
type yamuxAdaptor struct {
*yamux.Session
}
func (a *yamuxAdaptor) OpenStream() (muxStream, error) {
str, err := a.Session.OpenStream()
return str, err
}
func (a *yamuxAdaptor) AcceptStream() (muxStream, error) {
str, err := a.Session.AcceptStream()
return str, err
}
func (a *yamuxAdaptor) Wait() (error, error, []byte) {
select {}
}
func newYamuxAdaptor(rwc io.ReadWriteCloser, isServer bool) muxSession {
newSess := yamux.Client
if isServer {
newSess = yamux.Server
}
sess, err := newSess(rwc, yamux.DefaultConfig())
if err != nil {
panic(err)
}
return &yamuxAdaptor{sess}
}
type sshAdaptor struct {
ssh.Conn
channels <-chan ssh.NewChannel
}
func (a *sshAdaptor) OpenStream() (muxStream, error) {
c, reqs, err := a.Conn.OpenChannel("", []byte{})
if err != nil {
return nil, err
}
go ssh.DiscardRequests(reqs)
return c, nil
}
func (a *sshAdaptor) AcceptStream() (muxStream, error) {
newChannel, ok := <-a.channels
if !ok {
return nil, errors.New("SSH Session closed")
}
channel, reqs, err := newChannel.Accept()
if err != nil {
return nil, err
}
go ssh.DiscardRequests(reqs)
return channel, nil
}
func (a *sshAdaptor) Wait() (error, error, []byte) {
return a.Conn.Wait(), nil, nil
}
func newSSHAdaptor(rwc io.ReadWriteCloser, isServer bool) muxSession {
var (
conn ssh.Conn
newChannels <-chan ssh.NewChannel
globalRequests <-chan *ssh.Request
err error
)
if isServer {
sconf := &ssh.ServerConfig{NoClientAuth: true}
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
signer, err := ssh.NewSignerFromKey(privKey)
if err != nil {
panic(err)
}
sconf.AddHostKey(signer)
conn, newChannels, globalRequests, err = ssh.NewServerConn(&rwcConn{rwc}, sconf)
} else {
conn, newChannels, globalRequests, err = ssh.NewClientConn(&rwcConn{rwc}, "", new(ssh.ClientConfig))
}
if err != nil {
panic(err)
}
go ssh.DiscardRequests(globalRequests)
return &sshAdaptor{conn, newChannels}
}
type rwcConn struct {
io.ReadWriteCloser
}
func (c *rwcConn) LocalAddr() net.Addr { return nil }
func (c *rwcConn) RemoteAddr() net.Addr { return nil }
func (c *rwcConn) SetDeadline(time.Time) error { return nil }
func (c *rwcConn) SetReadDeadline(time.Time) error { return nil }
func (c *rwcConn) SetWriteDeadline(time.Time) error { return nil }
type tlsMode int
const (
tlsModeClient tlsMode = iota
tlsModeServer
tlsModeNone
)
type tcpAdaptor struct {
l net.Listener
remotePort string
doTLS tlsMode
done chan error
}
func (a *tcpAdaptor) OpenStream() (muxStream, error) {
conn, err := net.Dial("tcp", "127.0.0.1:"+a.remotePort)
if err != nil {
return nil, err
}
return a.wrapTLS(conn), nil
}
func (a *tcpAdaptor) AcceptStream() (muxStream, error) {
conn, err := a.l.Accept()
if err != nil {
return nil, err
}
return a.wrapTLS(conn), nil
}
func (a *tcpAdaptor) wrapTLS(c net.Conn) net.Conn {
// XXX
return c
}
func (a *tcpAdaptor) Wait() (error, error, []byte) {
return <-a.done, nil, nil
}