uk.co.thebadgerset.junit.extensions.example.ThrottleTestPerfBase Maven / Gradle / Ivy
Go to download
JUnit Toolkit enhances JUnit with performance testing, asymptotic behaviour analysis, and concurrency testing.
/*
* 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.example;
import uk.co.thebadgerset.junit.extensions.AsymptoticTestCase;
import uk.co.thebadgerset.junit.extensions.Throttle;
/**
* ThrottleTestPerfBase is a base implementation of tests for performance testing the accuracy of throttle
* implementations.
*
* CRC Card
* Responsibilities Collaborations
* Check one second accuracy of throttle by running for number of ops per second.
* {@link Throttle}
*
*
* @author Rupert Smith
*/
public abstract class ThrottleTestPerfBase extends AsymptoticTestCase
{
/** Used to hold the test setup on a per thread basis. */
ThreadLocal threadSetup = new ThreadLocal();
/**
* Creates a named throttle test.
*
* @param name The name of the test.
*/
public ThrottleTestPerfBase(String name)
{
super(name);
}
/**
* Should be implemented to provide a throttle implementation to test.
*
* @return The throttle to test.
*/
public abstract Throttle getTestThrottle();
/**
* Check one second accuracy of throttle by running for number of ops per second.
*
* @param opsPerSecond The test size, interpreted as ops per second.
*/
public void testThrottleAccuracy(int opsPerSecond)
{
// Ensure the test throttle is setup on the current test thread.
Throttle testThrottle = threadSetup.get();
if (testThrottle == null)
{
testThrottle = getTestThrottle();
threadSetup.set(testThrottle);
}
// Setup the throttle to the desired rate and reset it.
testThrottle.setRate(opsPerSecond);
// Loop for the specified operations per second, to get a one second test.
// The first loop returns straight away, so one extra is done.
for (int i = 0; i <= opsPerSecond; i++)
{
testThrottle.throttle();
}
}
}