/** Calls {@link System#nanoTime()}. */ @SuppressWarnings("GoodTime") // reading system time without TimeSource static long systemNanoTime() { return System.nanoTime(); }
/** * Returns System.nanoTime() unless the timeout has already elapsed. Returns 0L if and only if the * timeout has already elapsed. */ private static long initNanoTime(long timeoutNanos) { if (timeoutNanos <= 0L) { return 0L; } else { long startTime = System.nanoTime(); return (startTime == 0L) ? 1L : startTime; } }
/** * Returns the remaining nanos until the given timeout, or 0L if the timeout has already elapsed. * Caller must have previously sanitized timeoutNanos using toSafeNanos. */ private static long remainingNanos(long startTime, long timeoutNanos) { // assert timeoutNanos == 0L || startTime != 0L; // TODO : NOT CORRECT, BUT TESTS PASS ANYWAYS! // if (true) return timeoutNanos; // ONLY 2 TESTS FAIL IF WE DO: // if (true) return 0; return (timeoutNanos <= 0L) ? 0L : timeoutNanos - (System.nanoTime() - startTime); }
long now() { return System.nanoTime(); }
private void printEvent(String name) { long nowNanos = System.nanoTime(); if (name.equals("callStart")) { callStartNanos = nowNanos; } long elapsedNanos = nowNanos - callStartNanos; System.out.printf("%.3f %s%n", elapsedNanos / 1000000000d, name); }
@Override public void onOpen(WebSocket webSocket, Response response) { System.out.println("Executing test case " + number + "/" + count); startNanos.set(System.nanoTime()); }
@Override public Response intercept(Chain chain) throws IOException { long t1 = System.nanoTime(); Request request = chain.request(); logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); logger.info(String.format("Received response for %s in %.1fms%n%s", request.url(), (t2 - t1) / 1e6d, response.headers())); return response; } }
private void logWithTime(String message) { long timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); logger.log("[" + timeMs + " ms] " + message); }
@Override public EventListener create(Call call) { long callId = nextCallId.getAndIncrement(); System.out.printf("%04d %s%n", callId, call.request().url()); return new PrintingEventListener(callId, System.nanoTime()); } };
private static int randomIntFrom0to100() { // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml long x = System.nanoTime(); x ^= (x << 21); x ^= (x >>> 35); x ^= (x << 4); return Math.abs((int) x % 100); }
private static int randomIntFrom0to(int max) { // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml long x = System.nanoTime(); x ^= (x << 21); x ^= (x >>> 35); x ^= (x << 4); return Math.abs((int) x % max); }
private static int randomIntFrom0to100() { // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml long x = System.nanoTime(); x ^= (x << 21); x ^= (x >>> 35); x ^= (x << 4); return Math.abs((int) x % 100); }
private static int randomIntFrom0to(int max) { // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml long x = System.nanoTime(); x ^= (x << 21); x ^= (x >>> 35); x ^= (x << 4); return Math.abs((int) x % max); }
/** * Returns the number of milliseconds since time given by startNanoTime, which must have been * previously returned from a call to {@link System.nanoTime()}. */ long millisElapsedSince(long startNanoTime) { return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); }
@Override public void callStart(Call call) { startNs = System.nanoTime(); logWithTime("callStart: " + call.request()); }
@Override public void onComplete() { System.out.println("onComplete"); completeTime.set(System.nanoTime()); completedLatch.countDown(); }
@Override public void onComplete() { System.out.println("onComplete"); completeTime.set(System.nanoTime()); completedLatch.countDown(); }