Skip to content

Commit

Permalink
Added multi threaded "performance test". Note that it's not really a …
Browse files Browse the repository at this point in the history
…performance test but it gives some insight
  • Loading branch information
Sander de Groot committed Jul 2, 2016
1 parent 23cfd67 commit d46d9d7
Showing 1 changed file with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static org.mockito.Mockito.mock;

/**
Expand All @@ -16,6 +21,7 @@ public class BufferedContextualAppenderPerformanceTest {

private BufferedContextualAppender bufferedContextualAppender;
private BufferedAppenderWrapper stubAppender;
private StopWatch stopWatch;

@Before
public void setUp() {
Expand All @@ -26,11 +32,12 @@ public void setUp() {
bufferedContextualAppender.setFlushBufferFrom(Level.WARN);
bufferedContextualAppender.setLogMessagesAfterFlush(true);
bufferedContextualAppender.setDropBelowBufferFrom(true);

stopWatch = new StopWatch();
}

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

for (int i = 0; i < 1000; i++) {
Expand All @@ -44,7 +51,6 @@ public void Calls1000WithoutBufferFlush() {

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

for (int i = 0; i < 1000; i++) {
Expand All @@ -58,7 +64,6 @@ public void DirectCalls1000WithoutBuffer() {

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

for (int i = 0; i < 1000; i++) {
Expand All @@ -74,6 +79,46 @@ public void Calls1000WithBufferFlushEvery50() {
printStats(stopWatch, "1000 with buffer flush");
}

@Test
public void MultithreadedCalls100000WithBufferFlushEvery50() throws InterruptedException {
stopWatch.start();

final ExecutorService executorService = Executors.newFixedThreadPool(10);
final AtomicLong totalCount = new AtomicLong(0);

final Runnable threadCode = new Runnable() {
@Override
public void run() {
final long frequency = Math.round(Math.random() * 10) + 1;
final int amount = 1000;
System.out.println("Using " + frequency + " as flush frequency");
for (int i = 0; i < amount; i++) {
Level level = Level.DEBUG;
if (i % frequency == 0) {
level = Level.WARN; // trigger buffer flush
}
bufferedContextualAppender.doAppend(createLoggingEvent(level));
}
totalCount.getAndAdd(amount);
}
};

for (int i = 0; i < 100; i++) {
executorService.execute(threadCode);
}



while (totalCount.get() < 100000) {
executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
}

executorService.shutdown();

stopWatch.stop();
printStats(stopWatch, "100,000 multi threaded with random buffer flushes");
}

private void printStats(StopWatch stopWatch, String stat) {
final long msTime = stopWatch.getTime();
final long nsTime = stopWatch.getNanoTime();
Expand Down

0 comments on commit d46d9d7

Please sign in to comment.