Last active
December 19, 2015 22:58
-
-
Save chaomao/6030920 to your computer and use it in GitHub Desktop.
Disruptor performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lmax.disruptor.support; | |
import com.lmax.disruptor.util.PaddedLong; | |
import java.util.Arrays; | |
import java.util.concurrent.BlockingQueue; | |
import static java.lang.Thread.sleep; | |
public final class EventCountingQueueProcessor implements Runnable | |
{ | |
private final BlockingQueue<Long> blockingQueue; | |
private final PaddedLong[] counters; | |
private final int index; | |
private final int[] array; | |
private volatile boolean running; | |
public EventCountingQueueProcessor(final BlockingQueue<Long> blockingQueue, final PaddedLong[] counters, final int index) | |
{ | |
this.blockingQueue = blockingQueue; | |
this.counters = counters; | |
this.index = index; | |
array = new int[500000]; | |
for (int i = 0; i < array.length; i++) { | |
array[i] = i; | |
} | |
} | |
public void halt() | |
{ | |
running = false; | |
} | |
@Override | |
public void run() | |
{ | |
running = true; | |
while (running) | |
{ | |
try | |
{ | |
long value = blockingQueue.take().longValue(); | |
Arrays.sort(array); //almost 10ms, I add it to simulate real work | |
counters[index].set(counters[index].get() + 1L); | |
} | |
catch (InterruptedException ex) | |
{ | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lmax.disruptor.support; | |
import com.lmax.disruptor.WorkHandler; | |
import com.lmax.disruptor.util.PaddedLong; | |
import java.util.Arrays; | |
import static java.lang.Thread.sleep; | |
public final class EventCountingWorkHandler | |
implements WorkHandler<ValueEvent> | |
{ | |
private int[] array; | |
private final PaddedLong[] counters; | |
private final int index; | |
public EventCountingWorkHandler(final PaddedLong[] counters, final int index) | |
{ | |
this.counters = counters; | |
this.index = index; | |
array = new int[500000]; | |
for (int i = 0; i < array.length; i++) { | |
array[i] = i; | |
} | |
} | |
@Override | |
public void onEvent(final ValueEvent event) throws Exception | |
{ | |
Arrays.sort(array); //almost 10ms, I add it to simulate real work | |
counters[index].set(counters[index].get() + 1L); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We change some configuration in | |
//private static final int NUM_WORKERS = 3; | |
//private static final int BUFFER_SIZE = 1024 * 8 * 2; | |
//private static final long ITERATIONS = 1000L * 10L; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment