/** * Tests size of empty and populated {@link ArrayList} object with initial size of 0. */ @Test(invocationCount = 50) public void arrayListInitializeZero() { ArrayList<Object> arrayList = new ArrayList<>(0); long ourSize = objectSizes.getSizeOf(arrayList, 0); long theirSize = MemoryUtil.deepMemoryUsageOf(arrayList, VisibilityFilter.ALL); assertThat("Empty list", ourSize, is(equalTo(theirSize))); int size = (int) (Math.random() * 100); for (int i = 0; i < size; i++) { arrayList.add(new Object()); } if (checkObjectGraphIdentityHashCodeCollision(arrayList, new HashSet<Integer>(), new ArrayList<Integer>())) { LOGGER.info("Object graph identity collision check passed for test: arrayListInitializeZero()"); ourSize = objectSizes.getSizeOf(arrayList, 0) + (size * objectSizes.getSizeOfObjectObject()); theirSize = MemoryUtil.deepMemoryUsageOf(arrayList, VisibilityFilter.ALL); assertThat("Random list size", ourSize, is(equalTo(theirSize))); } }
/** * General test for arrays of different sizes. */ @Test(invocationCount = 50) public void array() { Object[] array = new Object[(int) (Math.random() * 100)]; long ourSize = objectSizes.getSizeOfArray(array.length); long theirSize = MemoryUtil.deepMemoryUsageOf(array, VisibilityFilter.ALL); assertThat("Empty array", ourSize, is(equalTo(theirSize))); for (int i = 0; i < array.length; i++) { array[i] = new Object(); } if (checkObjectGraphIdentityHashCodeCollision(array, new HashSet<Integer>(), new ArrayList<Integer>())) { LOGGER.info("Object graph identity collision check passed for test: array()"); ourSize = objectSizes.getSizeOfArray(array.length) + (array.length * objectSizes.getSizeOfObjectObject()); theirSize = MemoryUtil.deepMemoryUsageOf(array, VisibilityFilter.ALL); assertThat("Full array of " + array.length + " elements", ourSize, is(equalTo(theirSize))); } }
/** * Tests size of empty and populated {@link ArrayList} object. */ @Test(invocationCount = 50) public void arrayList() { ArrayList<Object> arrayList = new ArrayList<>(); long ourSize = objectSizes.getSizeOf(arrayList); long theirSize = MemoryUtil.deepMemoryUsageOf(arrayList, VisibilityFilter.ALL); assertThat("Empty list", ourSize, is(equalTo(theirSize))); int size = (int) (Math.random() * 100); for (int i = 0; i < size; i++) { arrayList.add(new Object()); } if (checkObjectGraphIdentityHashCodeCollision(arrayList, new HashSet<Integer>(), new ArrayList<Integer>())) { LOGGER.info("Object graph identity collision check passed for test: arrayList()"); ourSize = objectSizes.getSizeOf(arrayList) + (size * objectSizes.getSizeOfObjectObject()); theirSize = MemoryUtil.deepMemoryUsageOf(arrayList, VisibilityFilter.ALL); assertThat("Random list size", ourSize, is(equalTo(theirSize))); } }
/** * Tests size of empty and populated {@link ConcurrentHashMap} object. Note that populated * {@link ConcurrentHashMap} is being tested with only 1 segment. It is impossible to provide * the correct size with more segments, because it is unknown what will be the distributon of * elements between segments. */ @Test(invocationCount = 50) public void concurrentHashMap() { // we can only precisely calculate random amount of elements with one segment int concurrencyLevel = 1; ConcurrentHashMap<Object, Object> concurrentHashMap = new ConcurrentHashMap<>(16, 0.75f, concurrencyLevel); long theirSize = MemoryUtil.deepMemoryUsageOf(concurrentHashMap, VisibilityFilter.ALL); long ourSize = objectSizes.getSizeOfConcurrentHashMap(0); assertThat("Empty map", ourSize, is(equalTo(theirSize))); concurrentHashMap = new ConcurrentHashMap<>(16, 0.75f, 2); int size = (int) (Math.random() * 100); for (int i = 0; i < size; i++) { concurrentHashMap.put(new Object(), new Object()); } if (checkObjectGraphIdentityHashCodeCollision(concurrentHashMap, new HashSet<Integer>(), new ArrayList<Integer>())) { LOGGER.info("Object graph identity collision check passed for test: concurrentHashMap()"); theirSize = MemoryUtil.deepMemoryUsageOf(concurrentHashMap, VisibilityFilter.ALL); ourSize = objectSizes.getSizeOfConcurrentHashMap(size) + (size * 2 * objectSizes.getSizeOfObjectObject()); assertThat("Random map size of " + size, ourSize, is(equalTo(theirSize))); } }
if (checkObjectGraphIdentityHashCodeCollision(hashMap, new HashSet<Integer>(), new ArrayList<Integer>())) { LOGGER.info("Object graph identity collision check passed for test: hashMap()"); ourSize = objectSizes.getSizeOfHashMap(hashMap.size()) + (2 * size * objectSizes.getSizeOfObjectObject());