Logging Performance Comparison
blog.sebastian-daschner.com
blog.sebastian-daschner.com
You might think “that’s ok, it still shows me the overhead of logging.” This is subtly wrong, though, because you can’t necessarily talk about the overhead of logging without saying how much logging, from how many threads, with how much other work.
Take the simplest case, stdout. Writing to stdout requires a lock. If logging is sufficiently rare, most of the time, there’s no wait. As logging increases in frequency, there is more contention. Doubling the number of writes more than doubles the time spent logging, because of that contention.
To benchmark this properly, you’d[0] want to have a test where the total number of requests/second without logging and the number of logs/request as independent variables. That would show you the regions where logging becomes a bottleneck, and regions where it’s not.
[0] At a minimum. I’m not saying this would be an ideal benchmark—if you’re not doing marketing, benchmarks aren’t about “let’s find the biggest number”, but accurately modeling a phenomenon so that you can make predictions. That means you might need to do a lot of experimentation.
for (; counter < 1_000; counter++) {
// ... I know this doesn't make much sense :)
}
I think the JVM would optimise away this loop because it doesn't do anything. e.g. if the loop optimisation method is to unroll the loop into 1000 inline statements, there's nothing there.This makes "No Logging" result not quite a fair comparison; maybe something like the sum of 1000 random numbers do some actual work in the loop.
I have no idea how java handle this.. Does the AOT compiler do any optimization? Would this defer to the JIT?
The one thing from modern Java logging I love and miss in other languages is being able to set log levels on specific loggers while the app is running.
Yeah it involves JMX, which has an annoying protocol that insists on domain names that you can resolve, making it a PITA with K8s from your local terminal, but it's such a QoL feature.
Should work well with K8s config map mounted as volume.
Or even for successful requests per second averaged over spans of time including normal outages, degradations, and other indignities of life. It doesn't take many minutes at zero throughput to drag those averages down, so every minute shaved off by more illuminating logging is a performance benefit.
The tests he created effectively made logging dominate the request active time so it's rather deceptive in that manner.
His non-logging case was probably entirely optimized away by Hotspot unless he is running these benchmarks under JMH and properly disabled optimizations for that loop.
> The downside of this approach is that if the system crashes without a chance to flush the buffer, the last lines might not have been written and may be lost.