1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.perf.jmh;
19
20 import java.util.concurrent.TimeUnit;
21
22 import org.apache.logging.log4j.Level;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.apache.logging.log4j.core.config.Configuration;
27 import org.apache.logging.log4j.core.config.DefaultConfiguration;
28 import org.openjdk.jmh.annotations.BenchmarkMode;
29 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.OutputTimeUnit;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
35 import org.openjdk.jmh.logic.BlackHole;
36
37
38
39
40
41
42
43
44
45
46
47 @State(Scope.Thread)
48 public class SimpleBenchmark {
49 private static final String msg = "This is a test";
50 private Logger logger;
51
52 @Setup
53 public void setup() {
54 final Configuration config = ((LoggerContext) LogManager.getContext()).getConfiguration();
55 if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
56 System.out.println("Configuration was " + config.getName());
57 ((LoggerContext) LogManager.getContext()).start(new DefaultConfiguration());
58 }
59 logger = LogManager.getLogger(SimpleBenchmark.class.getName());
60 }
61
62 @BenchmarkMode(Mode.Throughput)
63 @OutputTimeUnit(TimeUnit.SECONDS)
64 @GenerateMicroBenchmark
65 public void testBaselineThroughput(final BlackHole bh) {
66 }
67
68 @BenchmarkMode(Mode.Throughput)
69 @OutputTimeUnit(TimeUnit.SECONDS)
70 @GenerateMicroBenchmark
71 public void testIsDebugEnabledThroughput(final BlackHole bh) {
72 bh.consume(logger.isDebugEnabled());
73 }
74
75 @BenchmarkMode(Mode.Throughput)
76 @OutputTimeUnit(TimeUnit.SECONDS)
77 @GenerateMicroBenchmark
78 public void testIsEnabledLevelThroughput(final BlackHole bh) {
79 bh.consume(logger.isEnabled(Level.DEBUG));
80 }
81
82 @BenchmarkMode(Mode.Throughput)
83 @OutputTimeUnit(TimeUnit.SECONDS)
84 @GenerateMicroBenchmark
85 public void testDebugDisabledThroughput(final BlackHole bh) {
86 logger.debug(msg);
87 }
88
89
90 @BenchmarkMode(Mode.SampleTime)
91 @OutputTimeUnit(TimeUnit.NANOSECONDS)
92 @GenerateMicroBenchmark
93 public void testBaselineResponseTime(final BlackHole bh) {
94 }
95
96 @BenchmarkMode(Mode.SampleTime)
97 @OutputTimeUnit(TimeUnit.NANOSECONDS)
98 @GenerateMicroBenchmark
99 public void testIsDebugEnabledResponseTime(final BlackHole bh) {
100 bh.consume(logger.isDebugEnabled());
101 }
102
103 @BenchmarkMode(Mode.SampleTime)
104 @OutputTimeUnit(TimeUnit.NANOSECONDS)
105 @GenerateMicroBenchmark
106 public void testIsEnabledLevelResponseTime(final BlackHole bh) {
107 bh.consume(logger.isEnabled(Level.DEBUG));
108 }
109
110 @BenchmarkMode(Mode.SampleTime)
111 @OutputTimeUnit(TimeUnit.NANOSECONDS)
112 @GenerateMicroBenchmark
113 public void testDebugDisabledResponseTime(final BlackHole bh) {
114 logger.debug(msg);
115 }
116 }