在工作中,我主动创建了一个日志记录库,该记录库将在内部服务和项目中使用。
迄今为止,我已经创建了一个带有自定义类的概念证明,该类extends
PatternLayout
对输出日志条目中的某些字段进行了模糊处理。
现在的主要任务是断言性能是可以接受的,并且我的解决方案可以处理数百万条日志条目,而不会丢失消息。
我决定为我的自定义登录,默认登录布局和Log4j2编写性能测试,以便比较输出结果。我已经使用进行了单元测试jmh
。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE_APPLY_RULES" class="ch.qos.logback.core.FileAppender">
<file>logs\log.txt</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.ingenico.epayments.logging.rule.LoggingRulesApplicator">
<pattern>%msg%n</pattern>
<applyRules>true</applyRules>
<loggingRules>src/test/resources/logging-rules.json</loggingRules>
</layout>
</encoder>
</appender>
<appender name="ASYNC_FILE_APPLY_RULES" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="FILE_APPLY_RULES" />
<queueSize>256</queueSize>
<includeCallerData>false</includeCallerData>
<neverBlock>true</neverBlock>
</appender>
<root level="INFO">
<appender-ref ref="ASYNC_FILE_APPLY_RULES" />
</root>
</configuration>
@Slf4j
public class LogbackTest {
@Test
public void launchBenchmark() throws RunnerException {
Options opt = new OptionsBuilder()
.include(this.getClass().getName() + ".*")
.timeUnit(TimeUnit.MICROSECONDS)
.warmupTime(TimeValue.seconds(1))
.warmupIterations(2)
.measurementTime(TimeValue.seconds(1))
.measurementIterations(2)
.threads(2)
.forks(1)
.shouldFailOnError(true)
.shouldDoGC(true)
.addProfiler(StackProfiler.class)
.addProfiler(HotspotRuntimeProfiler.class)
.build();
new Runner(opt).run();
}
@BenchmarkMode(Mode.All)
@Measurement(iterations = 200, time = 200, timeUnit = MILLISECONDS)
@Benchmark
public void logBenchmark() {
log.info("{\"id\":\"12345\",\"cardData\":{\"bar\":\"bar\",\"foo\":\"foo\",\"number\":\"number\"}}");
}
}
在运行测试并在输出中,我看到了很多结果数字,但是我不知道应该比较哪个值,以及如何断言如果我有良好的性能结果,那不是因为消息被丢弃了吗?
您能否给我一些有关如何准备有效的性能测试的提示?
您有一个异步AsyncAppender
附加程序,可以将消息放入队列中。同时,JMH基准测试希望带@Benchmark
注释的方法中的代码被同步执行以计算所需的时间。这行不通。
IMO,您正在尝试一次测试太多内容,例如整个Logback堆栈。如果您开发了自定义布局类,请LoggingRulesApplicator
首先为此类编写一个JMH bechmark。
看一下现有的log4j2 JMH基准测试。使用NOOP附加程序可以简化那里的设置,您可能也应该这样做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句