001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018 package org.apache.logging.log4j.perf.jmh; 019 020 import java.io.File; 021 import java.util.Arrays; 022 import java.util.concurrent.TimeUnit; 023 024 import org.apache.logging.log4j.LogManager; 025 import org.apache.logging.log4j.Logger; 026 import org.apache.logging.log4j.core.LifeCycle; 027 import org.openjdk.jmh.annotations.BenchmarkMode; 028 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; 029 import org.openjdk.jmh.annotations.Level; 030 import org.openjdk.jmh.annotations.Mode; 031 import org.openjdk.jmh.annotations.OutputTimeUnit; 032 import org.openjdk.jmh.annotations.Scope; 033 import org.openjdk.jmh.annotations.Setup; 034 import org.openjdk.jmh.annotations.State; 035 import org.openjdk.jmh.annotations.TearDown; 036 037 /** 038 * Tests Log4j2 Async Loggers performance. 039 */ 040 // ============================== HOW TO RUN THIS TEST: ==================================== 041 // 042 // single thread: 043 // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5 044 // 045 // multiple threads (for example, 4 threads): 046 // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5 -t 4 -si true 047 // 048 // Usage help: 049 // java -jar log4j-perf/target/microbenchmarks.jar -help 050 // 051 @State(Scope.Thread) 052 public class AsyncLoggersBenchmark { 053 final static char[] CHARS = new char[500]; 054 static { 055 Arrays.fill(CHARS, 'a'); 056 } 057 final static String TEST = new String(CHARS); 058 059 Logger logger; 060 061 @Setup(Level.Trial) 062 public void up() { 063 new File("perftest.log").delete(); 064 System.setProperty("log4j.configurationFile", "perf3PlainNoLoc.xml"); 065 System.setProperty("Log4jContextSelector", 066 "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"); 067 logger = LogManager.getLogger(getClass()); 068 } 069 070 @TearDown(Level.Trial) 071 public void down() { 072 ((LifeCycle) LogManager.getContext(false)).stop(); 073 new File("perftest.log").delete(); 074 } 075 076 @GenerateMicroBenchmark 077 @BenchmarkMode(Mode.Throughput) 078 @OutputTimeUnit(TimeUnit.SECONDS) 079 public boolean throughputBaseline() { 080 return logger.isInfoEnabled(); 081 } 082 083 @GenerateMicroBenchmark 084 @BenchmarkMode(Mode.Throughput) 085 @OutputTimeUnit(TimeUnit.SECONDS) 086 public void throughput() { 087 logger.info(TEST); 088 } 089 090 @GenerateMicroBenchmark 091 @BenchmarkMode(Mode.SampleTime) 092 @OutputTimeUnit(TimeUnit.NANOSECONDS) 093 public boolean latencyBaseline() { 094 return logger.isInfoEnabled(); 095 } 096 097 @GenerateMicroBenchmark 098 @BenchmarkMode(Mode.SampleTime) 099 @OutputTimeUnit(TimeUnit.NANOSECONDS) 100 public void latency() { 101 logger.info(TEST); 102 } 103 }