From 83bc46ee228db8c44a5b36e37c676a064fc472a5 Mon Sep 17 00:00:00 2001 From: Allan Vital Date: Tue, 13 Feb 2018 23:13:37 -0200 Subject: [PATCH] BAEL-1368 Infinispan Guide (#3651) * BAEL-1368: infinispan article * BAEL-1368: new method timing method --- .../infinispan/ConfigurationTest.java | 15 ++- .../service/HelloWorldServiceUnitTest.java | 94 ++++++------------- .../service/TransactionalServiceUnitTest.java | 6 +- 3 files changed, 46 insertions(+), 69 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java index b05314491b..906a887e1a 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -9,13 +9,15 @@ import org.infinispan.manager.DefaultCacheManager; import org.junit.After; import org.junit.Before; +import java.util.concurrent.Callable; + public class ConfigurationTest { private DefaultCacheManager cacheManager; private HelloWorldRepository repository = new HelloWorldRepository(); - protected HelloWorldService helloWorldService; + protected HelloWorldService helloWorldService; protected TransactionalService transactionalService; @Before @@ -53,4 +55,15 @@ public class ConfigurationTest { cacheManager.stop(); } + protected long timeThis(Callable callable) { + try { + long milis = System.currentTimeMillis(); + callable.call(); + return System.currentTimeMillis() - milis; + } catch (Exception e) { + e.printStackTrace(); + } + return 0l; + } + } diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java index bda2e6886b..c9ecd57995 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java @@ -3,103 +3,69 @@ package com.baeldung.infinispan.service; import com.baeldung.infinispan.ConfigurationTest; import org.junit.Test; +import java.util.concurrent.Callable; +import java.util.function.Consumer; + import static org.assertj.core.api.Java6Assertions.assertThat; public class HelloWorldServiceUnitTest extends ConfigurationTest { @Test public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { - long milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorld(); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorld(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isLessThan(100); } @Test public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { - long milis = System.currentTimeMillis(); - helloWorldService.findExpiringHelloWorld(); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findExpiringHelloWorld(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isLessThan(100); } @Test public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() - throws InterruptedException { + throws InterruptedException { - long milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorldInExpiringCache(); - long executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); Thread.sleep(1100); - milis = System.currentTimeMillis(); - helloWorldService.findSimpleHelloWorldInExpiringCache(); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); } @Test - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() - throws InterruptedException { + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { - long milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 1"); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); - milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 2"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findEvictingHelloWorld("key 1"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); } @Test - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() - throws InterruptedException { + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { - long milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 1"); - long executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); - milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 2"); - executionTime = System.currentTimeMillis() - milis; + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isLessThan(100); - assertThat(executionTime).isGreaterThanOrEqualTo(1000); - - milis = System.currentTimeMillis(); - helloWorldService.findPassivatingHelloWorld("key 1"); - executionTime = System.currentTimeMillis() - milis; - - assertThat(executionTime).isLessThan(100); } } diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java index f529222079..99efacd18a 100644 --- a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java +++ b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java @@ -14,11 +14,9 @@ public class TransactionalServiceUnitTest extends ConfigurationTest { transactionalService.getQuickHowManyVisits(); backgroundThread.start(); Thread.sleep(100); //lets wait our thread warm up - long milis = System.currentTimeMillis(); - transactionalService.getQuickHowManyVisits(); - long executionTime = System.currentTimeMillis() - milis; - assertThat(executionTime).isGreaterThan(500).isLessThan(1000); + assertThat(timeThis(() -> transactionalService.getQuickHowManyVisits())) + .isGreaterThan(500).isLessThan(1000); } }