Search in sources :

Example 6 with SimpleTimer

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();
}
Also used : SimpleTimer(com.ms.silverking.time.SimpleTimer) Timer(com.ms.silverking.time.Timer) SimpleTimer(com.ms.silverking.time.SimpleTimer) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SimpleStopwatch(com.ms.silverking.time.SimpleStopwatch) Stopwatch(com.ms.silverking.time.Stopwatch) SimpleStopwatch(com.ms.silverking.time.SimpleStopwatch)

Example 7 with SimpleTimer

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;
}
Also used : SimpleTimer(com.ms.silverking.time.SimpleTimer) Timer(com.ms.silverking.time.Timer) SimpleTimer(com.ms.silverking.time.SimpleTimer)

Example 8 with SimpleTimer

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;
    }
}
Also used : SimpleTimer(com.ms.silverking.time.SimpleTimer) Timer(com.ms.silverking.time.Timer) SimpleTimer(com.ms.silverking.time.SimpleTimer)

Aggregations

SimpleTimer (com.ms.silverking.time.SimpleTimer)8 Timer (com.ms.silverking.time.Timer)7 SimpleStopwatch (com.ms.silverking.time.SimpleStopwatch)3 Stopwatch (com.ms.silverking.time.Stopwatch)2 Pair (com.ms.silverking.collection.Pair)1 UUIDBase (com.ms.silverking.id.UUIDBase)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1