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());
}
}
Aggregations