Skip to content

Commit 3d3626e

Browse files
committed
initial commit
1 parent 57a245d commit 3d3626e

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

jmh-benchmarks/src/test/java/com/openelements/logger/benchmarks/BenchmarkConstants.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
public class BenchmarkConstants {
44

5-
public final static int WARMUP_ITERATIONS = 20;
5+
public final static int WARMUP_ITERATIONS = 10;
66

77
public final static int WARMUP_TIME_IN_SECONDS_PER_ITERATION = 4;
88

9-
public final static int MEASUREMENT_ITERATIONS = 20;
9+
public final static int MEASUREMENT_ITERATIONS = 10;
1010

1111
public final static int MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION = 4;
12+
13+
public final static int PARALLEL_THREAD_COUNT = 4;
14+
1215
}

jmh-benchmarks/src/test/java/com/openelements/logger/benchmarks/ChronicleLoggerBenchmark.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_ITERATIONS;
44
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION;
5+
import static com.openelements.logger.benchmarks.BenchmarkConstants.PARALLEL_THREAD_COUNT;
56
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_ITERATIONS;
67
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_TIME_IN_SECONDS_PER_ITERATION;
78

@@ -20,12 +21,13 @@
2021
import org.openjdk.jmh.annotations.Scope;
2122
import org.openjdk.jmh.annotations.Setup;
2223
import org.openjdk.jmh.annotations.State;
24+
import org.openjdk.jmh.annotations.Threads;
2325
import org.openjdk.jmh.annotations.Warmup;
2426

