1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.issue.lbcore243;
15
16 import ch.qos.logback.classic.LoggerContext;
17 import ch.qos.logback.classic.joran.JoranConfigurator;
18 import ch.qos.logback.core.joran.spi.JoranException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22
23
24
25
26 public class PerformanceComparatorLogback {
27 static Logger logbacklogger = LoggerFactory.getLogger(PerformanceComparatorLogback.class);
28
29 public static void main(String[] args) throws JoranException, InterruptedException {
30 initLogbackWithoutImmediateFlush();
31 logbackParametrizedDebugCall();
32
33 initLogbackWithImmediateFlush();
34 logbackParametrizedDebugCall();
35 System.out.println("###############################################");
36 System.out.println("Logback with immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
37
38 initLogbackWithoutImmediateFlush();
39 System.out.println("Logback without immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
40
41 System.out.println("###############################################");
42 }
43
44 private static long logbackParametrizedDebugCall() {
45
46 Integer j = Integer.valueOf(2);
47 long start = System.nanoTime();
48 for (int i = 0; i < Common.loop; i++) {
49 logbacklogger.debug("SEE IF THIS IS LOGGED {}.", j);
50 }
51 return (System.nanoTime() - start) / Common.loop;
52 }
53
54 static String DIR_PREFIX = "src/test/java/ch/qos/logback/classic/issue/lbcore243/";
55
56 static void configure(String file) throws JoranException {
57 LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
58 JoranConfigurator jc = new JoranConfigurator();
59 jc.setContext(loggerContext);
60 loggerContext.reset();
61 jc.doConfigure(file);
62 }
63
64 private static void initLogbackWithoutImmediateFlush() throws JoranException {
65 configure(DIR_PREFIX + "logback_without_immediateFlush.xml");
66 }
67
68 private static void initLogbackWithImmediateFlush() throws JoranException {
69 configure(DIR_PREFIX + "logback_with_immediateFlush.xml");
70 }
71 }