alfresco-community-repo/source/test-java/org/alfresco/repo/cache/InMemoryCacheStatisticsTest.java
Matt Ward d9c11a7ca7 Merged BRANCHES/DEV/mward/head_cachestats to HEAD:
89575: ACE-3327: Work In Progress. TX cache statistics.
   89642: ACE-3327: Exposes statistics through JMX. Adds hit ratio.
   89649: ACE-3327: Added "@since 5.0" to new classes.
   89691: ACE-3327: improved TransactionalCache stats tests.
   89743: ACE-3327: fixed lack of thread safety for InMemoryCacheStatistics.
   89752: ACE-3327: now possible to disable/enable tx cache statistics per-cache with properties.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@89798 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2014-11-03 18:08:50 +00:00

146 lines
5.0 KiB
Java

package org.alfresco.repo.cache;
import static org.junit.Assert.*;
import org.alfresco.repo.cache.TransactionStats.OpType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
/**
* Tests for the {@link InMemoryCacheStatistics} class.
*
* @since 5.0
* @author Matt Ward
*/
@RunWith(MockitoJUnitRunner.class)
public class InMemoryCacheStatisticsTest
{
InMemoryCacheStatistics cacheStats;
@Mock ApplicationContext appCtx;
@Before
public void setUp() throws Exception
{
cacheStats = new InMemoryCacheStatistics();
cacheStats.setApplicationContext(appCtx);
}
@Test
public void readOperationsThrowNoCacheStatsException()
{
try
{
cacheStats.count("cache1", OpType.GET_HIT);
fail("NoStatsForCache should have been thrown.");
}
catch(NoStatsForCache e)
{
// Good.
}
try
{
cacheStats.hitMissRatio("cache1");
fail("NoStatsForCache should have been thrown.");
}
catch(NoStatsForCache e)
{
// Good.
}
try
{
cacheStats.meanTime("cache1", OpType.GET_HIT);
fail("NoStatsForCache should have been thrown.");
}
catch(NoStatsForCache e)
{
// Good.
}
}
@Test
public void canAccumulateStatisticsPerCache()
{
TransactionStats txStats = new TransactionStats();
txStats.record(0, 1000, OpType.GET_HIT);
txStats.record(0, 2000, OpType.GET_HIT);
txStats.record(0, 3000, OpType.GET_HIT);
// Add first statistical datapoint.
cacheStats.add("cache1", txStats);
Mockito.verify(appCtx).publishEvent(Mockito.any(CacheStatisticsCreated.class));
// Cache stats should be visible
assertEquals(3, cacheStats.count("cache1", OpType.GET_HIT));
assertEquals(2000, cacheStats.meanTime("cache1", OpType.GET_HIT), 0.0d);
// A new transaction
txStats = new TransactionStats();
txStats.record(0, 4000, OpType.GET_HIT);
txStats.record(0, 5000, OpType.GET_HIT);
// Central cache stats should not yet include new transaction's stats.
assertEquals(3, cacheStats.count("cache1", OpType.GET_HIT));
cacheStats.add("cache1", txStats);
// New TX's stats should now be visible
assertEquals(5, cacheStats.count("cache1", OpType.GET_HIT));
assertEquals(3000, cacheStats.meanTime("cache1", OpType.GET_HIT), 0.0d);
// A different cache
try
{
cacheStats.count("cache2", OpType.GET_HIT);
fail("Expected NoStatsForCache error.");
}
catch(NoStatsForCache e)
{
// Good
}
txStats = new TransactionStats();
txStats.record(0, 4000, OpType.GET_HIT);
txStats.record(8000, 9000, OpType.GET_HIT);
cacheStats.add("cache2", txStats);
assertEquals(2, cacheStats.count("cache2", OpType.GET_HIT));
assertEquals(2500, cacheStats.meanTime("cache2", OpType.GET_HIT), 0.0d);
// cache2 should NOT affect cache1
assertEquals(5, cacheStats.count("cache1", OpType.GET_HIT));
assertEquals(3000, cacheStats.meanTime("cache1", OpType.GET_HIT), 0.0d);
// Some non-hit statistics
txStats = new TransactionStats();
txStats.record(0, 810, OpType.GET_MISS);
txStats.record(1000, 1820, OpType.PUT);
txStats.record(3000, 3830, OpType.REMOVE);
txStats.record(4000, 4840, OpType.CLEAR);
cacheStats.add("cache1", txStats);
// Hits haven't changed
assertEquals(5, cacheStats.count("cache1", OpType.GET_HIT));
assertEquals(3000, cacheStats.meanTime("cache1", OpType.GET_HIT), 0.0d);
// Other stats have changed
assertEquals(1, cacheStats.count("cache1", OpType.GET_MISS));
assertEquals(810, cacheStats.meanTime("cache1", OpType.GET_MISS), 0.01d);
assertEquals(1, cacheStats.count("cache1", OpType.PUT));
assertEquals(820, cacheStats.meanTime("cache1", OpType.PUT), 0.01d);
assertEquals(1, cacheStats.count("cache1", OpType.REMOVE));
assertEquals(830, cacheStats.meanTime("cache1", OpType.REMOVE), 0.01d);
assertEquals(1, cacheStats.count("cache1", OpType.CLEAR));
assertEquals(840, cacheStats.meanTime("cache1", OpType.CLEAR), 0.01d);
// Check hit ratio
assertEquals(5, cacheStats.count("cache1", OpType.GET_HIT));
assertEquals(1, cacheStats.count("cache1", OpType.GET_MISS));
assertEquals(0.83, cacheStats.hitMissRatio("cache1"), 0.01d);
}
}