Skip to content

caddyhttp: Optimize logs using zap's WithLazy()#6590

Merged
mholt merged 5 commits into
caddyserver:masterfrom
AlliBalliBaba:improved-http-performance
Sep 26, 2024
Merged

caddyhttp: Optimize logs using zap's WithLazy()#6590
mholt merged 5 commits into
caddyserver:masterfrom
AlliBalliBaba:improved-http-performance

Conversation

@AlliBalliBaba

Copy link
Copy Markdown
Contributor

After #6541 I realized that using zap's WithLazy() for the error logger was still much more efficient even if we are cloning the request. Originally I wanted to try using this originalRequest, which is made for logging but just gives a shallow copy without request headers. Instead, using request.Clone() gives a deep copy that just omits the request body.

This makes a simple request about 7% more efficient:

:80 {
    respond "Hello World"
}

wrk -t 4 -c 200 -d 60 http://localhost (20 cores Docker Ubuntu WSL)
With() ~205.000 RPS
WithLazy() ~219.000 RPS

Flamegraph With()
torch-caddy

Flamegraph WithLazy() and Copy()
torch-caddy2

Note that this will make the case where an error is logged about 1% slower since we are doing an additional request.Clone()

@AlliBalliBaba AlliBalliBaba force-pushed the improved-http-performance branch from a3e02df to d380731 Compare September 24, 2024 19:32
@francislavoie

Copy link
Copy Markdown
Member

We could possible do the same in reverseproxy.go and fastcgi.go?

@mholt mholt added the optimization 📉 Performance or cost improvements label Sep 24, 2024
@mholt

mholt commented Sep 24, 2024

Copy link
Copy Markdown
Member

That's very interesting -- thanks for submitting this PR!

I second Francis' question, but I think this LGTM either way.

@AlliBalliBaba

Copy link
Copy Markdown
Contributor Author

I can look into reverseproxy.go and fastcgi.go if there's similar potential for improvement. Probably something for another PR

@mholt

mholt commented Sep 25, 2024

Copy link
Copy Markdown
Member

Okay, doing it in increments sounds reasonable.

I'll merge this, but before I do, why does cloning the request make it faster? That seems counterintuitive. (I wonder if we should comment next to that line, either a link to this PR or a brief explanation)

@AlliBalliBaba

Copy link
Copy Markdown
Contributor Author

Cloning the request is faster than json encoding the request and cloning the logger. But yeah it seems counterintuitive, I'll add a comment.

@francislavoie francislavoie changed the title Performance: Uses zap's WithLazy() for the error logger caddyhttp: Optimize logs using zap's WithLazy() Sep 26, 2024

@mholt mholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looking forward to seeing if this works well in any other places.

@mholt mholt merged commit 22c98ea into caddyserver:master Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization 📉 Performance or cost improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants