/
TimeStressorCompletion.java
52 lines (45 loc) · 1.76 KB
/
TimeStressorCompletion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.radargun.stages.test.legacy;
import java.util.concurrent.TimeUnit;
import org.radargun.utils.TimeService;
import org.radargun.utils.Utils;
/**
* Time-based limitation of the stress test.
*
* @author Mircea Markus <Mircea.Markus@jboss.com>
*/
public class TimeStressorCompletion extends AbstractCompletion {
protected static final String PROGRESS_STRING = "Number of operations executed by this thread: %d. Elapsed time: %s. Remaining: %s. Total: %s.";
private volatile long lastPrint = -1;
private final long duration;
private final long logFrequency = TimeUnit.SECONDS.toNanos(20);
/**
* @param duration Duration of the test in nanoseconds. When duration is 0 the test will run indefinitely.
*/
public TimeStressorCompletion(long duration) {
this.duration = TimeUnit.MILLISECONDS.toNanos(duration);
}
@Override
public boolean moreToRun() {
boolean moreToRun = duration == 0 ? true : TimeService.nanoTime() < startTime + duration;
if (!moreToRun) {
runCompletionHandler();
}
return moreToRun;
}
@Override
public void logProgress(int executedOps) {
long now = TimeService.nanoTime();
//make sure this info is not printed more frequently than 20 secs
if (lastPrint < 0 || (now - lastPrint) < logFrequency) {
return;
}
synchronized (this) {
if (now - lastPrint < logFrequency) return;
lastPrint = now;
//make sure negative durations are not printed
long remaining = Math.max(0, (startTime + duration) - now);
log.infof(PROGRESS_STRING, executedOps, Utils.getNanosDurationString(now - startTime),
Utils.getNanosDurationString(remaining), Utils.getNanosDurationString(duration));
}
}
}