/** * Time the given function multiple times. * * @param repeat the number of times to repeat the function call; must be positive * @param runnable the function to call; may not be null */ default public void time(int repeat, Runnable runnable) { for (int i = 0; i != repeat; ++i) { Stopwatch sw = create().start(); try { runnable.run(); } finally { sw.stop(); } } }
public static void reset() { sw = Stopwatch.accumulating(); sws = Stopwatch.multiple(); }
/** * Returns a string representation of the maximum of all recorded durations. * * @return the string representation of the maximum duration; never null but possibly {@link Duration#ZERO}. */ default public String getMaximumAsString() { return asString(getMaximum()); }
Stopwatch sw = Stopwatch.reusable().start(); CountDownLatch latch = new CountDownLatch(1); Runnable runner = () -> { sw.stop(); return; Testing.print("Waited a total of " + sw.durations().statistics().getTotalAsString() + " for the replica to catch up to the master."); } catch (InterruptedException e) { Thread.interrupted();
protected static <T> Statistics time(String desc, int repeat, Callable<T> runnable, Consumer<T> cleanup) throws InterruptedException { sw.start(); try { sws.time(repeat, runnable, result -> { if (cleanup != null) cleanup.accept(result); }); } catch (Throwable t) { t.printStackTrace(); fail(t.getMessage()); } sw.stop(); // if (desc != null) Testing.print(60, "Time to " + desc + ":", sw.durations().statistics().getTotalAsString()); // Testing.print(60,"Total clock time:",sw.durations().statistics().getTotalAsString()); // Testing.print(54,"Time to invoke the functions:",sws); return sw.durations().statistics(); }
@Override public Stopwatch create() { return createWith(durations, latch::countUp, latch::countDown); }
public static Statistics completionTime() { return sw.durations().statistics(); }
@Test @SkipLongRunning public void shouldStartClusterAndAllowProducersAndConsumersToUseIt() throws Exception { Testing.Debug.enable(); final String topicName = "topicA"; final CountDownLatch completion = new CountDownLatch(2); final int numMessages = 100; final AtomicLong messagesRead = new AtomicLong(0); // Start a cluster and create a topic ... cluster.addBrokers(1).startup(); cluster.createTopics(topicName); // Consume messages asynchronously ... Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); return true; }); // Produce some messages asynchronously ... cluster.useTo().produceIntegers(topicName, numMessages, 1, completion::countDown); // Wait for both to complete ... if (completion.await(10, TimeUnit.SECONDS)) { sw.stop(); Testing.debug("Both consumer and producer completed normally in " + sw.durations()); } else { Testing.debug("Consumer and/or producer did not completed normally"); } assertThat(messagesRead.get()).isEqualTo(numMessages); }
/** * Create a new {@link Stopwatch} that records all of the measured durations of the stopwatch. * <p> * For example, the following code shows this behavior: * * <pre> * Stopwatch sw = Stopwatch.accumulating(); * sw.start(); * sleep(3000); // sleep 3 seconds * sw.stop(); * print(sw.durations()); // total and average duration are each 3 seconds * * sw.start(); * sleep(2000); // sleep 2 seconds * sw.stop(); * print(sw.durations()); // total duration is now 5 seconds, average is 2.5 seconds * </pre> * * @return the new stopwatch; never null */ public static Stopwatch accumulating() { return createWith(new MultipleDurations(), null, null); }
Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); sw.stop(); Testing.debug("The consumer completed normally in " + sw.durations()); } else { Testing.debug("Consumer did not completed normally");
/** * Time the given function. * * @param runnable the function that is to be executed; may not be null * @return the result of the operation */ default public <T> T time(Callable<T> runnable) { Stopwatch sw = create().start(); try { return runnable.call(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } finally { sw.stop(); } }
/** * Returns a string representation of the arithmetic mean of all recorded durations. * * @return the string representation of the average duration; never null but possibly {@link Duration#ZERO}. */ default public String getAverageAsString() { return asString(getAverage()); } }
/** * Create a new {@link Stopwatch} that can be reused. The resulting {@link Stopwatch#durations()}, however, * only reflect the most recently completed stopwatch interval. * <p> * For example, the following code shows this behavior: * * <pre> * Stopwatch sw = Stopwatch.reusable(); * sw.start(); * sleep(3000); // sleep 3 seconds * sw.stop(); * print(sw.durations()); // total and average duration are each 3 seconds * * sw.start(); * sleep(2000); // sleep 2 seconds * sw.stop(); * print(sw.durations()); // total and average duration are each 2 seconds * </pre> * * @return the new stopwatch; never null */ public static Stopwatch reusable() { return createWith(new SingleDuration(), null, null); }
Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); sw.stop(); Testing.debug("The consumer completed normally in " + sw.durations()); } else { Testing.debug("Consumer did not completed normally");
/** * Time the given function multiple times. * * @param repeat the number of times to repeat the function call; must be positive * @param runnable the function that is to be executed a number of times; may not be null * @param cleanup the function that is to be called after each time call to the runnable function, and not included * in the time measurements; may be null * @throws Exception the exception thrown by the runnable function */ default public <T> void time(int repeat, Callable<T> runnable, Consumer<T> cleanup) throws Exception { for (int i = 0; i != repeat; ++i) { T result = null; Stopwatch sw = create().start(); try { result = runnable.call(); } finally { sw.stop(); if (cleanup != null) { cleanup.accept(result); } } } }
/** * Returns a string representation of the total of all recorded durations. * * @return the string representation of the total duration; never null but possibly {@link Duration#ZERO}. */ default public String getTotalAsString() { return asString(getTotal()); }
@Override public Stopwatch create() { return createWith(durations, latch::countUp, latch::countDown); }
Stopwatch sw = Stopwatch.reusable().start(); CountDownLatch latch = new CountDownLatch(1); Runnable runner = () -> { sw.stop(); return; Testing.print("Waited a total of " + sw.durations().statistics().getTotalAsString() + " for the replica to catch up to the master."); } catch (InterruptedException e) { Thread.interrupted();
/** * Time the given function multiple times. * * @param repeat the number of times to repeat the function call; must be positive * @param runnable the function to call; may not be null */ default public void time(int repeat, Runnable runnable) { for (int i = 0; i != repeat; ++i) { Stopwatch sw = create().start(); try { runnable.run(); } finally { sw.stop(); } } }
/** * Returns a string representation of the minimum of all recorded durations. * * @return the string representation of the minimum duration; never null but possibly {@link Duration#ZERO}. */ default public String getMinimumAsString() { return asString(getMinimum()); }