From 601fa8a026e7488bbb67e3f809f8255e2e6aeedd Mon Sep 17 00:00:00 2001 From: Robert Greig Date: Mon, 22 Jan 2007 17:30:29 +0000 Subject: (Patch submitted by Rupert Smith) Added configurations for all performance test setups to the pom. Commented out to not break build. Waiting on junit-toolkit-maven-plugin being added to maven repository. Create a throttle utility class and tests for it. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk@498720 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/qpid/ping/AbstractPingProducer.java | 3 +- .../main/java/org/apache/qpid/ping/Throttle.java | 66 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 qpid/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java (limited to 'qpid/java/perftests/src/main') diff --git a/qpid/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java b/qpid/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java index 513e1609aa..1877a23056 100644 --- a/qpid/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java +++ b/qpid/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java @@ -1,5 +1,6 @@ package org.apache.qpid.ping; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; import java.util.ArrayList; @@ -35,7 +36,7 @@ public abstract class AbstractPingProducer implements Runnable, ExceptionListene private static final Logger _logger = Logger.getLogger(AbstractPingProducer.class); /** Used to format time stamping output. */ - protected static final SimpleDateFormat timestampFormatter = new SimpleDateFormat("hh:mm:ss:SS"); + protected static final DateFormat timestampFormatter = new SimpleDateFormat("hh:mm:ss:SS"); /** Used to tell the ping loop when to terminate, it only runs while this is true. */ protected boolean _publish = true; diff --git a/qpid/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java b/qpid/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java new file mode 100644 index 0000000000..9fb637149b --- /dev/null +++ b/qpid/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java @@ -0,0 +1,66 @@ +package org.apache.qpid.ping; + +/** + * Throttle is a helper class used in situations where a controlled rate of processing is desired. It allows a certain + * number of operations-per-second to be defined and supplies a {@link #throttle} method that can only be called at + * most at that rate. The first call to the throttle method will return immediately, subsequent calls will introduce + * a short pause to fill out the remainder of the current cycle to attain the desired rate. If there is no remainder + * left then it will return immediately. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
+ * + * @author Rupert Smith + */ +public class Throttle +{ + /** Holds the length of the cycle in nano seconds. */ + long cycleLengthNanos = 0L; + + /** Records the nano time of the last call to the throttle method. */ + long lastTimeNanos = 0L; + + /** + * Sets up the desired rate of operation per second that the throttle method should restrict to. + * + * @param opsPerSecond The maximum number of calls per second that the throttle method will take. + */ + public void setRate(int opsPerSecond) + { + // Calculate the length of a cycle. + cycleLengthNanos = 1000000000 / opsPerSecond; + } + + /** + * Introduces a short pause to fill out any time left in the cycle since this method was last called, of length + * defined by a call to the {@link #setRate} method. + */ + public void throttle() + { + // Record the time now. + long currentTimeNanos = System.nanoTime(); + + // Check if there is any time remaining in the current cycle and introduce a short wait to fill out the + // remainder of the cycle if needed. + long remainingTimeNanos = cycleLengthNanos - (currentTimeNanos - lastTimeNanos); + + if (remainingTimeNanos > 0) + { + long milliWait = remainingTimeNanos / 1000000; + int nanoWait = (int) (remainingTimeNanos % 1000000); + + try + { + Thread.currentThread().sleep(milliWait, nanoWait); + } + catch (InterruptedException e) + { + // Just ignore this? + } + } + + // Keep the time of the last call to this method to calculate the next cycle. + lastTimeNanos = currentTimeNanos; + } +} -- cgit v1.2.1