diff options
| author | Robert Greig <rgreig@apache.org> | 2007-01-22 17:30:29 +0000 |
|---|---|---|
| committer | Robert Greig <rgreig@apache.org> | 2007-01-22 17:30:29 +0000 |
| commit | 19d23d9e5882d8a487fa03d561b80513c5c0b71d (patch) | |
| tree | 3389a6e9ce703ce5acb3689f055d7ec9ccf0a5cd /java/perftests/src/main | |
| parent | 5cd7d4b442fb8552e3ce032e3a788fd0d52261da (diff) | |
| download | qpid-python-19d23d9e5882d8a487fa03d561b80513c5c0b71d.tar.gz | |
(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/qpid@498720 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/perftests/src/main')
| -rw-r--r-- | java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java | 3 | ||||
| -rw-r--r-- | java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java | 66 |
2 files changed, 68 insertions, 1 deletions
diff --git a/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java b/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java index 513e1609aa..1877a23056 100644 --- a/java/perftests/src/main/java/org/apache/qpid/ping/AbstractPingProducer.java +++ b/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/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java b/java/perftests/src/main/java/org/apache/qpid/ping/Throttle.java new file mode 100644 index 0000000000..9fb637149b --- /dev/null +++ b/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.
+ *
+ * <p><table id="crc"><caption>CRC Card</caption>
+ * <tr><th> Responsibilities <th> Collaborations
+ * </table>
+ *
+ * @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;
+ }
+}
|
