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 package org.apache.logging.log4j.perf.jmh; 018 019 import java.io.File; 020 import java.util.Arrays; 021 import java.util.concurrent.TimeUnit; 022 023 import org.openjdk.jmh.annotations.BenchmarkMode; 024 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; 025 import org.openjdk.jmh.annotations.Level; 026 import org.openjdk.jmh.annotations.Mode; 027 import org.openjdk.jmh.annotations.OutputTimeUnit; 028 import org.openjdk.jmh.annotations.Scope; 029 import org.openjdk.jmh.annotations.Setup; 030 import org.openjdk.jmh.annotations.State; 031 import org.openjdk.jmh.annotations.TearDown; 032 import org.slf4j.Logger; 033 import org.slf4j.LoggerFactory; 034 035 import ch.qos.logback.core.spi.LifeCycle; 036 037 /** 038 * Tests Logback Async Appender 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 public class LogbackAsyncAppenderBenchmark { 052 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 @State(Scope.Benchmark) 060 public static class NormalState { 061 Logger logger; 062 063 @Setup(Level.Trial) 064 public void up() { 065 System.setProperty("logback.configurationFile", "perf-logback-async.xml"); 066 logger = (Logger) LoggerFactory.getLogger(getClass()); 067 } 068 069 @TearDown(Level.Trial) 070 public void down() { 071 ((LifeCycle) LoggerFactory.getILoggerFactory()).stop(); 072 new File("perftest.log").delete(); 073 } 074 } 075 076 @GenerateMicroBenchmark 077 @BenchmarkMode(Mode.Throughput) 078 @OutputTimeUnit(TimeUnit.SECONDS) 079 public boolean throughputBaseline(NormalState e) { 080 return e.logger.isInfoEnabled(); 081 } 082 083 @GenerateMicroBenchmark 084 @BenchmarkMode(Mode.Throughput) 085 @OutputTimeUnit(TimeUnit.SECONDS) 086 public void throughput(NormalState e) { 087 e.logger.info(TEST); 088 } 089 090 @GenerateMicroBenchmark 091 @BenchmarkMode(Mode.SampleTime) 092 @OutputTimeUnit(TimeUnit.NANOSECONDS) 093 public boolean latencyBaseline(NormalState e) { 094 return e.logger.isInfoEnabled(); 095 } 096 097 @GenerateMicroBenchmark 098 @BenchmarkMode(Mode.SampleTime) 099 @OutputTimeUnit(TimeUnit.NANOSECONDS) 100 public void latency(NormalState e) { 101 e.logger.info(TEST); 102 } 103 }