org.apache.qpid.junit.extensions.util
Class MathUtils

java.lang.Object
  extended by org.apache.qpid.junit.extensions.util.MathUtils

public class MathUtils
extends Object

Mathematical support methods for the toolkit. Caculating averages, variances, min/max for test latencies and generating linear/exponential sequences for test size/concurrency ramping up.

The sequence specifications are of the form [lowest(, ...)(, highest)](,sample=s)(,exp), where round brackets enclose optional values. Using this pattern form it is possible to specify a single value, a range of values divided into s samples, a range of values divided into s samples but distributed exponentially, or a fixed set of samples.

The duration arguments are of the form (dD)(hH)(mM)(sS), where round brackets enclose optional values. At least one of the optional values must be present.

CRC Card
Responsibilities Collaborations
Generate a sequene of integers from a sequence specification.
Parse an encoded duration into milliseconds.


Field Summary
static Pattern DURATION_PATTERN
          The regular expression that matches the duration expression.
static String DURATION_REGEXP
          The duration definition matching regular expression.
static Pattern NAME_VALUE_PATTERN
          The regular expression that matches name=value pairs and variations.
static String NAME_VALUE_REGEXP
          For matching name=value pairs.
static String NAME_VALUE_SEQUENCE_REGEXP
          For matching name=[n: ...
static String NAME_VALUE_VARIATION_REGEXP
          For matching name=[value1: value2: ...] variations.
static String SEQUENCE_REGEXP
          The sequence defintion matching regular expression.
 
Constructor Summary
MathUtils()
           
 
Method Summary
static int[] generateExpSequence(int start, int end, int steps)
          Given a start and end and a number of steps this method generates a sequence of expontentially spaced integer values, starting at the start (inclusive) and finishing at the end (inclusive) with the specified number of values in the sequence.
static int[] generateSequence(int start, int end, int steps)
          Given a start and end and a number of steps this method generates a sequence of evenly spaced integer values, starting at the start (inclusive) and finishing at the end (inclusive) with the specified number of values in the sequence.
static void main(String[] args)
          Runs a quick test of the sequence generation methods to confirm that they work as expected.
static int maxInArray(int[] values)
          Returns the maximum value in an array of integers.
static long parseDuration(String duration)
          Parses a duration defined as a string, giving a duration in days, hours, minutes and seconds into a number of milliseconds equal to that duration.
static int[] parseSequence(String sequenceDef)
          Parses a string defintion of a sequence into an int array containing the sequence.
static String printArray(int[] array)
          Pretty prints an array of ints as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEQUENCE_REGEXP

public static final String SEQUENCE_REGEXP
The sequence defintion matching regular expression.

See Also:
Constant Field Values

DURATION_REGEXP

public static final String DURATION_REGEXP
The duration definition matching regular expression.

See Also:
Constant Field Values

DURATION_PATTERN

public static final Pattern DURATION_PATTERN
The regular expression that matches the duration expression.


NAME_VALUE_REGEXP

public static final String NAME_VALUE_REGEXP
For matching name=value pairs.

See Also:
Constant Field Values

NAME_VALUE_VARIATION_REGEXP

public static final String NAME_VALUE_VARIATION_REGEXP
For matching name=[value1: value2: ...] variations.

See Also:
Constant Field Values

NAME_VALUE_SEQUENCE_REGEXP

public static final String NAME_VALUE_SEQUENCE_REGEXP
For matching name=[n: ... :m](:sample=s)(:exp) sequences.

See Also:
Constant Field Values

NAME_VALUE_PATTERN

public static final Pattern NAME_VALUE_PATTERN
The regular expression that matches name=value pairs and variations.

Constructor Detail

MathUtils

public MathUtils()
Method Detail

main

public static void main(String[] args)
Runs a quick test of the sequence generation methods to confirm that they work as expected.

Parameters:
args - The command line parameters.

generateSequence

public static int[] generateSequence(int start,
                                     int end,
                                     int steps)
Given a start and end and a number of steps this method generates a sequence of evenly spaced integer values, starting at the start (inclusive) and finishing at the end (inclusive) with the specified number of values in the sequence. The sequence returned may contain less than the specified number where the integer range between start and end is too small to contain that many.

As the results are integers, they will not be perfectly evenly spaced but a best-fit.

Parameters:
start - The sequence start.
end - The sequence end.
steps - The number of steps.
Returns:
The sequence.

generateExpSequence

public static int[] generateExpSequence(int start,
                                        int end,
                                        int steps)
Given a start and end and a number of steps this method generates a sequence of expontentially spaced integer values, starting at the start (inclusive) and finishing at the end (inclusive) with the specified number of values in the sequence. An exponentially spaced sequence is one where the ratio between any two consecutive numbers in the sequence remains constant. The sequence returned may contain less than the specified number where the difference between two consecutive values is too small (this is more likely at the start of the sequence, where the values are closer together).

As the results are integers, they will not be perfectly exponentially spaced but a best-fit.

Parameters:
start - The sequence start.
end - The sequence end.
steps - The number of steps.
Returns:
The sequence.

parseSequence

public static int[] parseSequence(String sequenceDef)
Parses a string defintion of a sequence into an int array containing the sequence. The definition will conform to the regular expression: "^(\[[0-9,]+\])(,samples=[0-9]+)?(,exp)?$". This splits it into three parts, an array of integers, the optional sample count and the optional exponential flag.

Parameters:
sequenceDef - The sequence definition.
Returns:
The sequence as a fully expanded int array.

parseDuration

public static long parseDuration(String duration)
Parses a duration defined as a string, giving a duration in days, hours, minutes and seconds into a number of milliseconds equal to that duration.

Parameters:
duration - The duration definition string.
Returns:
The duration in millliseconds.

printArray

public static String printArray(int[] array)
Pretty prints an array of ints as a string.

Parameters:
array - The array to pretty print.
Returns:
The pretty printed string.

maxInArray

public static int maxInArray(int[] values)
Returns the maximum value in an array of integers.

Parameters:
values - The array to find the amx in.
Returns:
The max value.


Licensed to the Apache Software Foundation