forked from pion/dtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
122 lines (111 loc) · 2.74 KB
/
bench_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package dtls
import (
"context"
"crypto/tls"
"fmt"
"testing"
"time"
"github.com/pion/dtls/v3/pkg/crypto/selfsign"
dtlsnet "github.com/pion/dtls/v3/pkg/net"
"github.com/pion/logging"
"github.com/pion/transport/v3/dpipe"
"github.com/pion/transport/v3/test"
)
func TestSimpleReadWrite(t *testing.T) {
report := test.CheckRoutines(t)
defer report()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ca, cb := dpipe.Pipe()
certificate, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatal(err)
}
gotHello := make(chan struct{})
go func() {
server, sErr := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
LoggerFactory: logging.NewDefaultLoggerFactory(),
}, false)
if sErr != nil {
t.Error(sErr)
return
}
buf := make([]byte, 1024)
if _, sErr = server.Read(buf); sErr != nil { //nolint:contextcheck
t.Error(sErr)
}
gotHello <- struct{}{}
if sErr = server.Close(); sErr != nil { //nolint:contextcheck
t.Error(sErr)
}
}()
client, err := testClient(ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), &Config{
LoggerFactory: logging.NewDefaultLoggerFactory(),
InsecureSkipVerify: true,
}, false)
if err != nil {
t.Fatal(err)
}
if _, err = client.Write([]byte("hello")); err != nil {
t.Error(err)
}
select {
case <-gotHello:
// OK
case <-time.After(time.Second * 5):
t.Error("timeout")
}
if err = client.Close(); err != nil {
t.Error(err)
}
}
func benchmarkConn(b *testing.B, n int64) {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
ctx := context.Background()
ca, cb := dpipe.Pipe()
certificate, err := selfsign.GenerateSelfSigned()
server := make(chan *Conn)
go func() {
s, sErr := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), &Config{
Certificates: []tls.Certificate{certificate},
}, false)
if err != nil {
b.Error(sErr)
return
}
server <- s
}()
if err != nil {
b.Fatal(err)
}
hw := make([]byte, n)
b.ReportAllocs()
b.SetBytes(int64(len(hw)))
go func() {
client, cErr := testClient(ctx, dtlsnet.PacketConnFromConn(ca), ca.RemoteAddr(), &Config{InsecureSkipVerify: true}, false)
if cErr != nil {
b.Error(err)
}
for {
if _, cErr = client.Write(hw); cErr != nil { //nolint:contextcheck
b.Error(err)
}
}
}()
s := <-server
buf := make([]byte, 2048)
for i := 0; i < b.N; i++ {
if _, err = s.Read(buf); err != nil {
b.Error(err)
}
}
})
}
func BenchmarkConnReadWrite(b *testing.B) {
for _, n := range []int64{16, 128, 512, 1024, 2048} {
benchmarkConn(b, n)
}
}