uk.co.thebadgerset.junit.extensions.SleepThrottle Maven / Gradle / Ivy
Go to download
JUnit Toolkit enhances JUnit with performance testing, asymptotic behaviour analysis, and concurrency testing.
The newest version!
/*
* Copyright 2007 Rupert Smith.
*
* 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 uk.co.thebadgerset.junit.extensions;
/**
* SleepThrottle is a Throttle implementation that generates short pauses using the thread sleep methods. As the pauses
* get shorter, this technique gets more innacurate. In practice, around 200 Hz is the cap rate for accuracy.
*
* CRC Card
* Responsibilities Collaborations
* Accept throttling rate in operations per second.
* Inject short pauses to fill out processing cycles to a specified rate.
*
*
* @todo Introduce an adaptive feedback to compensate for short term and accumulated errors.
*
* @todo Introduce active looping for very short pauses, using a dummy for loop to waste cpu time. Add a callibration
* method to initialize its callibration.
*
* @author Rupert Smith
*/
public class SleepThrottle implements Throttle
{
/** Holds the length of a single cycle in nano seconds. */
private long cycleTimeNanos;
/** Holds the time of the last call to the throttle method in nano seconds. */
private long lastTimeNanos;
/**
* Flag used to detect the first call to the throttle method. Zero or negative start time cannot be relied on
* to detect this as System.nanoTime can return zero or negative values.
*/
boolean firstCall = true;
/**
* Specifies the throttling rate in operations per second. This must be called with with a value, the inverse
* of which is a measurement in nano seconds, such that the number of nano seconds do not overflow a long integer.
* The value must also be larger than zero.
*
* @param hertz The throttling rate in cycles per second.
*/
public void setRate(float hertz)
{
// Check that the argument is above zero.
if (hertz <= 0.0f)
{
throw new IllegalArgumentException("The throttle rate must be above zero.");
}
// Calculate the cycle time.
cycleTimeNanos = (long) (1000000000f / hertz);
// Reset the first pass flag.
firstCall = false;
}
/**
* This method can only be called at the rate set by the {@link #setRate} method, if it is called faster than this
* it will inject short pauses to restrict the call rate to that rate.
*/
public void throttle()
{
// Get the current time in nanos.
long currentTimeNanos = System.nanoTime();
// Don't introduce any pause on the first call.
if (!firstCall)
{
// Check if there is any time left in the cycle since the last call to this method and introduce a short pause
// to fill that time if there is.
long remainingTimeNanos = cycleTimeNanos - (currentTimeNanos - lastTimeNanos);
if (remainingTimeNanos > 0)
{
long milliPause = remainingTimeNanos / 1000000;
int nanoPause = (int) (remainingTimeNanos % 1000000);
try
{
Thread.sleep(milliPause, nanoPause);
}
catch (InterruptedException e)
{
// This is deliberately ignored.
}
}
}
else
{
firstCall = false;
}
// Update the last time stamp.
lastTimeNanos = System.nanoTime();
// Update ideas...
// lastTimeNanos = lastTimeNanos + cycleTimeNanos.
// Some sort of combination of what current time is, what it should be accumulated over many calls, and
// the difference between what it is and what the short pause should have made it be. Adaptive feedback.
//
// accumulatedError = System.nanoTime - (lastTimeNanos + cycleTimeNanos).
// Compensate for accumulatedError < cycleTime.
// Don't compensate for large accumulated error. (threading, gc pauses). Start accumulation from scratch.
//
// shortTermError = System.nanoTime - currentTimeNanos - remainingTimeNanos.
// Subtract some (or all) of this from the next cycle.
}
}