BAEL-1368 Infinispan Guide (#3651)

* BAEL-1368: infinispan article

* BAEL-1368: new method timing method
This commit is contained in:
Allan Vital 2018-02-13 23:13:37 -02:00 committed by Predrag Maric
parent bcc3b6ed95
commit 83bc46ee22
3 changed files with 46 additions and 69 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}