2527
@State(Scope.Benchmark)
2628
public class ChronicleLoggerBenchmark {
2729

28-
@Param({"FILE"})
30+
@Param({"FILE_ASYNC"})
2931
public String loggingType;
3032

3133
@Setup(Level.Iteration)
@@ -40,6 +42,7 @@ public void init() throws Exception {
4042

4143
@Benchmark
4244
@Fork(1)
45+
@Threads(PARALLEL_THREAD_COUNT)
4346
@BenchmarkMode(Mode.Throughput)
4447
@Warmup(iterations = WARMUP_ITERATIONS, time = WARMUP_TIME_IN_SECONDS_PER_ITERATION)
4548
@Measurement(iterations = MEASUREMENT_ITERATIONS, time = MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION)

jmh-benchmarks/src/test/java/com/openelements/logger/benchmarks/JulLoggerBenchmark.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_ITERATIONS;
44
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION;
5+
import static com.openelements.logger.benchmarks.BenchmarkConstants.PARALLEL_THREAD_COUNT;
56
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_ITERATIONS;
67
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_TIME_IN_SECONDS_PER_ITERATION;
78

@@ -23,6 +24,7 @@
2324
import org.openjdk.jmh.annotations.Scope;
2425
import org.openjdk.jmh.annotations.Setup;
2526
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.annotations.Threads;
2628
import org.openjdk.jmh.annotations.Warmup;
2729

2830
@State(Scope.Benchmark)
@@ -45,6 +47,7 @@ public void init() throws Exception {
4547

4648
@Benchmark
4749
@Fork(1)
50+
@Threads(PARALLEL_THREAD_COUNT)
4851
@BenchmarkMode(Mode.Throughput)
4952
@Warmup(iterations = WARMUP_ITERATIONS, time = WARMUP_TIME_IN_SECONDS_PER_ITERATION)
5053
@Measurement(iterations = MEASUREMENT_ITERATIONS, time = MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION)

jmh-benchmarks/src/test/java/com/openelements/logger/benchmarks/Log4JLoggerBenchmark.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_ITERATIONS;
44
import static com.openelements.logger.benchmarks.BenchmarkConstants.MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION;
5+
import static com.openelements.logger.benchmarks.BenchmarkConstants.PARALLEL_THREAD_COUNT;
56
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_ITERATIONS;
67
import static com.openelements.logger.benchmarks.BenchmarkConstants.WARMUP_TIME_IN_SECONDS_PER_ITERATION;
78

@@ -27,6 +28,7 @@
2728
import org.openjdk.jmh.annotations.Scope;
2829
import org.openjdk.jmh.annotations.Setup;
2930
import org.openjdk.jmh.annotations.State;
31+
import org.openjdk.jmh.annotations.Threads;
3032
import org.openjdk.jmh.annotations.Warmup;
3133

3234
@State(Scope.Benchmark)
@@ -35,23 +37,30 @@ public class Log4JLoggerBenchmark {
3537

3638
private final static String PATTERN = "%d %c [%t] %-5level: %msg [%marker] %X %n%throwable";
3739

38-
@Param({"FILE", "CONSOLE", "FILE_AND_CONSOLE"})
40+
@Param({"FILE", "CONSOLE", "FILE_AND_CONSOLE", "FILE_ASYNC", "FILE_ASYNC_AND_CONSOLE"})
3941
public String loggingType;
4042

4143
@Setup(org.openjdk.jmh.annotations.Level.Iteration)
4244
public void init() throws Exception {
4345
Files.deleteIfExists(Path.of("target/log4j-benchmark.log"));
46+
Files.deleteIfExists(Path.of("log4j-async-benchmark.log"));
47+
4448
if (Objects.equals(loggingType, "FILE")) {
4549
configureFileLogging();
4650
} else if (Objects.equals(loggingType, "CONSOLE")) {
4751
configureConsoleLogging();
4852
} else if (Objects.equals(loggingType, "FILE_AND_CONSOLE")) {
4953
configureFileAndConsoleLogging();
54+
} else if (Objects.equals(loggingType, "FILE_ASYNC")) {
55+
configureAsyncFileLogging();
56+
} else if (Objects.equals(loggingType, "FILE_ASYNC_AND_CONSOLE")) {
57+
configureAsyncAndConsoleFileLogging();
5058
}
5159
}
5260

5361
@Benchmark
5462
@Fork(1)
63+
@Threads(PARALLEL_THREAD_COUNT)
5564
@BenchmarkMode(Mode.Throughput)
5665
@Warmup(iterations = WARMUP_ITERATIONS, time = WARMUP_TIME_IN_SECONDS_PER_ITERATION)
5766
@Measurement(iterations = MEASUREMENT_ITERATIONS, time = MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION)
@@ -75,50 +84,94 @@ private static AppenderComponentBuilder createFileAppender(final String name,
7584
.addAttribute("pattern", PATTERN);
7685
return builder.newAppender(name, "File")
7786
.addAttribute("fileName", "target/log4j-benchmark.log")
87+
.addAttribute("append", false)
88+
.add(layoutBuilder);
89+
}
90+
91+
private static AppenderComponentBuilder createAsyncFileAppender(final String name,
92+
final ConfigurationBuilder<BuiltConfiguration> builder) {
93+
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
94+
.addAttribute("pattern", PATTERN);
95+
return builder.newAppender("file", "RandomAccessFile")
96+
.addAttribute("fileName", "target/log4j-async-benchmark.log")
97+
.addAttribute("immediateFlush", false)
98+
.addAttribute("append", false)
7899
.add(layoutBuilder);
79100
}
80101

81102
private static void configureConsoleLogging() {
103+
System.clearProperty("log4j2.contextSelector");
104+
82105
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
83106
builder.setStatusLevel(Level.ERROR);
84107
builder.setConfigurationName("loggingConfig");
85108

86-
// create a console appender
87109
builder.add(createConsoleAppender("console", builder));
88110

89-
// create the new logger
90111
builder.add(builder.newRootLogger(Level.DEBUG)
91112
.add(builder.newAppenderRef("console")));
92113
Configurator.initialize(builder.build());
93114
}
94115

95116
private static void configureFileLogging() {
117+
System.clearProperty("log4j2.contextSelector");
118+
96119
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
97120
builder.setStatusLevel(Level.ERROR);
98121
builder.setConfigurationName("loggingConfig");
99122

100-
// create a console appender
101123
builder.add(createFileAppender("file", builder));
102124

103-
// create the new logger
104125
builder.add(builder.newRootLogger(Level.DEBUG)
105126
.add(builder.newAppenderRef("file")));
106127
Configurator.initialize(builder.build());
107128
}
108129

109130
private static void configureFileAndConsoleLogging() {
131+
System.clearProperty("log4j2.contextSelector");
132+
110133
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
111134
builder.setStatusLevel(Level.ERROR);
112135
builder.setConfigurationName("loggingConfig");
113136

114-
// create a console appender
115137
builder.add(createFileAppender("file", builder));
116138
builder.add(createConsoleAppender("console", builder));
117139

118-
// create the new logger
119140
builder.add(builder.newRootLogger(Level.DEBUG)
120141
.add(builder.newAppenderRef("file"))
121142
.add(builder.newAppenderRef("console")));
122143
Configurator.initialize(builder.build());
123144
}
145+
146+
private static void configureAsyncFileLogging() {
147+
System.setProperty("log4j2.contextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
148+
149+
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
150+
builder.setStatusLevel(Level.ERROR);
151+
builder.setConfigurationName("loggingConfig");
152+
153+
builder.add(createAsyncFileAppender("file", builder));
154+
155+
// create the new logger
156+
builder.add(builder.newAsyncRootLogger(Level.DEBUG)
157+
.add(builder.newAppenderRef("file")));
158+
Configurator.initialize(builder.build());
159+
}
160+
161+
private static void configureAsyncAndConsoleFileLogging() {
162+
System.setProperty("log4j2.contextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
163+
164+
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
165+
builder.setStatusLevel(Level.ERROR);
166+
builder.setConfigurationName("loggingConfig");
167+
168+
builder.add(createAsyncFileAppender("file", builder));
169+
builder.add(createConsoleAppender("console", builder));
170+
171+
// create the new logger
172+
builder.add(builder.newAsyncRootLogger(Level.DEBUG)
173+
.add(builder.newAppenderRef("file"))
174+
.add(builder.newAppenderRef("console")));
175+
Configurator.initialize(builder.build());
176+
}
124177
}

log4j2-logger/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<parent>
8-
<groupId>com.open-elements.logging</groupId>
9-
<artifactId>java-logger-benchmark</artifactId>
10-
<version>1.0-SNAPSHOT</version>
8+
<groupId>com.open-elements.logging</groupId>
9+
<artifactId>java-logger-benchmark</artifactId>
10+
<version>1.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>log4j2-logger</artifactId>
@@ -25,6 +25,11 @@
2525
<version>2.20.0</version>
2626
<scope>runtime</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.lmax</groupId>
30+
<artifactId>disruptor</artifactId>
31+
<version>3.4.4</version>
32+
</dependency>
2833
<dependency>
2934
<groupId>org.apache.logging.log4j</groupId>
3035
<artifactId>log4j-api</artifactId>

0 commit comments

Comments
 (0)