Commit beae16f0 authored by Tw's avatar Tw Committed by Matt Holt

Proxy performance (#946)

* proxy: add benchmark
Signed-off-by: default avatarTw <tw19881113@gmail.com>

* replacer: prepare lazily

update issue#939

benchmark            old ns/op     new ns/op     delta
BenchmarkProxy-4     83865         72544         -13.50%
Signed-off-by: default avatarTw <tw19881113@gmail.com>

* proxy: use buffer pool to avoid temporary allocation
Signed-off-by: default avatarTw <tw19881113@gmail.com>
parent 12406909
...@@ -37,8 +37,8 @@ type Replacer interface { ...@@ -37,8 +37,8 @@ type Replacer interface {
// they will be used to overwrite other replacements // they will be used to overwrite other replacements
// if there is a name conflict. // if there is a name conflict.
type replacer struct { type replacer struct {
replacements map[string]string replacements map[string]func() string
customReplacements map[string]string customReplacements map[string]func() string
emptyValue string emptyValue string
responseRecorder *ResponseRecorder responseRecorder *ResponseRecorder
} }
...@@ -53,36 +53,36 @@ type replacer struct { ...@@ -53,36 +53,36 @@ type replacer struct {
func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer { func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer {
rep := &replacer{ rep := &replacer{
responseRecorder: rr, responseRecorder: rr,
customReplacements: make(map[string]string), customReplacements: make(map[string]func() string),
replacements: map[string]string{ replacements: map[string]func() string{
"{method}": r.Method, "{method}": func() string { return r.Method },
"{scheme}": func() string { "{scheme}": func() string {
if r.TLS != nil { if r.TLS != nil {
return "https" return "https"
} }
return "http" return "http"
}(), },
"{hostname}": func() string { "{hostname}": func() string {
name, err := os.Hostname() name, err := os.Hostname()
if err != nil { if err != nil {
return "" return ""
} }
return name return name
}(), },
"{host}": r.Host, "{host}": func() string { return r.Host },
"{hostonly}": func() string { "{hostonly}": func() string {
host, _, err := net.SplitHostPort(r.Host) host, _, err := net.SplitHostPort(r.Host)
if err != nil { if err != nil {
return r.Host return r.Host
} }
return host return host
}(), },
"{path}": r.URL.Path, "{path}": func() string { return r.URL.Path },
"{path_escaped}": url.QueryEscape(r.URL.Path), "{path_escaped}": func() string { return url.QueryEscape(r.URL.Path) },
"{query}": r.URL.RawQuery, "{query}": func() string { return r.URL.RawQuery },
"{query_escaped}": url.QueryEscape(r.URL.RawQuery), "{query_escaped}": func() string { return url.QueryEscape(r.URL.RawQuery) },
"{fragment}": r.URL.Fragment, "{fragment}": func() string { return r.URL.Fragment },
"{proto}": r.Proto, "{proto}": func() string { return r.Proto },
"{remote}": func() string { "{remote}": func() string {
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" { if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
return fwdFor return fwdFor
...@@ -92,25 +92,25 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla ...@@ -92,25 +92,25 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
return r.RemoteAddr return r.RemoteAddr
} }
return host return host
}(), },
"{port}": func() string { "{port}": func() string {
_, port, err := net.SplitHostPort(r.RemoteAddr) _, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil { if err != nil {
return "" return ""
} }
return port return port
}(), },
"{uri}": r.URL.RequestURI(), "{uri}": func() string { return r.URL.RequestURI() },
"{uri_escaped}": url.QueryEscape(r.URL.RequestURI()), "{uri_escaped}": func() string { return url.QueryEscape(r.URL.RequestURI()) },
"{when}": time.Now().Format(timeFormat), "{when}": func() string { return time.Now().Format(timeFormat) },
"{file}": func() string { "{file}": func() string {
_, file := path.Split(r.URL.Path) _, file := path.Split(r.URL.Path)
return file return file
}(), },
"{dir}": func() string { "{dir}": func() string {
dir, _ := path.Split(r.URL.Path) dir, _ := path.Split(r.URL.Path)
return dir return dir
}(), },
"{request}": func() string { "{request}": func() string {
dump, err := httputil.DumpRequest(r, false) dump, err := httputil.DumpRequest(r, false)
if err != nil { if err != nil {
...@@ -118,14 +118,15 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla ...@@ -118,14 +118,15 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
} }
return requestReplacer.Replace(string(dump)) return requestReplacer.Replace(string(dump))
}(), },
}, },
emptyValue: emptyValue, emptyValue: emptyValue,
} }
// Header placeholders (case-insensitive) // Header placeholders (case-insensitive)
for header, values := range r.Header { for header, values := range r.Header {
rep.replacements[headerReplacer+strings.ToLower(header)+"}"] = strings.Join(values, ",") values := values
rep.replacements[headerReplacer+strings.ToLower(header)+"}"] = func() string { return strings.Join(values, ",") }
} }
return rep return rep
...@@ -141,9 +142,9 @@ func (r *replacer) Replace(s string) string { ...@@ -141,9 +142,9 @@ func (r *replacer) Replace(s string) string {
// Make response placeholders now // Make response placeholders now
if r.responseRecorder != nil { if r.responseRecorder != nil {
r.replacements["{status}"] = strconv.Itoa(r.responseRecorder.status) r.replacements["{status}"] = func() string { return strconv.Itoa(r.responseRecorder.status) }
r.replacements["{size}"] = strconv.Itoa(r.responseRecorder.size) r.replacements["{size}"] = func() string { return strconv.Itoa(r.responseRecorder.size) }
r.replacements["{latency}"] = time.Since(r.responseRecorder.start).String() r.replacements["{latency}"] = func() string { return time.Since(r.responseRecorder.start).String() }
} }
// Include custom placeholders, overwriting existing ones if necessary // Include custom placeholders, overwriting existing ones if necessary
...@@ -158,7 +159,10 @@ func (r *replacer) Replace(s string) string { ...@@ -158,7 +159,10 @@ func (r *replacer) Replace(s string) string {
idxEnd := strings.Index(s[endOffset:], "}") idxEnd := strings.Index(s[endOffset:], "}")
if idxEnd > -1 { if idxEnd > -1 {
placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1]) placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1])
replacement := r.replacements[placeholder] replacement := ""
if getReplacement, ok := r.replacements[placeholder]; ok {
replacement = getReplacement()
}
if replacement == "" { if replacement == "" {
replacement = r.emptyValue replacement = r.emptyValue
} }
...@@ -169,7 +173,11 @@ func (r *replacer) Replace(s string) string { ...@@ -169,7 +173,11 @@ func (r *replacer) Replace(s string) string {
} }
// Regular replacements - these are easier because they're case-sensitive // Regular replacements - these are easier because they're case-sensitive
for placeholder, replacement := range r.replacements { for placeholder, getReplacement := range r.replacements {
if !strings.Contains(s, placeholder) {
continue
}
replacement := getReplacement()
if replacement == "" { if replacement == "" {
replacement = r.emptyValue replacement = r.emptyValue
} }
...@@ -181,7 +189,7 @@ func (r *replacer) Replace(s string) string { ...@@ -181,7 +189,7 @@ func (r *replacer) Replace(s string) string {
// Set sets key to value in the r.customReplacements map. // Set sets key to value in the r.customReplacements map.
func (r *replacer) Set(key, value string) { func (r *replacer) Set(key, value string) {
r.customReplacements["{"+key+"}"] = value r.customReplacements["{"+key+"}"] = func() string { return value }
} }
const ( const (
......
...@@ -21,19 +21,26 @@ func TestNewReplacer(t *testing.T) { ...@@ -21,19 +21,26 @@ func TestNewReplacer(t *testing.T) {
switch v := rep.(type) { switch v := rep.(type) {
case *replacer: case *replacer:
if v.replacements["{host}"] != "localhost" { if v.replacements["{host}"]() != "localhost" {
t.Error("Expected host to be localhost") t.Error("Expected host to be localhost")
} }
if v.replacements["{method}"] != "POST" { if v.replacements["{method}"]() != "POST" {
t.Error("Expected request method to be POST") t.Error("Expected request method to be POST")
} }
// Response placeholders should only be set after call to Replace() // Response placeholders should only be set after call to Replace()
if got, want := v.replacements["{status}"], ""; got != want { got, want := "", ""
if getReplacement, ok := v.replacements["{status}"]; ok {
got = getReplacement()
}
if want := ""; got != want {
t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got) t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got)
} }
rep.Replace("{foobar}") rep.Replace("{foobar}")
if got, want := v.replacements["{status}"], "200"; got != want { if getReplacement, ok := v.replacements["{status}"]; ok {
got = getReplacement()
}
if want = "200"; got != want {
t.Errorf("Expected status to be %s, was: %s", want, got) t.Errorf("Expected status to be %s, was: %s", want, got)
} }
default: default:
...@@ -84,10 +91,14 @@ func TestReplace(t *testing.T) { ...@@ -84,10 +91,14 @@ func TestReplace(t *testing.T) {
complexCases := []struct { complexCases := []struct {
template string template string
replacements map[string]string replacements map[string]func() string
expect string expect string
}{ }{
{"/a{1}/{2}", map[string]string{"{1}": "12", "{2}": ""}, "/a12/"}, {"/a{1}/{2}",
map[string]func() string{
"{1}": func() string { return "12" },
"{2}": func() string { return "" }},
"/a12/"},
} }
for _, c := range complexCases { for _, c := range complexCases {
......
...@@ -761,3 +761,37 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil } ...@@ -761,3 +761,37 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil }
func (c *fakeConn) Close() error { return nil } func (c *fakeConn) Close() error { return nil }
func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) } func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) }
func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) } func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) }
func BenchmarkProxy(b *testing.B) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, client"))
}))
defer backend.Close()
upstream := newFakeUpstream(backend.URL, false)
upstream.host.UpstreamHeaders = http.Header{
"Hostname": {"{hostname}"},
"Host": {"{host}"},
"X-Real-IP": {"{remote}"},
"X-Forwarded-Proto": {"{scheme}"},
}
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
Upstreams: []Upstream{upstream},
}
w := httptest.NewRecorder()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
// create request and response recorder
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
b.Fatalf("Failed to create request: %v", err)
}
b.StartTimer()
p.ServeHTTP(w, r)
}
}
...@@ -22,6 +22,12 @@ import ( ...@@ -22,6 +22,12 @@ import (
"time" "time"
) )
var bufferPool = sync.Pool{New: createBuffer}
func createBuffer() interface{} {
return make([]byte, 32*1024)
}
// onExitFlushLoop is a callback set by tests to detect the state of the // onExitFlushLoop is a callback set by tests to detect the state of the
// flushLoop() goroutine. // flushLoop() goroutine.
var onExitFlushLoop func() var onExitFlushLoop func()
...@@ -214,6 +220,9 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, r ...@@ -214,6 +220,9 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, r
} }
func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
buf := bufferPool.Get()
defer bufferPool.Put(buf)
if p.FlushInterval != 0 { if p.FlushInterval != 0 {
if wf, ok := dst.(writeFlusher); ok { if wf, ok := dst.(writeFlusher); ok {
mlw := &maxLatencyWriter{ mlw := &maxLatencyWriter{
...@@ -226,7 +235,7 @@ func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { ...@@ -226,7 +235,7 @@ func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
dst = mlw dst = mlw
} }
} }
io.Copy(dst, src) io.CopyBuffer(dst, src, buf.([]byte))
} }
type writeFlusher interface { type writeFlusher interface {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment