-
Notifications
You must be signed in to change notification settings - Fork 14
/
server_bench_test.go
60 lines (50 loc) · 1.21 KB
/
server_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
package dnscrypt
import (
"fmt"
"net"
"testing"
"time"
"github.com/ameshkov/dnsstamps"
"github.com/stretchr/testify/require"
)
func BenchmarkServeUDP(b *testing.B) {
benchmarkServe(b, "udp")
}
func BenchmarkServeTCP(b *testing.B) {
benchmarkServe(b, "tcp")
}
func benchmarkServe(b *testing.B, network string) {
srv := newTestServer(b, &testHandler{})
b.Cleanup(func() {
err := srv.Close()
require.NoError(b, err)
})
client := &Client{
Timeout: 1 * time.Second,
Net: network,
}
serverAddr := fmt.Sprintf("127.0.0.1:%d", srv.UDPAddr().Port)
if network == "tcp" {
serverAddr = fmt.Sprintf("127.0.0.1:%d", srv.TCPAddr().Port)
}
stamp := dnsstamps.ServerStamp{
ServerAddrStr: serverAddr,
ServerPk: srv.resolverPk,
ProviderName: srv.server.ProviderName,
Proto: dnsstamps.StampProtoTypeDNSCrypt,
}
ri, err := client.DialStamp(stamp)
require.NoError(b, err)
require.NotNil(b, ri)
conn, err := net.Dial(network, stamp.ServerAddrStr)
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := createTestMessage()
res, err := client.ExchangeConn(conn, m, ri)
require.NoError(b, err)
assertTestMessageResponse(b, res)
}
b.StopTimer()
}