Do not explicitly create threads in PacketQueue (#157)

* Do not explicitly create threads in PacketQueue, but borrow threads for execution from ExecutorService.

* Added method to release created packet in PacketQueue.

* Added constructor overload to override default ExecutorService of PacketQueue.

* Fixed inconsistent formatting.

* Added throttling support to PacketQueue.

* Added performance tests for various configurations of PacketQueue.

* Updated comments.

* Added test for fixed size thread pool to compare it with ForkJoinPool.

* Fixed comments.

* Changed default values of PacketQueue configuration to closer match old implementation.

* Removed unused constants.

* Added tests to reveal introduced bug. Fixed bug.

* Fixed comments in PacketsQueue.

* Do not hard code class name in thread pool name.

* Return 0 when handler is null (should be not be reachable, but anyway).

* Added test to check capacity constraint of PacketQueue.

* Replaced manual capacity check with checking offer return value.

* Made throttler class static.

* Added test to check that PacketQueue thread is release without work.

* Do GC before running performance benchmark.

* Fixed formatting and added timeout.

* Added test to check cooperative multi-tasking behavior when executor is shared between queues and has less threads then executor has.

* Minor code rearrangement.

* Use dedicated syncronization object for sate of AsyncPacketReader.

* Stop PacketQueue when benchmark is finished.

* Added constant for number of benchmark iterations.

* Minor code style enhancements.

* Added myself to the list of authors.

* Added test to check that million of PacketQueue can share single thread without eating CPU when idle.

* Extracted AsyncQueueHandler class.

* Fixed imports.

* Minor fixes.

* Minimize diff by undoing unnecessary changes.

* Minimized diff.

* Reduce diff.

* Added constructor argument null checks.

* Removed throttling and simplified AsyncQueueHandler.

* Reviewed usage of syncronized blocks.

* Moved variable closer to usage.

* Fixed according to code review.

* Added extra constructor.

* Removed cancel from onYield because it's unnecessary after removing throttling.

* Rearranged onYield and handleQueueItemsUntilEmpty, introduced rescheduleReader

* Updated copyright.
This commit is contained in:
Yury Yaroshevich 2018-12-12 19:37:28 +03:00 committed by bbaldino
parent 17b9bd9c17
commit c325421927
5 changed files with 1033 additions and 95 deletions

View File

@ -0,0 +1,285 @@
/*
* Copyright @ 2018 8x8, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ice4j.util;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.Logger; // Disambiguation
/**
* Asynchronously reads items from provided {@link #queue} on separate thread
* borrowed from {@link #executor} and process items with specified handler.
* Thread is not blocked when queue is empty and returned back to
* {@link #executor} pool. New or existing thread is borrowed from
* {@link #executor} when queue is non empty and {@link #reader} is not
* running
*
* @author Yura Yaroshevich
*/
public final class AsyncQueueHandler<T>
{
/**
* The {@link java.util.logging.Logger} used by the
* {@link AsyncQueueHandler} class and its instances for logging output.
*/
private static final java.util.logging.Logger logger
= Logger.getLogger(AsyncQueueHandler.class.getName());
/**
* The default <tt>ExecutorService</tt> to run <tt>AsyncQueueHandler</tt>
* when there is no executor injected in constructor.
*/
private final static ExecutorService sharedExecutor
= Executors.newCachedThreadPool(
new CustomizableThreadFactory(
AsyncQueueHandler.class.getName() + "-executor-", true));
/**
* Executor service to run {@link #reader}, which asynchronously
* invokes specified {@link #handler} on queued items.
*/
private final ExecutorService executor;
/**
* An {@link BlockingQueue <T>} whose items read on separate thread and
* processed by provided {@link #handler}.
*/
private final BlockingQueue<T> queue;
/**
* The {@link Handler<T>} used to handle items read from
* {@link #queue} by {@link #reader}.
*/
private final Handler<T> handler;
/**
* An identifier of current reader which is used for debugging purpose.
*/
private final String id;
/**
* Specifies the number of items allowed to be handled sequentially
* without yielding control to executor's thread. Specifying positive
* number allows implementation of cooperative multi-tasking
* between different {@link AsyncQueueHandler<T>} sharing
* same {@link ExecutorService}.
*/
private final long maxSequentiallyHandledItems;
/**
* A flag which indicates if reading of {@link #queue} is allowed
* to continue.
*/
private final AtomicBoolean running = new AtomicBoolean();
/**
* Synchronization object of current instance state, in particular
* used to resolve races between {@link #handleQueueItemsUntilEmpty()}
* and {@link #reader} exit. In particular synchronization object used to
* access to field {@link #readerFuture}.
*/
private final Object syncRoot = new Object();
/**
* Stores <tt>Future</tt> of currently executing {@link #reader}
*/
private Future<?> readerFuture;
/**
* Perpetually reads item from {@link #queue} and uses
* {@link #handler} on each of them.
*/
private final Runnable reader = new Runnable()
{
@Override
public void run()
{
long sequentiallyHandledItems = 0;
while (running.get())
{
if (maxSequentiallyHandledItems > 0 &&
sequentiallyHandledItems >= maxSequentiallyHandledItems)
{
onYield();
return;
}
T item;
synchronized (syncRoot)
{
item = queue.poll();
if (item == null)
{
cancel(false);
return;
}
}
sequentiallyHandledItems++;
try
{
handler.handleItem(item);
}
catch (Throwable e)
{
logger.warning("Failed to handle item: " + e);
}
}
}
};
/**
* Constructs instance of {@link AsyncQueueHandler<T>} which is capable of
* asynchronous reading provided queue from thread borrowed from executor to
* process items with provided handler.
* @param queue thread-safe queue which holds items to process
* @param handler an implementation of handler routine which will be
* invoked per each item placed in the queue.
* @param id optional identifier of current handler for debug purpose
* @param executor optional executor service to borrow threads from
*/
public AsyncQueueHandler(
BlockingQueue<T> queue,
Handler<T> handler,
String id,
ExecutorService executor)
{
this(queue, handler, id, executor, -1);
}
/**
* Constructs instance of {@link AsyncQueueHandler<T>} which is capable of
* asynchronous reading provided queue from thread borrowed from executor to
* process items with provided handler.
* @param queue thread-safe queue which holds items to process
* @param handler an implementation of handler routine which will be
* invoked per each item placed in the queue.
* @param id optional identifier of current handler for debug purpose
* @param executor optional executor service to borrow threads from
* @param maxSequentiallyHandledItems maximum number of items sequentially
* handled on thread borrowed from {@link #executor} before temporary
* releasing thread and re-acquiring it from {@link #executor}.
*/
public AsyncQueueHandler(
BlockingQueue<T> queue,
Handler<T> handler,
String id,
ExecutorService executor,
long maxSequentiallyHandledItems)
{
if (queue == null)
{
throw new IllegalArgumentException("queue is null");
}
if (handler == null)
{
throw new IllegalArgumentException("handler is null");
}
this.executor = executor != null ? executor : sharedExecutor;
this.queue = queue;
this.handler = handler;
this.id = id;
this.maxSequentiallyHandledItems = maxSequentiallyHandledItems;
}
/**
* Attempts to stop execution of {@link #reader} if running
*/
public void cancel()
{
cancel(true);
}
/**
* Checks if {@link #reader} is running on one of {@link #executor}
* thread and if no submits execution of {@link #reader} on executor.
*/
public void handleQueueItemsUntilEmpty()
{
synchronized (syncRoot)
{
if (readerFuture == null || readerFuture.isDone())
{
rescheduleReader();
}
}
}
/**
* Invoked when execution of {@link #reader} is about to temporary
* cancel and further execution need to be re-scheduled.
*/
private void onYield()
{
if (logger.isLoggable(java.util.logging.Level.FINE))
{
logger.fine("Yielding AsyncQueueHandler with ID = " + id);
}
rescheduleReader();
}
/**
* Attempts to cancel currently running reader.
* @param mayInterruptIfRunning indicates if {@link #reader} allowed
* to be interrupted if running
*/
private void cancel(boolean mayInterruptIfRunning)
{
synchronized (syncRoot)
{
running.set(false);
if (readerFuture != null)
{
readerFuture.cancel(mayInterruptIfRunning);
readerFuture = null;
}
}
}
/**
* Reschedules execution of {@link #reader} on {@link #executor}'s thread.
*/
private void rescheduleReader()
{
synchronized (syncRoot)
{
running.set(true);
readerFuture = executor.submit(reader);
}
}
/**
* A simple interface to handle enqueued {@link T} items.
* @param <T> the type of the item.
*/
public interface Handler<T>
{
/**
* Does something with an item.
* @param item the item to do something with.
*/
void handleItem(T item);
}
}

View File

@ -15,7 +15,6 @@
*/ */
package org.ice4j.util; package org.ice4j.util;
import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Logger; // Disambiguation. import java.util.logging.Logger; // Disambiguation.
@ -30,6 +29,7 @@ import java.util.logging.Logger; // Disambiguation.
* <br> MultiplexingSocket#received (and the rest of Multiplex* classes). * <br> MultiplexingSocket#received (and the rest of Multiplex* classes).
* *
* @author Boris Grozev * @author Boris Grozev
* @author Yura Yaroshevich
*/ */
public abstract class PacketQueue<T> public abstract class PacketQueue<T>
{ {
@ -65,9 +65,11 @@ public abstract class PacketQueue<T>
} }
/** /**
* The underlying {@link Queue} which holds packets. * The underlying {@link BlockingQueue} which holds packets.
* Used as synchronization object between {@link #close()}, {@link #get()}
* and {@link #doAdd(Object)}.
*/ */
private final Queue<T> queue; private final BlockingQueue<T> queue;
/** /**
* Whether this {@link PacketQueue} should store the {@code byte[]} or * Whether this {@link PacketQueue} should store the {@code byte[]} or
@ -76,13 +78,6 @@ public abstract class PacketQueue<T>
*/ */
private final boolean copy; private final boolean copy;
/**
* The capacity of this {@link PacketQueue}. If one of the {@code add}
* methods is called while the queue holds this many packets, the first
* packet in the queue will be dropped.
*/
private final int capacity;
/** /**
* The {@link QueueStatistics} instance optionally used to collect and print * The {@link QueueStatistics} instance optionally used to collect and print
* detailed statistics about this queue. * detailed statistics about this queue.
@ -90,16 +85,11 @@ public abstract class PacketQueue<T>
private final QueueStatistics queueStatistics; private final QueueStatistics queueStatistics;
/** /**
* The {@link Thread} optionally used to perpetually read packets from this * The optionally used {@link AsyncQueueHandler} to perpetually read packets
* queue and handle them using {@link #handler}. * from {@link #queue} on separate thread and handle them with provided
* packet handler.
*/ */
private final Thread thread; private final AsyncQueueHandler asyncQueueHandler;
/**
* The {@link org.ice4j.util.PacketQueue.PacketHandler} optionally used to
* handle packets read from this queue by {@link #thread}.
*/
private final PacketHandler<T> handler;
/** /**
* A string used to identify this {@link PacketQueue} for logging purposes. * A string used to identify this {@link PacketQueue} for logging purposes.
@ -107,9 +97,10 @@ public abstract class PacketQueue<T>
private final String id; private final String id;
/** /**
* Whether this queue has been closed. * Whether this queue has been closed. Field is denoted as volatile,
* because it is set in one thread and could be read in while loop in other.
*/ */
private boolean closed = false; private volatile boolean closed = false;
/** /**
* The number of packets which were dropped from this {@link PacketQueue} as * The number of packets which were dropped from this {@link PacketQueue} as
@ -163,9 +154,37 @@ public abstract class PacketQueue<T>
public PacketQueue(int capacity, boolean copy, public PacketQueue(int capacity, boolean copy,
boolean enableStatistics, String id, boolean enableStatistics, String id,
PacketHandler<T> packetHandler) PacketHandler<T> packetHandler)
{
this(capacity, copy, enableStatistics, id, packetHandler, null);
}
/**
* Initializes a new {@link PacketQueue} instance.
* @param capacity the capacity of the queue.
* @param copy whether the queue is to store the instances it is given via
* the various {@code add} methods, or create a copy.
* @param enableStatistics whether detailed statistics should be calculated
* and printed. WARNING: this will produce copious output (one line per
* packet added or removed).
* @param id the ID of the packet queue, to be used for logging.
* @param packetHandler An optional handler to be used by the queue for
* packets read from it. If a non-null value is passed the queue will
* start its own thread, which will read packets from the queue and execute
* {@code handler.handlePacket} on them. If set to null, no thread will be
* created, and the queue will provide access to the head element via
* {@link #get()} and {@link #poll()}.
* @param executor An optional executor service to use to execute
* packetHandler for items added to queue.
*/
public PacketQueue(
int capacity,
boolean copy,
boolean enableStatistics,
String id,
PacketHandler<T> packetHandler,
ExecutorService executor)
{ {
this.copy = copy; this.copy = copy;
this.capacity = capacity;
this.id = id; this.id = id;
queue = new ArrayBlockingQueue<>(capacity); queue = new ArrayBlockingQueue<>(capacity);
@ -174,83 +193,21 @@ public abstract class PacketQueue<T>
if (packetHandler != null) if (packetHandler != null)
{ {
handler = packetHandler; asyncQueueHandler = new AsyncQueueHandler<T>(
queue,
thread = new Thread(){ new HandlerAdapter(packetHandler),
@Override id,
public void run() executor,
{ packetHandler.maxSequentiallyProcessedPackets());
runInReadingThread();
}
};
thread.setName(getClass().getName() + "-" + id);
thread.setDaemon(true);
thread.start();
} }
else else
{ {
thread = null; asyncQueueHandler = null;
handler = null;
} }
logger.fine("Initialized a PacketQueue instance with ID " + id); logger.fine("Initialized a PacketQueue instance with ID " + id);
} }
/**
* Perpetually reads packets from this {@link PacketQueue} and uses
* {@link #handler} on each of them.
*/
private void runInReadingThread()
{
if (Thread.currentThread() != thread)
{
logger.warning("runInReadingThread executing in "
+ Thread.currentThread());
return;
}
if (handler == null)
{
logger.warning("No handler set, the reading thread will be stopped.");
return;
}
while (!closed)
{
T pkt;
synchronized (queue)
{
pkt = queue.poll();
if (pkt == null)
{
try
{
queue.wait(100);
}
catch (InterruptedException ie)
{
}
continue;
}
}
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
try
{
handler.handlePacket(pkt);
}
catch (Exception e)
{
logger.warning("Failed to handle packet: " + e);
}
}
}
/** /**
* Adds a packet represented by a {@code byte[]} with a corresponding * Adds a packet represented by a {@code byte[]} with a corresponding
* offset and length to this queue. * offset and length to this queue.
@ -324,7 +281,7 @@ public abstract class PacketQueue<T>
synchronized (queue) synchronized (queue)
{ {
if (queue.size() >= capacity) while (!queue.offer(pkt))
{ {
// Drop from the head of the queue. // Drop from the head of the queue.
T p = queue.poll(); T p = queue.poll();
@ -346,10 +303,14 @@ public abstract class PacketQueue<T>
{ {
queueStatistics.add(System.currentTimeMillis()); queueStatistics.add(System.currentTimeMillis());
} }
queue.offer(pkt);
queue.notifyAll(); queue.notifyAll();
} }
if (asyncQueueHandler != null)
{
asyncQueueHandler.handleQueueItemsUntilEmpty();
}
} }
/** /**
@ -360,7 +321,7 @@ public abstract class PacketQueue<T>
*/ */
public T get() public T get()
{ {
if (handler != null) if (asyncQueueHandler != null)
{ {
// If the queue was configured with a handler, it is running its // If the queue was configured with a handler, it is running its
// own reading thread, and reading from it via this interface would // own reading thread, and reading from it via this interface would
@ -407,7 +368,7 @@ public abstract class PacketQueue<T>
if (closed) if (closed)
return null; return null;
if (handler != null) if (asyncQueueHandler != null)
{ {
// If the queue was configured with a handler, it is running its // If the queue was configured with a handler, it is running its
// own reading thread, and reading from it via this interface would // own reading thread, and reading from it via this interface would
@ -428,12 +389,23 @@ public abstract class PacketQueue<T>
} }
} }
/**
* Closes current <tt>PacketQueue</tt> instance. No items will be added
* to queue when it's closed. Threads which were blocked in {@link #get()}
* will receive <tt>null</tt>. Asynchronous queue processing by
* {@link #asyncQueueHandler} is stopped.
*/
public void close() public void close()
{ {
if (!closed) if (!closed)
{ {
closed = true; closed = true;
if (asyncQueueHandler != null)
{
asyncQueueHandler.cancel();
}
synchronized (queue) synchronized (queue)
{ {
queue.notifyAll(); queue.notifyAll();
@ -481,6 +453,18 @@ public abstract class PacketQueue<T>
protected abstract T createPacket( protected abstract T createPacket(
byte[] buf, int off, int len, Object context); byte[] buf, int off, int len, Object context);
/**
* Releases packet when it is handled by provided packet handler.
* This method is not called when <tt>PacketQueue</tt> was created without
* handler and hence no automatic queue processing is done.
* Default implementation is empty, but it might be used to implement
* packet pooling to re-use them.
* @param pkt packet to release
*/
protected void releasePacket(T pkt)
{
}
/** /**
* A simple interface to handle packets. * A simple interface to handle packets.
* @param <T> the type of the packets. * @param <T> the type of the packets.
@ -494,5 +478,62 @@ public abstract class PacketQueue<T>
* {@code false} otherwise. * {@code false} otherwise.
*/ */
boolean handlePacket(T pkt); boolean handlePacket(T pkt);
/**
* Specifies the number of packets allowed to be processed sequentially
* without yielding control to executor's thread. Specifying positive
* number will allow other possible queues sharing same
* {@link ExecutorService} to process their packets.
* @return positive value to specify max number of packets which allows
* implementation of cooperative multi-tasking between different
* {@link PacketQueue} sharing same {@link ExecutorService}.
*/
default long maxSequentiallyProcessedPackets()
{
return -1;
}
}
/**
* An adapter class implementing {@link AsyncQueueHandler.Handler<T>}
* to wrap {@link PacketHandler<T>}.
*/
private final class HandlerAdapter implements AsyncQueueHandler.Handler<T>
{
/**
* An actual handler of packets.
*/
private final PacketHandler<T> handler;
/**
* Constructs adapter of {@link PacketHandler<T>} to
* {@link AsyncQueueHandler.Handler<T>} interface.
* @param handler an handler instance to adapt
*/
HandlerAdapter(PacketHandler<T> handler)
{
this.handler = handler;
}
/**
* {@inheritDoc}
*/
@Override
public void handleItem(T item)
{
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
try
{
handler.handlePacket(item);
}
finally
{
releasePacket(item);
}
}
} }
} }

View File

@ -0,0 +1,62 @@
package org.ice4j.util;
import java.util.concurrent.*;
/**
* Concrete dummy implementation of PacketQueue. Intended for launching tests
* against base implementation of PacketQueue.
*
* @author Yura Yaroshevich
*/
class DummyQueue
extends PacketQueue<DummyQueue.Dummy>
{
DummyQueue(
int capacity,
PacketHandler<Dummy> packetHandler,
ExecutorService executor)
{
super(capacity, false, false, "DummyQueue", packetHandler,
executor);
}
DummyQueue(int capacity)
{
super(capacity, false, false, "DummyQueue", null, null);
}
@Override
public byte[] getBuffer(Dummy pkt)
{
return null;
}
@Override
public int getOffset(Dummy pkt)
{
return 0;
}
@Override
public int getLength(Dummy pkt)
{
return 0;
}
@Override
public Object getContext(Dummy pkt)
{
return null;
}
@Override
protected Dummy createPacket(byte[] buf, int off, int len,
Object context)
{
return new Dummy();
}
static class Dummy {
int seed;
}
}

View File

@ -0,0 +1,236 @@
package org.ice4j.util;
import org.junit.*;
import java.time.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Logger; // Disambiguation
/**
* Contains test for checking performance aspects of various
* PacketQueue configuration
*
* @author Yura Yaroshevich
*/
@Ignore("Check only performance aspect of PacketQueue")
public class PacketQueueBenchmarkTests
{
/**
* The <tt>Logger</tt> used by the <tt>PacketQueueBenchmarkTests</tt> class
* and its instances for logging output.
*/
private static final java.util.logging.Logger logger
= Logger.getLogger(PacketQueueBenchmarkTests.class.getName());
/**
* Number of iteration to run benchmark and compute
* average execution time.
*/
private final int numberOfBenchmarkIterations = 100;
/**
* Simulates number of concurrent existing PacketQueue instances inside
* application. In case of JVB it is linear to number of connected peers
* to JVB instance.
*/
private final int numberOfQueues = 800;
/**
* Simulates number of messages processed via single PacketQueue
*/
private final int numberOfItemsInQueue = 1000;
/**
* Simulates the computation "weight" of single item processed by
* PacketQueue
*/
private final int singleQueueItemProcessingWeight = 300;
@Test
public void testMultiplePacketQueueThroughputWithThreadPerQueue()
throws Exception
{
/*
* This test roughly simulates initial implementation of PacketQueue
* when each PacketQueue instance has it's own processing thread
*/
measureBenchmark("ThreadPerQueuePool", () -> {
final ExecutorService executorService
= Executors.newFixedThreadPool(numberOfQueues);
Duration duration = runBenchmark(
executorService,
-1 /* Disable cooperative multi-tasking mode,
which is not relevant when each queue has it's own processing
thread*/);
executorService.shutdownNow();
return duration;
});
}
@Test
public void testMultiplePacketQueueThroughputWithCachedThreadPerQueue()
throws Exception
{
/*
* This test is slight modification of previous test, but now threads
* are re-used between PacketQueues when possible.
*/
measureBenchmark("CachedThreadPerQueuePool", () -> {
final ExecutorService executorService
= Executors.newCachedThreadPool();
Duration duration = runBenchmark(
executorService,
-1 /* Disable cooperative multi-tasking mode,
which is not relevant when each queue has it's own processing
thread*/);
executorService.shutdownNow();
return duration;
});
}
@Test
public void testMultiplePacketQueueThroughputWithFixedSizePool()
throws Exception
{
/*
* This test creates pool with limited number of threads, all
* PacketQueues share threads in cooperative multi-tasking mode.
*/
measureBenchmark("FixedSizeCPUBoundPool", () -> {
final ExecutorService executorService
= Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
Duration duration = runBenchmark(
executorService,
50 /* Because queues will share executor
with limited number of threads, so configure cooperative
multi-tasking mode*/);
executorService.shutdownNow();
return duration;
});
}
@Test
public void testMultiplePacketQueueThroughputWithForkJoinPool()
throws Exception
{
/*
* This test check proposed change to PacketQueue implementation when
* all created PacketQueues share single ExecutorService with limited
* number of threads. Execution starvation is resolved by implementing
* cooperative multi-tasking when each PacketQueue release it's thread
* borrowed for ExecutorService so other PacketQueue instances can
* proceed with execution.
* This modification has noticeable better performance when executed
* on system which is already loaded by other concurrent tasks.
*/
measureBenchmark("ForkJoinCPUBoundPool", () -> {
final ExecutorService executorService
= Executors.newWorkStealingPool(
Runtime.getRuntime().availableProcessors());
Duration duration = runBenchmark(
executorService,
50 /* Because queues will share executor
with limited number of threads, so configure cooperative
multi-tasking mode*/);
executorService.shutdownNow();
return duration;
});
}
private Duration runBenchmark(
final ExecutorService executor,
final long maxSequentiallyPackets)
throws InterruptedException
{
final CountDownLatch completionGuard
= new CountDownLatch(numberOfItemsInQueue * numberOfQueues);
final ArrayList<DummyQueue> queues = new ArrayList<>();
for (int i = 0; i < numberOfQueues; i++) {
queues.add(new DummyQueue(
numberOfItemsInQueue,
new PacketQueue.PacketHandler<DummyQueue.Dummy>()
{
@Override
public boolean handlePacket(DummyQueue.Dummy pkt)
{
double result = 0;
// some dummy computationally exp
final int end
= pkt.seed + singleQueueItemProcessingWeight;
for (int i = pkt.seed; i < end; i++)
{
result += Math.log(Math.sqrt(i));
}
completionGuard.countDown();
return result > 0;
}
@Override
public long maxSequentiallyProcessedPackets()
{
return maxSequentiallyPackets;
}
},
executor));
}
long startTime = System.nanoTime();
for (DummyQueue queue : queues)
{
for (int i = 0; i < numberOfItemsInQueue; i++)
{
queue.add(new DummyQueue.Dummy());
}
}
completionGuard.await();
long endTime = System.nanoTime();
for (DummyQueue queue : queues) {
queue.close();
}
return Duration.ofNanos(endTime - startTime);
}
private void measureBenchmark(String name, Callable<Duration> runWithDuration) throws Exception
{
final ArrayList<Duration> experimentDuration = new ArrayList<>();
for (int i = 0; i < 1 + numberOfBenchmarkIterations; i++)
{
System.gc();
final Duration duration = runWithDuration.call();
if (i != 0)
{
experimentDuration.add(duration);
}
}
long totalNanos = 0;
for (Duration duration : experimentDuration)
{
totalNanos += duration.toNanos();
}
long averageNanos = totalNanos / experimentDuration.size();
long sumSquares = 0;
for (Duration duration : experimentDuration)
{
long diff = Math.abs(duration.toNanos() - averageNanos);
sumSquares = diff * diff;
}
double stdDev
= Math.sqrt((1.0 / (experimentDuration.size() - 1)) * sumSquares);
System.out.println(name
+ " : avg = " + TimeUnit.NANOSECONDS.toMillis(averageNanos) + " ms"
+ ", std_dev = " + TimeUnit.NANOSECONDS.toMillis((long)stdDev) + " ms");
}
}

View File

@ -0,0 +1,314 @@
package org.ice4j.util;
import org.junit.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.Logger; // Disambiguation
/**
* Test various aspects of {@link PacketQueue} implementation.
*
* @author Yura Yaroshevich
*/
public class PacketQueueTests
{
/**
* The <tt>Logger</tt> used by the <tt>PacketQueueTests</tt> class and
* its instances for logging output.
*/
private static final java.util.logging.Logger logger
= Logger.getLogger(PacketQueueTests.class.getName());
@Test
public void testAddingItemToQueueNotifiesBlockedThreadsImmediately()
throws Exception
{
final DummyQueue dummyQueue = new DummyQueue(10);
final CompletableFuture<DummyQueue.Dummy> dummyItem =
CompletableFuture.supplyAsync(dummyQueue::get);
try
{
// This block surrounded with try/catch necessary to ensure that
// thread calling PacketQueue::get blocked before items is added
// to queue. Giving 50ms for CompletableFuture to stuck on get.
final DummyQueue.Dummy nullDummy
= dummyItem.get(50, TimeUnit.MILLISECONDS);
Assert.fail("There is no items in queue, must not be here");
}
catch (TimeoutException e)
{
// no item is added during 50 ms into queue.
}
final DummyQueue.Dummy pushedItem = new DummyQueue.Dummy();
dummyQueue.add(pushedItem);
try
{
// checks that thread stuck in PacketQueue::get notified
// "immediately" when item added to queue. Giving a few ms for
// CompletableFuture to transit to completed state.
final DummyQueue.Dummy poppedItem = dummyItem.get(
50, TimeUnit.MILLISECONDS);
Assert.assertEquals(pushedItem, poppedItem);
}
catch (TimeoutException e)
{
Assert.fail("Expected that blocked thread notified immediately "
+ "about item added to queue");
}
}
@Test
public void testClosingQueueImmediatelyNotifiesAllThreadsBlockedOnGet()
throws Exception
{
final DummyQueue dummyQueue = new DummyQueue(10);
final ArrayList<CompletableFuture<DummyQueue.Dummy>>
dummyItems = new ArrayList<>();
for (int i = 0; i < ForkJoinPool.getCommonPoolParallelism(); i++)
{
dummyItems.add(CompletableFuture.supplyAsync(dummyQueue::get));
}
for (CompletableFuture<DummyQueue.Dummy> dummyItem : dummyItems)
{
try
{
// This block surrounded with try/catch necessary to ensure that
// thread calling PacketQueue::get blocked before items is added
// to queue. Giving 50ms for CompletableFuture to stuck on get.
final DummyQueue.Dummy nullDummy
= dummyItem.get(50, TimeUnit.MILLISECONDS);
Assert.fail("There is no items in queue, must not be here");
}
catch (TimeoutException e)
{
// no item is added during 50 ms into queue.
}
}
dummyQueue.close();
for (CompletableFuture<DummyQueue.Dummy> dummyItem : dummyItems)
{
try
{
// checks that thread stuck in PacketQueue::get notified
// "immediately" when PacketQueue is stopped. Giving 1 ms for
// CompletableFuture to transit to completed state.
final DummyQueue.Dummy poppedItem = dummyItem.get(
1, TimeUnit.MILLISECONDS);
Assert.assertNull("When PacketQueue is closed "
+ "null must be returned", poppedItem);
}
catch (TimeoutException e)
{
Assert.fail("Expected that blocked thread notified immediately "
+ "when queue is stopped");
}
}
}
@Test
public void testAddingWhenCapacityReachedRemovesOldestItem()
{
final int capacity = 10;
final DummyQueue dummyQueue = new DummyQueue(capacity);
for (int i = 0; i < capacity + 1; i++)
{
DummyQueue.Dummy item = new DummyQueue.Dummy();
item.seed = i;
dummyQueue.add(item);
}
for (int i = 0; i < capacity + 1; i++)
{
final DummyQueue.Dummy item = dummyQueue.poll();
if (i == capacity)
{
Assert.assertNull(item);
}
else
{
Assert.assertNotEquals("Oldest item must be removed when "
+ "item exceeding capacity added", 0, item.seed);
}
}
}
@Test
public void testPacketQueueReaderThreadIsReleasedWhenPacketQueueEmpty()
throws Exception
{
final ExecutorService singleThreadExecutor
= Executors.newSingleThreadExecutor();
final CountDownLatch queueCompletion = new CountDownLatch(1);
final DummyQueue queue = new DummyQueue(
10,
pkt -> {
queueCompletion.countDown();
return true;
},
singleThreadExecutor);
queue.add(new DummyQueue.Dummy());
final boolean completed
= queueCompletion.await(50, TimeUnit.MILLISECONDS);
Assert.assertTrue("Expected all queued items are handled "
+ "at this time point", completed);
Future<?> executorCompletion = singleThreadExecutor.submit(() -> {
// do nothing, just pump Runnable via executor's thread to
// verify it's not stuck
});
try
{
executorCompletion.get(10, TimeUnit.MILLISECONDS);
}
catch (TimeoutException e)
{
Assert.fail("Executors thread must be released by PacketQueue "
+ "when queue is empty");
}
singleThreadExecutor.shutdownNow();
}
@Test
public void testPacketQueueCooperativeMultiTaskingWhenSharingExecutor()
throws Exception
{
final int maxSequentiallyProcessedPackets = 1;
final int queueCapacity = 10 * maxSequentiallyProcessedPackets;
final CountDownLatch completionGuard
= new CountDownLatch(2 * queueCapacity);
final ExecutorService singleThreadExecutor
= Executors.newSingleThreadExecutor();
final AtomicInteger queue1Counter = new AtomicInteger();
final AtomicInteger queue2Counter = new AtomicInteger();
final AtomicBoolean queuesEvenlyProcessed
= new AtomicBoolean(true);
final BiFunction<AtomicInteger,
AtomicInteger,
PacketQueue.PacketHandler<DummyQueue.Dummy>>
newPacketQueue = (AtomicInteger self, AtomicInteger other) ->
new PacketQueue.PacketHandler<DummyQueue.Dummy>()
{
@Override
public boolean handlePacket(DummyQueue.Dummy pkt)
{
int diff = Math.abs(
self.incrementAndGet() - other.get());
queuesEvenlyProcessed.set(queuesEvenlyProcessed.get()
&& diff <= maxSequentiallyProcessedPackets());
completionGuard.countDown();
return false;
}
@Override
public long maxSequentiallyProcessedPackets()
{
return maxSequentiallyProcessedPackets;
}
};
final DummyQueue queue1 = new DummyQueue(
queueCapacity,
newPacketQueue.apply(queue1Counter, queue2Counter),
singleThreadExecutor);
final DummyQueue queue2 = new DummyQueue(
queueCapacity,
newPacketQueue.apply(queue2Counter, queue1Counter),
singleThreadExecutor);
for (int i = 0; i < queueCapacity; i++)
{
queue1.add(new DummyQueue.Dummy());
queue2.add(new DummyQueue.Dummy());
}
final boolean completed
= completionGuard.await(1, TimeUnit.SECONDS);
Assert.assertTrue("Expected all queued items are handled "
+ "at this time point", completed);
Assert.assertTrue(
"Queues sharing same thread with configured cooperative"
+ " multi-tasking must yield execution to be processed evenly",
queuesEvenlyProcessed.get());
singleThreadExecutor.shutdownNow();
}
@Test
public void testManyQueuesCanShareSingleThread()
throws Exception
{
final ExecutorService singleThreadedExecutor
= Executors.newSingleThreadExecutor();
final int numberOfQueues = 1_000_000;
final ArrayList<DummyQueue> queues = new ArrayList<>();
final CountDownLatch completionGuard
= new CountDownLatch(numberOfQueues);
for (int i = 0; i < numberOfQueues; i++)
{
queues.add(new DummyQueue(1,
pkt -> {
completionGuard.countDown();
return true;
},
singleThreadedExecutor));
}
final DummyQueue.Dummy dummyPacket = new DummyQueue.Dummy();
for (DummyQueue queue : queues)
{
// Push item for processing to cause borrowing execution
// thread from ExecutorService
queue.add(dummyPacket);
}
final boolean completed
= completionGuard.await(1, TimeUnit.SECONDS);
Assert.assertTrue("Expected all queued items are handled "
+ "at this time point", completed);
final List<Runnable> packetReaders
= singleThreadedExecutor.shutdownNow();
Assert.assertEquals("Queues must not utilize thread when"
+ "there is no work.", 0, packetReaders.size());
for (DummyQueue queue : queues)
{
queue.close();
}
}
}