Logback及Log4J效能測試
分別從兩個方面測試:
1、日誌輸出的格式對效能的影響,即日誌中包含動態方法名和行號對效能的影響
2、日誌長度對效能的影響
LogBack測試
本測試在Spring Boot框架基礎上進行,日誌配置檔案logback-spring.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="30 seconds"> <property name="log.path" value="D:\\log\\eurekaServer\\" /> <!-- 不帶方法名和行號 --> <appender name="LOGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${log.path}/eurekaServer.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/eurekaServer-%d{yyyyMMdd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>60</maxHistory> <totalSizeCap>20GB</totalSizeCap> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%-14.14thread] %-30.30logger{30} - %msg%n </pattern> </layout> </appender> <root level="info"> <appender-ref ref="LOGFILE" /> </root> <logger name="com.EurekaServer" level="INFO" additivity="false"> <appender-ref ref="LOGFILE" /> </logger> </configuration>
只帶行號:
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%-14.14thread] %-30.30logger{30} %-4line - %msg%n
只帶方法名:
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%-14.14thread] %-30.30logger{30} [%-20.20method] - %msg%n
帶方法名和行號:
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%-14.14thread] %-30.30logger{30} [%-20.20method]:%-4line - %msg%n
因為基於Spring Boot,所以只需在測試類外面加上如下兩個註解即可:
@RunWith(SpringRunner.class)
@SpringBootTest
整個測試類內容如下:
import java.util.ArrayList; import java.util.List; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.perf4j.StopWatch; import org.perf4j.slf4j.Slf4JStopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class EurekaServerApplicationTests { Logger log = LoggerFactory.getLogger(EurekaServerApplicationTests.class); @Ignore @Test public void contextLoads() { } // @Ignore @Test public void logMehodAndLine() throws InterruptedException{ StopWatch watch = new Slf4JStopWatch(); List<Long> times = new ArrayList<Long>(); int n = 10; int size = 1000000; Long totalTime = 0L; for(int i = 0;i<n;i++){ watch.start(); for(int j = 0;j<size - 1;j++){ log.info("Example message"); } watch.stop(); times.add(i, watch.getElapsedTime()); totalTime += watch.getElapsedTime(); Thread.sleep(500); } log.info("{}條耗時:{}ms",size,times.toString()); log.info("平均每{}條耗時:{}ms",size,totalTime/n); } /** * 測試日誌長度對時間的影響 */ @Ignore @Test public void logLength() throws InterruptedException{ StopWatch watch = new Slf4JStopWatch(); List<Long> times = new ArrayList<Long>(); int n = 50; int size = 10000; int length = 1000; StringBuffer info = new StringBuffer(); log.info("開始。。。。"); for(int i = 0;i < n; i++){ while(info.length()<(i+1)*length){ info.append("a"); } String infoStr = info.toString(); watch.start(); for(int j = 0;j<size - 1;j++){ log.info(infoStr); } watch.stop(); times.add(i, watch.getElapsedTime()); Thread.sleep(500); } for(int i = 0;i < n; i++){ log.info("長度為{}時,{}條共耗時:{}ms",(i+1)*length,size,times.get(i)); } } }
日誌輸出的格式對效能的影響
包含行號測試結果:
2017-12-17 16:47:28.795 INFO [main ] E.EurekaServerApplicationTests 42 - 1000000條耗時:[55742, 55179, 54539, 55101, 56176, 56494, 56571, 55494, 55828, 56417]ms2017-12-17 16:47:28.795 INFO [main ] E.EurekaServerApplicationTests 43 - 平均每1000000條耗時:55754ms
包含動態方法名測試結果:
2017-12-17 17:19:46.811 INFO [main ] E.EurekaServerApplicationTests [logMehodAndLine ] - 1000000條耗時:[56293, 55258, 54957, 55100, 55357, 53849, 55144, 55342, 54842, 56428]ms
2017-12-17 17:19:46.811 INFO [main ] E.EurekaServerApplicationTests [logMehodAndLine ] - 平均每1000000條耗時:55257ms
同時包含動態方法名和行號的測試結果:
2017-12-17 17:35:27.582 INFO [main ] E.EurekaServerApplicationTests [logMehodAndLine ]:42 - 1000000條耗時:[59606, 57803, 57607, 57038, 58981, 57819, 58784, 58220, 60550, 57470]ms
2017-12-17 17:35:27.583 INFO [main ] E.EurekaServerApplicationTests [logMehodAndLine ]:43 - 平均每1000000條耗時:58387ms
不包含動態方法名和行號的測試結果:
2017-12-17 17:49:30.144 INFO [main ] E.EurekaServerApplicationTests - 1000000條耗時:[3877, 3425, 3189, 3002, 4061, 3372, 3767, 3138, 4020, 2914]ms
2017-12-17 17:49:30.144 INFO [main ] E.EurekaServerApplicationTests - 平均每1000000條耗時:3476ms
可見,同時包含方法名和行號的時間與只包含方法名或行號差不多,但都比不包含方法名和行號需要的時間多,大概為16倍。
日誌長度對效能的影響
以包含方法名和行號、輸出到檔案為例
測試結果:
n=50、size = 10000、length = 1000時:
2017-12-17 15:35:55.726 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為1000時,10000條共耗時:884ms
2017-12-17 15:35:55.726 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為2000時,10000條共耗時:769ms
2017-12-17 15:35:55.726 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為3000時,10000條共耗時:1009ms
2017-12-17 15:35:55.726 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為4000時,10000條共耗時:1050ms
2017-12-17 15:35:55.726 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為5000時,10000條共耗時:805ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為6000時,10000條共耗時:835ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為7000時,10000條共耗時:956ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為8000時,10000條共耗時:776ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為9000時,10000條共耗時:1016ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為10000時,10000條共耗時:1054ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為11000時,10000條共耗時:1172ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為12000時,10000條共耗時:1189ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為13000時,10000條共耗時:1078ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為14000時,10000條共耗時:1780ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為15000時,10000條共耗時:2187ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為16000時,10000條共耗時:2096ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為17000時,10000條共耗時:2072ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為18000時,10000條共耗時:3175ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為19000時,10000條共耗時:3202ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為20000時,10000條共耗時:3065ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為21000時,10000條共耗時:3867ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為22000時,10000條共耗時:2614ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為23000時,10000條共耗時:3466ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為24000時,10000條共耗時:4425ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為25000時,10000條共耗時:5366ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為26000時,10000條共耗時:6404ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為27000時,10000條共耗時:6481ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為28000時,10000條共耗時:5930ms
2017-12-17 15:35:55.810 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為29000時,10000條共耗時:6752ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為30000時,10000條共耗時:5903ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為31000時,10000條共耗時:6539ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為32000時,10000條共耗時:8695ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為33000時,10000條共耗時:8874ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為34000時,10000條共耗時:10502ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為35000時,10000條共耗時:11454ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為36000時,10000條共耗時:11974ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為37000時,10000條共耗時:11695ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為38000時,10000條共耗時:12955ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為39000時,10000條共耗時:13296ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為40000時,10000條共耗時:13684ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為41000時,10000條共耗時:13281ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為42000時,10000條共耗時:14756ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為43000時,10000條共耗時:14626ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為44000時,10000條共耗時:17325ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為45000時,10000條共耗時:18513ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為46000時,10000條共耗時:21122ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為47000時,10000條共耗時:16580ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為48000時,10000條共耗時:15385ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為49000時,10000條共耗時:16923ms
2017-12-17 15:35:55.826 INFO [main ] E.EurekaServerApplicationTests [logLength ]:75 - 長度為50000時,10000條共耗時:17295ms
結論:時間會隨著長度增加而增加
Log4J測試:
效果應該差不多