How Fast is Logging in Java?

What’s the cost of trace/debug logging in production? What’s the performance cost of logging to a file? In these benchmarks, I compare the performance impact of logging levels (errorwarninfodebugtrace) and logging appenders (console appender, file appender) on several realistic OptaPlanner use cases.

Benchmark methodology

    • Logging implementation: SFL4J 1.7.2 with Logback 1.0.9 (Logback is the spiritual successor of Log4J 1)
    • Logging configuration (logback.xml):
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
  <appender name="appender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d [%t] %-5p %m%n</pattern>
    </encoder>
  </appender>
 
  <logger name="org.optaplanner" level="info"/>
 
  <root level="warn">
    <appender-ref ref="appender"/>
  </root>
 
</configuration>
Which results in output like this:
2015-02-23 08:07:35,310 [main] INFO Solving started: time spent (18), best score (uninitialized/0hard/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
2015-02-23 08:07:35,363 [main] INFO Construction Heuristic phase (0) ended: step total (6), time spent (71), best score (0hard/-5460soft).
2015-02-23 08:07:35,641 [main] INFO Local Search phase (1) ended: step total (1), time spent (349), best score (0hard/-5460soft).
2015-02-23 08:07:35,652 [main] INFO Solving ended: time spent (360), best score (0hard/-5460soft), average calculate count per second (905), environment mode (REPRODUCIBLE).
  • VM arguments: -Xmx1536M -server
    Software: Linux 3.2.0-59-generic-pae
    Hardware: Intel® Xeon® CPU W3550 @ 3.07GHz
  • Each run solves 13 planning problems with OptaPlanner. Each planning problem runs for 5 minutes. It starts with a 30 second JVM warm up which is discarded.
  • Solving a planning problem involves no IO (except a few milliseconds during startup to load the input). A single CPU is completely saturated. It constantly creates many short lived objects, and the GC collects them afterwards.
  • The benchmarks measure the number of scores that can be calculated per millisecond. Higher is better. Calculating a score for a proposed planning solution is non-trivial: it involves many calculations, including checking for conflicts between every entity and every other entity.
To reproduce these benchmarks locally, build optaplanner from source and run the main classGeneralOptaPlannerBenchmarkApp.

Logging levels: errorwarninfodebug and trace

I ran the same benchmark set several times with a different OptaPlanner logging level:
?
1
<logger name="org.optaplanner" level="error|warn|info|debug|trace"/>
All other libraries (including Drools) were set to warn logging. The logging verbosity of OptaPlanner differs greatly per level:
  • error and warn0 lines
  • info: 4 lines per benchmark, so less than 1 line per minute.
  • debug: 1 line per step, so about 1 per second for Tabu Search and more for Late Acceptance
  • trace: 1 line per move, so between 1k and 120k per second
These are the raw benchmark numbers, measured in average score calculation count per second (higher is better):
Screen Shot 2015-02-24 at 17.43.10
I’ll ignore the difference between error, warn and info logging: The difference is at most 4%, the runs are 100% reproducible and I didn’t otherwise use the computer during the benchmarking, so I presume the difference can be blamed on the JIT hotspot compiling or CPU luck.
What does turning on Debug or Trace logging cost us in performance (versus Info logging)?
loggingLevelSlowerThanInfo
Screen Shot 2015-02-24 at 17.51.39
Trace logging is up to almost 4 times slower! The impact of Debug logging is far less, but still clearly noticeable in many cases. The use cases that use Late Acceptance (Cloud balancing and Course scheduling), which therefore do more debug logging, seem to have a higher performance loss (although that might be in the eye of the beholder).
But wait a second! Those benchmarks use a console appender. What if they use a file appender, like in production?

Logging appenders: appending to the console or a file

On a production server, appending to the console often means losing the log information as it gets piped into/dev/null. Also during development, the IDE’s console buffer can overflow, causing the loss of log lines. One way to avoid these issues is to configure a file appender. To conserve disk space, I used a rolling file appender which compresses old log files in zip files of 5MB:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<appender name="appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>local/log/optaplanner.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
    <fileNamePattern>local/log/optaplanner.%i.log.zip</fileNamePattern>
    <minIndex>1</minIndex>
    <maxIndex>3</maxIndex>
  </rollingPolicy>
  <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    <maxFileSize>5MB</maxFileSize>
  </triggeringPolicy>
  <encoder>
    <pattern>%d [%t] %-5p %m%n</pattern>
  </encoder>
</appender>
These are the raw benchmark numbers, measured again in average score calculation count per second (higher is better):
Screen Shot 2015-02-24 at 17.52.39
fileAppendingSlowerThanConsoleAppendingWhat does file appender cost us in performance (versus console appender)?
Screen Shot 2015-02-24 at 17.53.42
For info logging, it doesn’t really matter. For debug logging, there’s a noticeable slowdown for a minority of the cases.Trace logging is an extra up to almost 4 times slower! And it stacks with our previous observation: In the worst case (Machine reassignment B1), trace logging to a file is 90% slower than info logging to the console.

Conclusion

Like all diagnostic information, logging comes at a performance cost. Good libraries carefully select the logging level of each statement to balance out diagnostic needs, verbosity and performance impact.
Here’s my recommendation for OptaPlanner users: In development, use debug (or trace) logging with a console appender by default, so you can see what’s going on. In production, use warn (or info) logging with a file appender by default, so you retain important information.
Share on Google Plus

About Admin

Arun is a JAVA/J2EE developer and passionate about coding and managing technical team.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment