private boolean sendMessages( RingBuffer<byte[]> ringBuffer, long messagesPerSecond, int runtimeSeconds) throws InterruptedException { LOGGER.info("Rate: " + messagesPerSecond + ", for " + runtimeSeconds + "s"); long runtimeNanos = TimeUnit.SECONDS.toNanos(runtimeSeconds); long t0 = System.nanoTime(); long delta = 0; long sent = 0; try { do { delta = System.nanoTime() - t0; long shouldHaveSent = (messagesPerSecond * delta) / 1000000000; for (; sent < shouldHaveSent; sent++) { if (!send(ringBuffer)) { return false; } } LockSupport.parkNanos(1); } while (delta <= runtimeNanos); Thread.sleep(1000); return ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize()); } finally { while (!ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize())) { Thread.sleep(1000); } } }
private boolean send(RingBuffer<byte[]> ringBuffer) { if (ringBuffer.hasAvailableCapacity(1)) { long next = ringBuffer.next(); byte[] event = ringBuffer.get(next); System.arraycopy(input, 0, event, 0, event.length); ringBuffer.publish(next); return true; } else { errorCount++; return false; } }
/** Returns {@code true} if the specified disruptor still has unprocessed events. */ private static boolean hasBacklog(final Disruptor<?> theDisruptor) { final RingBuffer<?> ringBuffer = theDisruptor.getRingBuffer(); return !ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize()); }