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.io.File;
21 import java.util.Arrays;
22 import java.util.concurrent.TimeUnit;
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.apache.logging.log4j.core.LifeCycle;
27 import org.openjdk.jmh.annotations.BenchmarkMode;
28 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
29 import org.openjdk.jmh.annotations.Level;
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.annotations.TearDown;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @State(Scope.Thread)
52 public class AsyncLoggersBenchmark {
53 final static char[] CHARS = new char[500];
54 static {
55 Arrays.fill(CHARS, 'a');
56 }
57 final static String TEST = new String(CHARS);
58
59 Logger logger;
60
61 @Setup(Level.Trial)
62 public void up() {
63 new File("perftest.log").delete();
64 System.setProperty("log4j.configurationFile", "perf3PlainNoLoc.xml");
65 System.setProperty("Log4jContextSelector",
66 "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
67 logger = LogManager.getLogger(getClass());
68 }
69
70 @TearDown(Level.Trial)
71 public void down() {
72 ((LifeCycle) LogManager.getContext(false)).stop();
73 new File("perftest.log").delete();
74 }
75
76 @GenerateMicroBenchmark
77 @BenchmarkMode(Mode.Throughput)
78 @OutputTimeUnit(TimeUnit.SECONDS)
79 public boolean throughputBaseline() {
80 return logger.isInfoEnabled();
81 }
82
83 @GenerateMicroBenchmark
84 @BenchmarkMode(Mode.Throughput)
85 @OutputTimeUnit(TimeUnit.SECONDS)
86 public void throughput() {
87 logger.info(TEST);
88 }
89
90 @GenerateMicroBenchmark
91 @BenchmarkMode(Mode.SampleTime)
92 @OutputTimeUnit(TimeUnit.NANOSECONDS)
93 public boolean latencyBaseline() {
94 return logger.isInfoEnabled();
95 }
96
97 @GenerateMicroBenchmark
98 @BenchmarkMode(Mode.SampleTime)
99 @OutputTimeUnit(TimeUnit.NANOSECONDS)
100 public void latency() {
101 logger.info(TEST);
102 }
103 }