Skip to content

Commit

Permalink
Added simple "performance-test" which allows you to compare performan…
Browse files Browse the repository at this point in the history
…ce between difference modes a bit.
  • Loading branch information
Sander de Groot committed Jul 2, 2016
1 parent 42753c1 commit 23cfd67
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.github.sdegroot.logback.logbuffer;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.mock;

/**
* This is a "performance test" that doesn't assert anything. It just shows the differences a bit.
*/
public class BufferedContextualAppenderPerformanceTest {

private BufferedContextualAppender bufferedContextualAppender;
private BufferedAppenderWrapper stubAppender;

@Before
public void setUp() {
stubAppender = mock(BufferedAppenderWrapper.class);
bufferedContextualAppender = new BufferedContextualAppender(stubAppender, 50);
bufferedContextualAppender.setBufferFrom(Level.DEBUG);
bufferedContextualAppender.setBufferUntil(Level.INFO);
bufferedContextualAppender.setFlushBufferFrom(Level.WARN);
bufferedContextualAppender.setLogMessagesAfterFlush(true);
bufferedContextualAppender.setDropBelowBufferFrom(true);
}

@Test
public void Calls1000WithoutBufferFlush() {
final StopWatch stopWatch = new StopWatch();
stopWatch.start();

for (int i = 0; i < 1000; i++) {
bufferedContextualAppender.doAppend(createLoggingEvent(Level.DEBUG));
}

stopWatch.stop();
;
printStats(stopWatch, "1000 without buffer flush");
}

@Test
public void DirectCalls1000WithoutBuffer() {
final StopWatch stopWatch = new StopWatch();
stopWatch.start();

for (int i = 0; i < 1000; i++) {
stubAppender.appendDirectly(createLoggingEvent(Level.DEBUG));
}

stopWatch.stop();
;
printStats(stopWatch, "1000 without buffer");
}

@Test
public void Calls1000WithBufferFlushEvery50() {
final StopWatch stopWatch = new StopWatch();
stopWatch.start();

for (int i = 0; i < 1000; i++) {
Level level = Level.DEBUG;
if (i % 50 == 0) {
level = Level.WARN; // trigger buffer flush
}
bufferedContextualAppender.doAppend(createLoggingEvent(level));

}

stopWatch.stop();
printStats(stopWatch, "1000 with buffer flush");
}

private void printStats(StopWatch stopWatch, String stat) {
final long msTime = stopWatch.getTime();
final long nsTime = stopWatch.getNanoTime();
System.out.println(stat + " took " + nsTime + "ns (" + msTime + "ms)");
}

private static ILoggingEvent createLoggingEvent(Level level) {
final LoggingEvent loggingEvent = new LoggingEvent();
loggingEvent.setLevel(level);
return loggingEvent;
}

}

0 comments on commit 23cfd67

Please sign in to comment.