/** * 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(); } } }
/** * 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(); } }
/** * 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); } } } }
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(); }
Stopwatch sw = Stopwatch.reusable().start(); CountDownLatch latch = new CountDownLatch(1); Runnable runner = () -> {
@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); }
Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet();
Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet();
/** * 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(); } }
/** * 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(); } } }
/** * 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); } } } }
Stopwatch sw = Stopwatch.reusable().start(); CountDownLatch latch = new CountDownLatch(1); Runnable runner = () -> {