@@ -33,87 +33,11 @@ func BenchmarkTLSStreaming(b *testing.B) {
3333 benchmark (b , streamingHandler , true )
3434}
3535
36- // BenchmarkTLSHandshake measures end-to-end TLS handshake performance
37- // for fasthttp client and server.
38- //
39- // It re-establishes new TLS connection per each http request.
40- func BenchmarkTLSHandshakeRSAWithClientSessionCache (b * testing.B ) {
41- bc := & benchConfig {
42- IsTLS : true ,
43- DisableClientSessionCache : false ,
44- }
45- benchmarkExt (b , handshakeHandler , bc )
46- }
47-
48- func BenchmarkTLSHandshakeRSAWithoutClientSessionCache (b * testing.B ) {
49- bc := & benchConfig {
50- IsTLS : true ,
51- DisableClientSessionCache : true ,
52- }
53- benchmarkExt (b , handshakeHandler , bc )
54- }
55-
56- func BenchmarkTLSHandshakeECDSAWithClientSessionCache (b * testing.B ) {
57- bc := & benchConfig {
58- IsTLS : true ,
59- DisableClientSessionCache : false ,
60- UseECDSA : true ,
61- }
62- benchmarkExt (b , handshakeHandler , bc )
63- }
64-
65- func BenchmarkTLSHandshakeECDSAWithoutClientSessionCache (b * testing.B ) {
66- bc := & benchConfig {
67- IsTLS : true ,
68- DisableClientSessionCache : true ,
69- UseECDSA : true ,
70- }
71- benchmarkExt (b , handshakeHandler , bc )
72- }
73-
74- func BenchmarkTLSHandshakeECDSAWithCurvesWithClientSessionCache (b * testing.B ) {
75- bc := & benchConfig {
76- IsTLS : true ,
77- DisableClientSessionCache : false ,
78- UseCurves : true ,
79- UseECDSA : true ,
80- }
81- benchmarkExt (b , handshakeHandler , bc )
82- }
83-
84- func BenchmarkTLSHandshakeECDSAWithCurvesWithoutClientSessionCache (b * testing.B ) {
85- bc := & benchConfig {
86- IsTLS : true ,
87- DisableClientSessionCache : true ,
88- UseCurves : true ,
89- UseECDSA : true ,
90- }
91- benchmarkExt (b , handshakeHandler , bc )
92- }
93-
9436func benchmark (b * testing.B , h fasthttp.RequestHandler , isTLS bool ) {
95- bc := & benchConfig {
96- IsTLS : isTLS ,
97- }
98- benchmarkExt (b , h , bc )
99- }
100-
101- type benchConfig struct {
102- IsTLS bool
103- DisableClientSessionCache bool
104- UseCurves bool
105- UseECDSA bool
106- }
107-
108- func benchmarkExt (b * testing.B , h fasthttp.RequestHandler , bc * benchConfig ) {
10937 var serverTLSConfig , clientTLSConfig * tls.Config
110- if bc . IsTLS {
38+ if isTLS {
11139 certFile := "rsa.pem"
11240 keyFile := "rsa.key"
113- if bc .UseECDSA {
114- certFile = "ecdsa.pem"
115- keyFile = "ecdsa.key"
116- }
11741 cert , err := tls .LoadX509KeyPair (certFile , keyFile )
11842 if err != nil {
11943 b .Fatalf ("cannot load TLS certificate from certFile=%q, keyFile=%q: %v" , certFile , keyFile , err )
@@ -123,17 +47,9 @@ func benchmarkExt(b *testing.B, h fasthttp.RequestHandler, bc *benchConfig) {
12347 PreferServerCipherSuites : true ,
12448 }
12549 serverTLSConfig .CurvePreferences = []tls.CurveID {}
126- if bc .UseCurves {
127- serverTLSConfig .CurvePreferences = []tls.CurveID {
128- tls .CurveP256 ,
129- }
130- }
13150 clientTLSConfig = & tls.Config {
13251 InsecureSkipVerify : true ,
13352 }
134- if bc .DisableClientSessionCache {
135- clientTLSConfig .ClientSessionCache = fakeSessionCache {}
136- }
13753 }
13854 ln := fasthttputil .NewInmemoryListener ()
13955 serverStopCh := make (chan struct {})
@@ -151,12 +67,12 @@ func benchmarkExt(b *testing.B, h fasthttp.RequestHandler, bc *benchConfig) {
15167 Dial : func (addr string ) (net.Conn , error ) {
15268 return ln .Dial ()
15369 },
154- IsTLS : clientTLSConfig != nil ,
70+ IsTLS : isTLS ,
15571 TLSConfig : clientTLSConfig ,
15672 }
15773
15874 b .RunParallel (func (pb * testing.PB ) {
159- runRequests (b , pb , c )
75+ runRequests (b , pb , c , isTLS )
16076 })
16177 ln .Close ()
16278 <- serverStopCh
@@ -173,9 +89,13 @@ func handshakeHandler(ctx *fasthttp.RequestCtx) {
17389 ctx .SetConnectionClose ()
17490}
17591
176- func runRequests (b * testing.B , pb * testing.PB , c * fasthttp.HostClient ) {
92+ func runRequests (b * testing.B , pb * testing.PB , c * fasthttp.HostClient , isTLS bool ) {
17793 var req fasthttp.Request
178- req .SetRequestURI ("http://foo.bar/baz" )
94+ if isTLS {
95+ req .SetRequestURI ("https://foo.bar/baz" )
96+ } else {
97+ req .SetRequestURI ("http://foo.bar/baz" )
98+ }
17999 var resp fasthttp.Response
180100 for pb .Next () {
181101 if err := c .Do (& req , & resp ); err != nil {
@@ -186,13 +106,3 @@ func runRequests(b *testing.B, pb *testing.PB, c *fasthttp.HostClient) {
186106 }
187107 }
188108}
189-
190- type fakeSessionCache struct {}
191-
192- func (fakeSessionCache ) Get (sessionKey string ) (* tls.ClientSessionState , bool ) {
193- return nil , false
194- }
195-
196- func (fakeSessionCache ) Put (sessionKey string , cs * tls.ClientSessionState ) {
197- // no-op
198- }
0 commit comments