forked from corestoreio/caddy-esi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_bm_test.go
133 lines (116 loc) · 4.16 KB
/
middleware_bm_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
// Copyright 2015-2017, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package caddyesi_test
import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/corestoreio/caddy-esi"
"github.com/corestoreio/caddy-esi/esitag"
"github.com/corestoreio/caddy-esi/esitesting"
"github.com/corestoreio/errors"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func BenchmarkMiddleware(b *testing.B) {
defer esitag.RegisterResourceHandler("bmServe01", esitesting.MockRequestContent("Hello 2017!")).DeferredDeregister()
const serveFile = `testdata/page03.html`
const caddyFile = `esi {
timeout 19s
on_error "my important global error message"
allowed_methods GET
# log_file ./benchmark_serve.log
# log_level debug
}`
fileStat, err := os.Stat(serveFile)
if err != nil {
b.Fatal(err)
}
fileSize := fileStat.Size()
ctc := caddy.NewTestController("http", caddyFile)
if err := caddyesi.PluginSetup(ctc); err != nil {
b.Fatal(err)
}
httpserver.GetConfig(ctc).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
f, err := os.Open(serveFile)
if err != nil {
b.Fatal(err)
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
b.Fatal(err)
}
return http.StatusOK, nil
})
})
mids := httpserver.GetConfig(ctc).Middleware()
finalHandler := httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusNotImplemented, errors.New("Should not be called! Or File not found")
})
var stack httpserver.Handler = finalHandler
for i := len(mids) - 1; i >= 0; i-- {
stack = mids[i](stack)
}
req := httptest.NewRequest("GET", "https://my.blog/any/path", nil)
req.Header = http.Header{
"Host": []string{"www.example.com"},
"Connection": []string{"keep-alive"},
"Pragma": []string{"no-cache"},
"Cache-Control": []string{"no-cache"},
"Upgrade-Insecure-Requests": []string{"1"},
"User-Agent": []string{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10)"},
"Accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
"DNT": []string{"1"},
"Referer": []string{"https://www.example.com/"},
"Accept-Encoding": []string{"gzip, deflate, sdch, br"},
"Avail-Dictionary": []string{"lhdx6rYE"},
"Accept-Language": []string{"en-US,en;q=0.8"},
"Cookie": []string{"x-wl-uid=1vnTVF5WyZIe5Fymf2a4H+pFPyJa4wxNmzCKdImj1UqQPV5ecUs2sm46vDbGJUI+sE=", "session-token=AIo5Vf+c/GhoTRWq4V; JSESSIONID=58B7C7A24731R869B75D142E970CEAD4; csm-hit=D5P2DBNF895ZDJTCTEQ7+s-D5P2DBNF895ZDJTCTEQ7|1483297885458; session-id-time=2082754801l"},
}
b.Run("ServeHTTP_Serial", func(b *testing.B) {
b.SetBytes(fileSize)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rec := httptest.NewRecorder() // 3 allocs
code, err := stack.ServeHTTP(rec, req)
if err != nil {
b.Fatalf("%+v", err)
}
if code != http.StatusOK {
b.Fatalf("Code must be StatusOK but got %d", code)
}
}
})
b.Run("ServeHTTP_Parallel", func(b *testing.B) {
b.SetBytes(fileSize)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rec := httptest.NewRecorder() // 3 allocs
code, err := stack.ServeHTTP(rec, req)
if err != nil {
b.Fatalf("%+v", err)
}
if code != http.StatusOK {
b.Fatalf("Code must be StatusOK but got %d", code)
}
}
})
})
}