use of com.ms.silverking.time.SimpleTimer in project SilverKing by Morgan-Stanley.
the class FileWriteWithDelay method write.
public static void write(File file, long size, double rateLimitMBs, boolean randomBytes, Pair<Double, Double> delay) throws IOException {
OutputStream out;
long totalBytesWritten;
Stopwatch sw;
Timer displayTimer;
double pause;
boolean delayExecuted;
byte[] buff;
if (randomBytes) {
buff = buffer;
} else {
buff = buffer2;
}
delayExecuted = false;
totalBytesWritten = 0;
out = new FileOutputStream(file);
displayTimer = new SimpleTimer(TimeUnit.SECONDS, 1);
sw = new SimpleStopwatch();
do {
int bytesToWrite;
bytesToWrite = (int) Math.min(size - totalBytesWritten, buff.length);
out.write(buff, 0, bytesToWrite);
totalBytesWritten += bytesToWrite;
if (!delayExecuted && delay != null && sw.getSplitSeconds() > delay.getV1()) {
delayExecuted = true;
pause = delay.getV2();
} else {
double targetTime;
targetTime = totalBytesWritten / (rateLimitMBs * 1000000.0);
pause = targetTime - sw.getSplitSeconds();
}
if (displayTimer.hasExpired()) {
System.out.printf("%f\t%d\t%f\n", sw.getSplitSeconds(), totalBytesWritten, ((double) totalBytesWritten / sw.getSplitSeconds() / 1000000.0));
displayTimer.reset();
}
if (pause > 0.0) {
ThreadUtil.sleepSeconds(pause);
}
} while (totalBytesWritten < size);
out.close();
}
use of com.ms.silverking.time.SimpleTimer in project SilverKing by Morgan-Stanley.
the class WaitForHostPort method doWait.
public static int doWait(HostAndPort hostAndPort, int timeoutSeconds) {
Timer sw;
sw = new SimpleTimer(TimeUnit.SECONDS, timeoutSeconds);
while (!sw.hasExpired()) {
if (canConnect(hostAndPort)) {
return successExitCode;
}
ThreadUtil.sleep(pollIntervalMillis);
}
return errorExitCode;
}
use of com.ms.silverking.time.SimpleTimer in project SilverKing by Morgan-Stanley.
the class NamespaceRequest method waitForCompletion.
public boolean waitForCompletion(int waitLimitMillis) {
synchronized (incompletePeers) {
Timer timer;
timer = new SimpleTimer(TimeUnit.MILLISECONDS, waitLimitMillis);
while (incompletePeers.size() > 0 && !timer.hasExpired()) {
try {
incompletePeers.wait(timer.getRemainingMillis());
} catch (InterruptedException ie) {
}
}
if (incompletePeers.size() > 0) {
Log.warning("Unable to receive namespaces from: " + CollectionUtil.toString(incompletePeers));
}
return incompletePeers.size() == 0;
}
}
Aggregations