22
33import static com .openelements .logger .benchmarks .BenchmarkConstants .MEASUREMENT_ITERATIONS ;
44import static com .openelements .logger .benchmarks .BenchmarkConstants .MEASUREMENT_TIME_IN_SECONDS_PER_ITERATION ;
5+ import static com .openelements .logger .benchmarks .BenchmarkConstants .PARALLEL_THREAD_COUNT ;
56import static com .openelements .logger .benchmarks .BenchmarkConstants .WARMUP_ITERATIONS ;
67import static com .openelements .logger .benchmarks .BenchmarkConstants .WARMUP_TIME_IN_SECONDS_PER_ITERATION ;
78
2728import org .openjdk .jmh .annotations .Scope ;
2829import org .openjdk .jmh .annotations .Setup ;
2930import org .openjdk .jmh .annotations .State ;
31+ import org .openjdk .jmh .annotations .Threads ;
3032import 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}
0 commit comments