| 175 | } |
| 176 | |
| 177 | func BenchmarkLog(b *testing.B) { |
| 178 | start := time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC) |
| 179 | e := &Event{ |
| 180 | Start: start, |
| 181 | End: start.Add(100 * time.Millisecond), |
| 182 | Request: &http.Request{ |
| 183 | RequestURI: "/?q=x", |
| 184 | Header: http.Header{ |
| 185 | "User-Agent": {"Mozilla Firefox"}, |
| 186 | "Referer": {"http://foo.com/"}, |
| 187 | "X-Forwarded-For": {"3.3.3.3"}, |
| 188 | }, |
| 189 | RemoteAddr: "2.2.2.2:666", |
| 190 | Host: "foo.com", |
| 191 | URL: &url.URL{ |
| 192 | Path: "/", |
| 193 | RawQuery: "?q=x", |
| 194 | Host: "proxy host", |
| 195 | }, |
| 196 | Method: "GET", |
| 197 | Proto: "HTTP/1.1", |
| 198 | }, |
| 199 | Response: &http.Response{ |
| 200 | StatusCode: 200, |
| 201 | ContentLength: 1234, |
| 202 | Header: http.Header{"foo": []string{"bar"}}, |
| 203 | Request: &http.Request{ |
| 204 | RemoteAddr: "5.6.7.8:1234", |
| 205 | }, |
| 206 | }, |
| 207 | UpstreamAddr: mustParse("http://7.8.9.0:5678/foo").Host, |
| 208 | } |
| 209 | |
| 210 | // benchmark the custom parser and text/template |
| 211 | // to explain why there is a custom approach. |
| 212 | // The custom parser is 8x faster and has zero allocs. |
| 213 | // |
| 214 | // BenchmarkLog/my_parser-8 1000000 2326 ns/op 0 B/op 0 allocs/op |
| 215 | // BenchmarkLog/go_text/template-8 100000 19026 ns/op 848 B/op 76 allocs/op |
| 216 | b.Run("custom parser", func(b *testing.B) { |
| 217 | var keys []string |
| 218 | for k := range fields { |
| 219 | keys = append(keys, k) |
| 220 | } |
| 221 | sort.Strings(keys) |
| 222 | format := strings.Join(keys, " ") |
| 223 | |
| 224 | l, err := New(io.Discard, format) |
| 225 | if err != nil { |
| 226 | b.Fatal(err) |
| 227 | } |
| 228 | |
| 229 | b.ResetTimer() |
| 230 | for range b.N { |
| 231 | l.Log(e) |
| 232 | } |
| 233 | }) |
| 234 | b.Run("text/template", func(b *testing.B) { |