1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.perf.jmh;
18
19 import java.io.File;
20 import java.util.Arrays;
21 import java.util.concurrent.TimeUnit;
22
23 import org.openjdk.jmh.annotations.BenchmarkMode;
24 import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
25 import org.openjdk.jmh.annotations.Level;
26 import org.openjdk.jmh.annotations.Mode;
27 import org.openjdk.jmh.annotations.OutputTimeUnit;
28 import org.openjdk.jmh.annotations.Scope;
29 import org.openjdk.jmh.annotations.Setup;
30 import org.openjdk.jmh.annotations.State;
31 import org.openjdk.jmh.annotations.TearDown;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import ch.qos.logback.core.spi.LifeCycle;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class LogbackAsyncAppenderBenchmark {
52
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 @State(Scope.Benchmark)
60 public static class NormalState {
61 Logger logger;
62
63 @Setup(Level.Trial)
64 public void up() {
65 System.setProperty("logback.configurationFile", "perf-logback-async.xml");
66 logger = (Logger) LoggerFactory.getLogger(getClass());
67 }
68
69 @TearDown(Level.Trial)
70 public void down() {
71 ((LifeCycle) LoggerFactory.getILoggerFactory()).stop();
72 new File("perftest.log").delete();
73 }
74 }
75
76 @GenerateMicroBenchmark
77 @BenchmarkMode(Mode.Throughput)
78 @OutputTimeUnit(TimeUnit.SECONDS)
79 public boolean throughputBaseline(final NormalState e) {
80 return e.logger.isInfoEnabled();
81 }
82
83 @GenerateMicroBenchmark
84 @BenchmarkMode(Mode.Throughput)
85 @OutputTimeUnit(TimeUnit.SECONDS)
86 public void throughput(final NormalState e) {
87 e.logger.info(TEST);
88 }
89
90 @GenerateMicroBenchmark
91 @BenchmarkMode(Mode.SampleTime)
92 @OutputTimeUnit(TimeUnit.NANOSECONDS)
93 public boolean latencyBaseline(final NormalState e) {
94 return e.logger.isInfoEnabled();
95 }
96
97 @GenerateMicroBenchmark
98 @BenchmarkMode(Mode.SampleTime)
99 @OutputTimeUnit(TimeUnit.NANOSECONDS)
100 public void latency(final NormalState e) {
101 e.logger.info(TEST);
102 }
103 }