Comments (7)
@kisielk I think it's possible to avoid the use of fmt
altogether. By using io.WriteString
and strconv.Itoa
. Since the format verbs are either %s
or %d
. The buildCommonLogLine
could then take a writer and write to it directly, instead of returning a string.
Although, if using fmt
once per request is really okay, then yeah.. It is possible by moving a few things around may be.
from handlers.
I can make this change and send you a pull request if that's okay..
from handlers.
Go for it
On Tuesday, May 27, 2014, Mahmud Ridwan [email protected] wrote:
I can make this change and send you a pull request if that's okay..
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-44257743
.
-K
from handlers.
However, please try benchmarking it to make sure the modified version is actually faster and does fewer allocations than the fmt version.
from handlers.
I wrote a simple benchmark func
func BenchmarkWriteLog(b *testing.B) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
b.Fatalf(err.Error())
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
req := newRequest("GET", "http://example.com")
req.RemoteAddr = "192.168.100.5"
b.ResetTimer()
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
writeLog(buf, req, ts, http.StatusUnauthorized, 500)
}
}
For current implementation, I get this:
BenchmarkWriteLog 500000 3351 ns/op
For the modified implementation (with no fmt
at all), I get this:
BenchmarkWriteLog 1000000 1394 ns/op
from handlers.
That certainly looks promising. How about memory usage? There's a benchmark
flag to measure allocations as well. I expect it will be lower too.
On Wednesday, May 28, 2014, Mahmud Ridwan [email protected] wrote:
I wrote a simple benchmark function
func BenchmarkWriteLog(b *testing.B) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
b.Fatalf(err.Error())
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)req := newRequest("GET", "http://example.com") req.RemoteAddr = "192.168.100.5" buf := &bytes.Buffer{} for i := 0; i < b.N; i++ { buf.Reset() writeLog(buf, req, ts, http.StatusUnauthorized, 500) }}
For current implementation, I get this:
BenchmarkWriteLog 500000 3351 ns/op
For the modified implementation (with no fmt at all), I get this:
BenchmarkWriteLog 1000000 1394 ns/op
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-44383295
.
-K
from handlers.
Ran $ go test -bench . -benchmem
For current implementation:
BenchmarkWriteLog 500000 3287 ns/op 338 B/op 11 allocs/op
For the modified implementation (#13; with no fmt
at all):
BenchmarkWriteLog 1000000 1412 ns/op 162 B/op 5 allocs/op
from handlers.
Related Issues (20)
- [Feature] Json logging handler HOT 2
- [feature] FileServer handler HOT 2
- [feature] add brotli encoding support in compress handler HOT 3
- [bug] ProxyHeaders breaks gorilla/csrf HOT 1
- [bug] websockets are logged with a status code of 200 instead of 101 HOT 1
- [bug] ProxyHeaders XFF parsing handles whitespace incorrectly
- [question] How to add X-Request-ID in access Log
- [question] Does handler add AllowedOrigins * header by default?
- [feature] Basic Auth handler
- [feature] improve the CPUUtilization and MemoryUtilization
- Incorrect regex in `forRegex`
- [question] upgrade httpsnoop HOT 1
- OPTIONS in Allow for MethodHandler
- The ProxyHeaders middleware is misleading and dangerous
- [BUG] CORS middleware sets `Vary: Origin` header only in some cases HOT 1
- [BUG] Listing Origin as an allowed request header is unnecessary
- [FEATURE] Allow users to disable caching of preflight responses
- [FEATURE] Support Private Network Access
- [BUG] Sometimes missing Vary: Origin header may lead to Web cache poisoning
- [BUG] Allowing methods that are not uppercase should be possible but isn't
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handlers.