Skip to content

Instantly share code, notes, and snippets.

@williamhogman
Created April 3, 2017 20:52
Show Gist options
  • Select an option

  • Save williamhogman/1aa507614b3e3c444c155752b612502b to your computer and use it in GitHub Desktop.

Select an option

Save williamhogman/1aa507614b3e3c444c155752b612502b to your computer and use it in GitHub Desktop.
package zens;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import com.facebook.collections.specialized.ColtLongHashSet;
import java.util.AbstractSet;
class LongEvent {
long l;
public void set(long l) {
this.l = l;
}
}
class MessageHandler implements EventHandler<LongEvent> {
private AbstractSet<Long> vals = new ColtLongHashSet(1000000);
public void onEvent(LongEvent e, long seq, boolean endOfBatch) {
vals.add(e.l);
}
public void reset() {
vals = new ColtLongHashSet(1000000);
}
}
public class App
{
public static void main( String[] args ) throws InterruptedException
{
// Executor that will be used to construct new threads for consumers
Executor executor = Executors.newCachedThreadPool();
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024 * 2 * 2;
// Construct the Disruptor
Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);
MessageHandler mh = new MessageHandler();
// Connect the handler
disruptor.handleEventsWith(mh);
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
// Warm up
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; l < 1000000; l++) {
bb.putLong(0, l);
ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);
}
for (int i = 0; i < 200; i++) {
mh.reset();
long start = System.nanoTime();
for (long l = 0; l < 1000000; l++) {
bb.putLong(0, l);
ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);
}
long end = System.nanoTime();
System.out.println("lulz: " + (end - start));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment