Search in sources :

Example 1 with ReflectionSingleObjectSizer

use of org.apache.geode.internal.size.ReflectionSingleObjectSizer in project geode by apache.

the class MemLRUEvictionControllerDUnitTest method testRegionOperations.

// ////// Test Methods
/**
   * Carefully verifies that region operations effect the {@link LRUStatistics} as expected.
   */
@Test
public void testRegionOperations() throws CacheException {
    int threshold = 4;
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(threshold));
    Region region;
    if (usingMain) {
        DistributedSystem system = DistributedSystem.connect(new Properties());
        Cache cache = CacheFactory.create(system);
        region = cache.createRegion("Test", factory.create());
    } else {
        region = createRegion(name, factory.create());
    }
    LRUStatistics lruStats = getLRUStats(region);
    assertNotNull(lruStats);
    String sampleKey = new String("10000");
    int stringSize = // String object
    SharedLibrary.getObjectHeaderSize() + (2 * 4) + // 2 ints and a reference on a string
    SharedLibrary.getReferenceSize();
    stringSize = (int) ReflectionSingleObjectSizer.roundUpSize(stringSize);
    int charArraySize = // char array
    sampleKey.length() * 2 + SharedLibrary.getObjectHeaderSize() + // object
    4;
    // length of char array
    charArraySize = (int) ReflectionSingleObjectSizer.roundUpSize(charArraySize);
    assertEquals(stringSize, ReflectionSingleObjectSizer.sizeof(String.class));
    assertEquals(ReflectionSingleObjectSizer.roundUpSize(SharedLibrary.getObjectHeaderSize() + 4), (new ReflectionSingleObjectSizer()).sizeof(new char[0]));
    assertEquals(charArraySize, (new ReflectionSingleObjectSizer()).sizeof(new char[5]));
    int keySize = stringSize + charArraySize;
    assertEquals(keySize, WellKnownClassSizer.sizeof(sampleKey));
    assertEquals(keySize, ObjectSizer.DEFAULT.sizeof(sampleKey));
    // now that keys are inlined the keySize is 0
    keySize = 0;
    byte[] sampleValue = new byte[1000];
    int valueSize = // byte array object;
    sampleValue.length + SharedLibrary.getObjectHeaderSize() + // length of byte array
    4;
    valueSize = (int) ReflectionSingleObjectSizer.roundUpSize(valueSize);
    int entrySize = keySize + valueSize + getEntryOverhead(region);
    assertEquals(valueSize, ObjectSizer.DEFAULT.sizeof(sampleValue));
    for (int i = 1; i <= 100; i++) {
        Object key = String.valueOf(10000 + i);
        Object value = new byte[1000];
        region.put(key, value);
        assertEquals(i * entrySize, lruStats.getCounter());
        assertEquals(0, lruStats.getEvictions());
    }
    for (int i = 100; i >= 1; i--) {
        Object key = String.valueOf(10000 + i);
        region.destroy(key);
        assertEquals((i - 1) * entrySize, lruStats.getCounter());
        assertEquals(0, lruStats.getEvictions());
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) ReflectionSingleObjectSizer(org.apache.geode.internal.size.ReflectionSingleObjectSizer) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

Properties (java.util.Properties)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 DistributedSystem (org.apache.geode.distributed.DistributedSystem)1 LocalRegion (org.apache.geode.internal.cache.LocalRegion)1 LRUStatistics (org.apache.geode.internal.cache.lru.LRUStatistics)1 ReflectionSingleObjectSizer (org.apache.geode.internal.size.ReflectionSingleObjectSizer)1 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)1 Test (org.junit.Test)1