Goldfish: A collection of queue consumer/producer examples
At OSCON 2012 I sat through the disruptor presentation by trish gee. I wanted to learn more about it so Cord, Todd and I whipped up some sample code that uses it. After writing the proof of concept code, I wanted something to compare my implementation to, so I wrote some queue consumer code using other libraries and models.
I’m pretty pleased with the results and compiled all of my sample code into a small multi-module maven project which I put on GitHub.
The goal of the project wasn’t benchmark the performance of the different libraries, but to experiment with implementations. There is some timing code, but it should only be used as a general start/stop reference.
All of the examples use a simple event/payload object. Some libraries like disruptor take advantage of prepopulating these objects, so your milage may vary.
They also iterate over a number of events defined in the goldfish-common module’s BaseMain class. Currently the loop generates 5,000,000 events to process. For examples that use more than 1 thread, the number of cores/cpus minus 1 is used through
The first use case is the single module that uses disruptor to and a single thread executor service. This was the first disruptor example that I wrote and it is pretty simple. The size of the ring is 16384 and I’m using the
SleepingWaitStrategy classes when building the disruptor instance. The event handler doesn’t use the handled event either.
The second use case was to test multiple threads against a single disruptor instance. I don’t think that I did this the correct way, but the implementation is out there to review. Because each handler will receive every event for a given ring buffer, I use a count/modulo approach to disregard events that a thread shouldn’t handle.
I believe that a better alternative would be ensure that each thread has its own ringbuffer and a wrapper would be used to distribute work across them.
There are three other modules in the project that experiment with:
- Having a single thread with a blocking queue.
- Having multiple threads with a shared blocking queue.
- Having an executor service with registered handlers that use a shared blocking queue.
If you know of a library or framework that provides a queue with consumer/producer functionality, let me know because I’m looking for additional modules to add to the project.