01: package com.clarkware.junitperf;
02:
03: import java.util.Random;
04:
05: /**
06: * The <code>RandomTimer</code> is a <code>Timer</code>
07: * with a random delay and a uniformly distributed variation.
08: *
09: * @author <b>Mike Clark</b>
10: * @author Clarkware Consulting, Inc.
11: *
12: * @see com.clarkware.junitperf.Timer
13: */
14:
15: public class RandomTimer implements Timer {
16:
17: private final Random random;
18: private final long delay;
19: private final double variation;
20:
21: /**
22: * Constructs a <code>RandomTimer</code> with the
23: * specified minimum delay and variation.
24: *
25: * @param delay Minimum delay (ms).
26: * @param variation Variation (ms).
27: */
28: public RandomTimer(long delay, double variation) {
29: this .delay = delay;
30: this .variation = variation;
31: this .random = new Random();
32: }
33:
34: /**
35: * Returns the timer delay.
36: *
37: * @return Delay (ms).
38: */
39: public long getDelay() {
40: return (long) Math.abs((random.nextDouble() * variation)
41: + delay);
42: }
43: }
|