All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.thebadgerset.junit.extensions.BatchedThrottle Maven / Gradle / Ivy

Go to download

JUnit Toolkit enhances JUnit with performance testing, asymptotic behaviour analysis, and concurrency testing.

There is a newer version: 0.7
Show 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;

/**
 * BatchedThrottle is a {@link SleepThrottle} that uses batching to achieve much higher throttling rates than a sleep
 * throttle can. Sleep throttle has difficulties once the rate gets above a few hundred hertz, because the JVM cannot
 * generate timed pauses that are too short. BatchedThrottle gets around this by only inserting pauses once every so
 * many calls to the {@link #throttle()} method, and using a sleep throttle run at a lower rate. The rate for the sleep
 * throttle is chosen so that it remains under 100hz. The final throttling rate of this throttle is equal to the batch
 * size times the rate of the underlying sleep throttle.
 *
 * 

The batching calculation involves taking the log to the base 100 of the desired rate and rounding this to * an integer. The batch size is always an exact power of 100 because of the rounding. The rate for the underlying * sleep throttle is then chosen appropriately. * *

*
CRC Card
Responsibilities Collaborations *
Accept throttling rate in operations per second. *
Inject short pauses, occasionaly, to fill out processing cycles to a specified rate. *
* * @todo Should always round the log base 100 down to the nearest integer? * * @author Rupert Smith */ public class BatchedThrottle extends SleepThrottle { /** Holds the batch size. */ int batchSize; /** The call count within the current batch. */ long callCount; /** * Specifies the throttling rate in operations per second. * * @param hertz The throttling rate in cycles per second. */ public void setRate(float hertz) { // Log base 10 over 2 is used here to get a feel for what power of 100 the total rate is. // As the total rate goes up the powers of 100 the batch size goes up by powers of 100 to keep the // throttle rate in the range 1 to 100. int x = (int) (Math.log10(hertz) / 2); batchSize = (int) Math.pow(100, x); float throttleRate = hertz / batchSize; // Reset the call count. callCount = 0; // Set the sleep throttle super class at a rate within its abilities. super.setRate(throttleRate); } /** * Throttle calls to this method to the rate specified by the {@link #setRate(float)} method. */ public void throttle() { if ((callCount++ % batchSize) == 0) { super.throttle(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy