BAEL-1368 Infinispan Guide (#3651)
* BAEL-1368: infinispan article * BAEL-1368: new method timing method
This commit is contained in:
		
							parent
							
								
									bcc3b6ed95
								
							
						
					
					
						commit
						83bc46ee22
					
				| @ -9,6 +9,8 @@ import org.infinispan.manager.DefaultCacheManager; | |||||||
| import org.junit.After; | import org.junit.After; | ||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| 
 | 
 | ||||||
|  | import java.util.concurrent.Callable; | ||||||
|  | 
 | ||||||
| public class ConfigurationTest { | public class ConfigurationTest { | ||||||
| 
 | 
 | ||||||
|     private DefaultCacheManager cacheManager; |     private DefaultCacheManager cacheManager; | ||||||
| @ -53,4 +55,15 @@ public class ConfigurationTest { | |||||||
|         cacheManager.stop(); |         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; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -3,103 +3,69 @@ package com.baeldung.infinispan.service; | |||||||
| import com.baeldung.infinispan.ConfigurationTest; | import com.baeldung.infinispan.ConfigurationTest; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
|  | import java.util.concurrent.Callable; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  | 
 | ||||||
| import static org.assertj.core.api.Java6Assertions.assertThat; | import static org.assertj.core.api.Java6Assertions.assertThat; | ||||||
| 
 | 
 | ||||||
| public class HelloWorldServiceUnitTest extends ConfigurationTest { | public class HelloWorldServiceUnitTest extends ConfigurationTest { | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { |     public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { | ||||||
|         long milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) | ||||||
|         helloWorldService.findSimpleHelloWorld(); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         long executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 | 
 | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |         assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) | ||||||
| 
 |           .isLessThan(100); | ||||||
|         milis = System.currentTimeMillis(); |  | ||||||
|         helloWorldService.findSimpleHelloWorld(); |  | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isLessThan(100); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { |     public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { | ||||||
|         long milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) | ||||||
|         helloWorldService.findExpiringHelloWorld(); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         long executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 | 
 | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |         assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) | ||||||
| 
 |           .isLessThan(100); | ||||||
|         milis = System.currentTimeMillis(); |  | ||||||
|         helloWorldService.findExpiringHelloWorld(); |  | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isLessThan(100); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() |     public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() | ||||||
|             throws InterruptedException { |             throws InterruptedException { | ||||||
| 
 | 
 | ||||||
|         long milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) | ||||||
|         helloWorldService.findSimpleHelloWorldInExpiringCache(); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         long executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |  | ||||||
| 
 | 
 | ||||||
|         Thread.sleep(1100); |         Thread.sleep(1100); | ||||||
| 
 | 
 | ||||||
|         milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) | ||||||
|         helloWorldService.findSimpleHelloWorldInExpiringCache(); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() |     public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { | ||||||
|           throws InterruptedException { |  | ||||||
| 
 | 
 | ||||||
|         long milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) | ||||||
|         helloWorldService.findEvictingHelloWorld("key 1"); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         long executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 | 
 | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |         assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))) | ||||||
|  |           .isGreaterThanOrEqualTo(1000); | ||||||
| 
 | 
 | ||||||
|         milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) | ||||||
|         helloWorldService.findEvictingHelloWorld("key 2"); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |  | ||||||
| 
 |  | ||||||
|         milis = System.currentTimeMillis(); |  | ||||||
|         helloWorldService.findEvictingHelloWorld("key 1"); |  | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() |     public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { | ||||||
|           throws InterruptedException { |  | ||||||
| 
 | 
 | ||||||
|         long milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) | ||||||
|         helloWorldService.findPassivatingHelloWorld("key 1"); |           .isGreaterThanOrEqualTo(1000); | ||||||
|         long executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 | 
 | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |         assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))) | ||||||
|  |           .isGreaterThanOrEqualTo(1000); | ||||||
| 
 | 
 | ||||||
|         milis = System.currentTimeMillis(); |         assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) | ||||||
|         helloWorldService.findPassivatingHelloWorld("key 2"); |           .isLessThan(100); | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 | 
 | ||||||
|         assertThat(executionTime).isGreaterThanOrEqualTo(1000); |  | ||||||
| 
 |  | ||||||
|         milis = System.currentTimeMillis(); |  | ||||||
|         helloWorldService.findPassivatingHelloWorld("key 1"); |  | ||||||
|         executionTime = System.currentTimeMillis() - milis; |  | ||||||
| 
 |  | ||||||
|         assertThat(executionTime).isLessThan(100); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,11 +14,9 @@ public class TransactionalServiceUnitTest extends ConfigurationTest { | |||||||
|         transactionalService.getQuickHowManyVisits(); |         transactionalService.getQuickHowManyVisits(); | ||||||
|         backgroundThread.start(); |         backgroundThread.start(); | ||||||
|         Thread.sleep(100); //lets wait our thread warm up |         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); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user