summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorgjoranv <gv@oath.com>2018-05-04 11:19:52 +0200
committergjoranv <gv@oath.com>2018-05-04 11:25:56 +0200
commitdd71cb045baf5f23b29f2d6adf1d18b7cb24817f (patch)
tree54bb352e07991c0a86ad42ff5c3cf41ce9f8e5b7 /vespajlib
parent489478804f543762078494e235f31ace181950e9 (diff)
Remove CachingSearcher and its Cache with SizeCalculator
- The SizeCalculator uses recursive reflective calls in the object tree, and attempts illegal access to e.g. java.lang.module classes on Java 9+. With this access explicitly allowed via "--add-opens" the calculator returns a much larger size than the actual size of the object.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/cache/Cache.java276
-rw-r--r--vespajlib/src/main/java/com/yahoo/cache/SizeCalculator.java175
-rw-r--r--vespajlib/src/test/java/com/yahoo/cache/CacheTestCase.java197
-rw-r--r--vespajlib/src/test/java/com/yahoo/cache/CalcTestCase.java178
4 files changed, 0 insertions, 826 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/cache/Cache.java b/vespajlib/src/main/java/com/yahoo/cache/Cache.java
deleted file mode 100644
index af3bb7db21b..00000000000
--- a/vespajlib/src/main/java/com/yahoo/cache/Cache.java
+++ /dev/null
@@ -1,276 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.cache;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * <p>A generic cache which keeps the total memory consumed by its content
- * below a configured maximum.</p>
- *
- * <p>Thread safe.</p>
- *
- * @author vegardh
- */
-public class Cache<K, V> {
- private Map<CacheKey<K>,CacheValue<K, V>> content=new LinkedHashMap<>(12500, 1.0f, true);
- private SizeCalculator calc = new SizeCalculator();
- private long maxSizeBytes;
-
- private long currentSizeBytes=0;
-
- /** The time an element is allowed to live, negative for indefinite lifespan */
- private long timeToLiveMillis=-1;
-
- /** The max allowed size of an entry, negative for no limit */
- private long maxEntrySizeBytes=10000;
-
- /**
- * Creates a new cache
- *
- * @param maxSizeBytes the max size in bytes this cache is permitted to consume,
- * including Result objects and Query keys
- * @param timeToLiveMillis a negative value means unlimited time
- * @param maxEntrySizeBytes never cache objects bigger than this, negative for no such limit
- */
- public Cache(long maxSizeBytes,long timeToLiveMillis, long maxEntrySizeBytes) {
- this.maxSizeBytes=maxSizeBytes;
- this.timeToLiveMillis=timeToLiveMillis;
- this.maxEntrySizeBytes=maxEntrySizeBytes;
- }
-
- private synchronized CacheValue<K, V> synchGet(CacheKey<K> k) {
- return content.get(k);
- }
-
- private synchronized boolean synchPut(K key,V value, long keySizeBytes, long valueSizeBytes) {
- // log.info("Put "+key.toString()+ " key size:"+keySizeBytes+" val size:"+valueSizeBytes);
- if ((valueSizeBytes+keySizeBytes)>maxSizeBytes) {
- return false;
- }
- makeRoomForBytes(valueSizeBytes+keySizeBytes);
- CacheKey<K> cacheKey = new CacheKey<>(keySizeBytes, key);
- CacheValue<K, V> cacheValue;
- if (timeToLiveMillis<0) {
- cacheValue=new CacheValue<>(valueSizeBytes,value, cacheKey);
- } else {
- cacheValue=new AgingCacheValue<>(valueSizeBytes,value, cacheKey);
- }
- currentSizeBytes+=(valueSizeBytes+keySizeBytes);
- content.put(cacheKey, cacheValue);
- return true;
- }
-
- /**
- * Attempts to add a value to the cache
- *
- * @param key the key of the value
- * @param value the value to add
- * @return true if the value was added, false if it could not be added
- */
- public boolean put(K key,V value) {
- long keySizeBytes=calc.sizeOf(key);
- long valueSizeBytes=calc.sizeOf(value);
- if (tooBigToCache(keySizeBytes+valueSizeBytes)) {
- return false;
- }
- return synchPut(key, value, keySizeBytes, valueSizeBytes);
- }
-
- /**
- * Don't cache elems that are too big, even if there's space
- * @return true if the argument is too big to cache.
- */
- private boolean tooBigToCache(long totalSize) {
- if (maxEntrySizeBytes<0) {
- return false;
- }
- if (totalSize > maxEntrySizeBytes) {
- return true;
- }
- return false;
- }
-
- private void makeRoomForBytes(long bytes) {
- if ((maxSizeBytes-currentSizeBytes) > bytes) {
- return;
- }
- if (content.isEmpty()) {
- return;
- }
- for (Iterator<Map.Entry<CacheKey<K>, CacheValue<K, V>>> i = content.entrySet().iterator() ; i.hasNext() ; ) {
- Map.Entry<CacheKey<K>, CacheValue<K, V>> entry = i.next();
- CacheKey<K> key = entry.getKey();
- CacheValue<K, V> value = entry.getValue();
- // Can't call this.remove(), breaks iterator.
- i.remove(); // Access order: first ones are LRU.
- currentSizeBytes-=key.sizeBytes();
- currentSizeBytes-=value.sizeBytes();
- if ((maxSizeBytes-currentSizeBytes) > bytes) {
- break;
- }
- }
- }
-
- public boolean containsKey(K k) {
- return content.containsKey(new CacheKey<>(-1, k));
- }
-
- /** Returns a value, if it is present in the cache */
- public V get(K key) {
- // Currently it works to make a new CacheKey object without size
- // because we have changed hashCode() there.
- CacheKey<K> cacheKey = new CacheKey<>(-1, key);
- CacheValue<K, V> value=synchGet(cacheKey);
- if (value==null) {
- return null;
- }
- if (timeToLiveMillis<0) {
- return value.value();
- }
-
- if (value.expired(timeToLiveMillis)) {
- // There was a value, which has now expired
- remove(key);
- return null;
- } else {
- return value.value();
- }
- }
-
- /**
- * Removes a cache value if present
- *
- * @return true if the value was removed, false if it was not present
- */
- public synchronized boolean remove(K key) {
- CacheValue<K, V> value=content.remove(key);
- if (value==null) {
- return false;
- }
- currentSizeBytes-=value.sizeBytes();
- currentSizeBytes-=value.getKey().sizeBytes();
- return true;
- }
-
- public long getTimeToLiveMillis() {
- return timeToLiveMillis;
- }
-
- public int size() {
- return content.size();
- }
-
- private static class CacheKey<K> {
- private long sizeBytes;
- private K key;
- public CacheKey(long sizeBytes,K key) {
- this.sizeBytes=sizeBytes;
- this.key=key;
- }
-
- public long sizeBytes() {
- return sizeBytes;
- }
-
- public K getKey() {
- return key;
- }
-
- public int hashCode() {
- return key.hashCode();
- }
-
- @SuppressWarnings("rawtypes")
- public boolean equals(Object k) {
- if (key==null) {
- return false;
- }
- if (k==null) {
- return false;
- }
- if (k instanceof CacheKey) {
- return key.equals(((CacheKey)k).getKey());
- }
- return false;
- }
- }
-
- private static class CacheValue<K, V> {
- private long sizeBytes;
- private V value;
- private CacheKey<K> key;
- public CacheValue(long sizeBytes, V value, CacheKey<K> key) {
- this.sizeBytes=sizeBytes;
- this.value=value;
- this.key = key;
- }
-
- public boolean expired(long ttl) {
- return false;
- }
-
- public V value() {
- return value;
- }
-
- public long sizeBytes() {
- return sizeBytes;
- }
-
- public CacheKey<K> getKey() {
- return key;
- }
- }
-
- private static class AgingCacheValue<K, V> extends CacheValue<K, V> {
- private long birthTimeMillis;
-
- public AgingCacheValue(long sizeBytes,V value, CacheKey<K> key) {
- super(sizeBytes,value, key);
- this.birthTimeMillis=System.currentTimeMillis();
- }
-
- public long ageMillis() {
- return System.currentTimeMillis()-birthTimeMillis;
- }
-
- public boolean expired(long ttl) {
- return (ageMillis() >= ttl);
- }
- }
-
- /**
- * Empties the cache
- */
- public synchronized void clear() {
- content.clear();
- currentSizeBytes=0;
- }
-
- /**
- * Collection of keys.
- */
- public Collection<K> getKeys() {
- Collection<K> ret = new ArrayList<>();
- for (Iterator<CacheKey<K>> i = content.keySet().iterator(); i.hasNext();) {
- ret.add(i.next().getKey());
- }
- return ret;
- }
-
- /**
- * Collection of values.
- */
- public Collection<V> getValues() {
- Collection<V> ret = new ArrayList<>();
- for (Iterator<CacheValue<K, V>> i = content.values().iterator(); i.hasNext();) {
- ret.add(i.next().value());
- }
- return ret;
- }
-
-}
diff --git a/vespajlib/src/main/java/com/yahoo/cache/SizeCalculator.java b/vespajlib/src/main/java/com/yahoo/cache/SizeCalculator.java
deleted file mode 100644
index e062ad8783f..00000000000
--- a/vespajlib/src/main/java/com/yahoo/cache/SizeCalculator.java
+++ /dev/null
@@ -1,175 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.cache;
-
-import java.lang.reflect.Array;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.IdentityHashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Size calculator for objects.
- * Thread safe.
- * @author vegardh
- * @see <a href="http://www.javaspecialists.co.za/archive/Issue078.html">MemoryCounter by Dr H M Kabutz</a>
- */
-public class SizeCalculator {
-
- private static class ObjectSet {
- private final Map<Object, Object> map = new IdentityHashMap<>();
-
- public boolean had(Object obj) {
- if (map.containsKey(obj)) {
- return true;
- }
- map.put(obj, null);
- return false;
- }
- }
-
- private int getPointerSize() {
- return 4;
- }
-
- private int getClassSize() {
- return 8;
- }
-
- private int getArraySize() {
- return 16;
- }
-
- @SuppressWarnings("serial")
- private final IdentityHashMap<Class<?>, Integer> primitiveSizes = new IdentityHashMap<Class<?>, Integer>() {
- {
- put(boolean.class, 1);
- put(byte.class, 1);
- put(char.class, 2);
- put(short.class, 2);
- put(int.class, 4);
- put(float.class, 4);
- put(double.class, 8);
- put(long.class, 8);
- }
- };
-
- // Only called on un-visited objects and only with array.
- private long sizeOfArray(Object a, ObjectSet visitedObjects) {
- long sum = getArraySize();
- int length = Array.getLength(a);
- if (length == 0) {
- return sum;
- }
- Class<?> elementClass = a.getClass().getComponentType();
- if (elementClass.isPrimitive()) {
- sum += length * (primitiveSizes.get(elementClass));
- return sum;
- } else {
- for (int i = 0; i < length; i++) {
- Object val = Array.get(a, i);
- sum += getPointerSize();
- sum += sizeOfObject(val, visitedObjects);
- }
- return sum;
- }
- }
-
- private long getSumOfFields(Class<?> clas, Object obj,
- ObjectSet visitedObjects) {
- long sum = 0;
- Field[] fields = clas.getDeclaredFields();
- for (Field field : fields) {
- if (!Modifier.isStatic(field.getModifiers())) {
- if (field.getType().isPrimitive()) {
- sum += primitiveSizes.get(field.getType());
- } else {
- sum += getPointerSize();
- field.setAccessible(true);
- try {
- sum += sizeOfObject(field.get(obj), visitedObjects);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
- return sum;
- }
-
- // Skip literal strings
- private boolean isIntern(Object obj) {
- if (obj instanceof String) {
- if (obj == ((String) obj).intern()) {
- return true;
- }
- }
- return false;
- }
-
- // Only called on non-visited non-arrays.
- private long sizeOfNonArray(Class<?> clas, Object obj,
- ObjectSet visitedObjects) {
- if (isIntern(obj)) {
- return 0;
- }
- long sum = getClassSize();
- while (clas != null) {
- sum += getSumOfFields(clas, obj, visitedObjects);
- clas = clas.getSuperclass();
- }
- return sum;
- }
-
- private long sizeOfObject(Object obj, ObjectSet visitedObjects) {
- if (obj == null) {
- return 0;
- }
- if (visitedObjects.had(obj)) {
- return 0;
- }
- Class<?> clas = obj.getClass();
- if (clas.isArray()) {
- return sizeOfArray(obj, visitedObjects);
- }
- return sizeOfNonArray(clas, obj, visitedObjects);
- }
-
- /**
- * Returns the heap size of an object/array
- *
- * @return Number of bytes for object, approximately
- */
- public long sizeOf(Object value) {
- ObjectSet visitedObjects = new ObjectSet();
- return sizeOfObject(value, visitedObjects);
- }
-
- /**
- * Returns the heap size of two objects/arrays, common objects counted only
- * once
- *
- * @return Number of bytes for objects, approximately
- */
- public long sizeOf(Object value1, Object value2) {
- ObjectSet visitedObjects = new ObjectSet();
- return sizeOfObject(value1, visitedObjects)
- + sizeOfObject(value2, visitedObjects);
- }
-
- /**
- * The approximate size in bytes for a list of objects, viewed as a closure,
- * ie. common objects are counted only once.
- *
- * @return total number of bytes
- */
- public long sizeOf(List<?> objects) {
- ObjectSet visitedObjects = new ObjectSet();
- long sum = 0;
- for (Object o : objects) {
- sum += sizeOfObject(o, visitedObjects);
- }
- return sum;
- }
-
-}
diff --git a/vespajlib/src/test/java/com/yahoo/cache/CacheTestCase.java b/vespajlib/src/test/java/com/yahoo/cache/CacheTestCase.java
deleted file mode 100644
index 8f14016b542..00000000000
--- a/vespajlib/src/test/java/com/yahoo/cache/CacheTestCase.java
+++ /dev/null
@@ -1,197 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.cache;
-
-import junit.framework.TestCase;
-
-import java.util.ArrayList;
-import java.util.Collection;
-
-public class CacheTestCase extends TestCase {
-
- public void testBasicGet() {
- Cache<String, String> cache = new Cache<>(100 * 1024 * 1024, 3600, 10000);
- String q = "/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff";
- String q2 = "/std_xmls_a00?hits=5&offset=5&query=flowers+shop&tracelevel=4&objid=ffffffffffffffff";
- String r = "result";
- String r2 = "result2";
- assertNull(cache.get(q));
- cache.put(q, r);
- assertNotNull(cache.get(q));
- assertEquals(cache.get(q), r);
- cache.put(q2, r);
- assertEquals(cache.get(q2), r);
- cache.put(q, r2);
- assertEquals(cache.get(q), r2);
- }
-
- public void testPutTooLarge() {
- byte[] tenMB = new byte[10*1024*1024];
- for (int i = 0 ; i <10*1024*1024 ; i++) {
- tenMB[i]=127;
- }
- byte[] sevenMB = new byte[7*1024*1024];
- for (int i = 0 ; i <7*1024*1024 ; i++) {
- sevenMB[i]=127;
- }
- Cache<String, byte[]> cache=new Cache<>(9*1024*1024,3600, 100*1024*1024); // 9 MB
- assertFalse(cache.put("foo", tenMB));
- assertTrue(cache.put("foo", sevenMB));
- assertEquals(cache.get("foo"), sevenMB);
- }
-
- public void testInvalidate() {
- byte[] tenMB = new byte[10*1024*1024];
- for (int i = 0 ; i <10*1024*1024 ; i++) {
- tenMB[i]=127;
- }
- byte[] sevenMB = new byte[7*1024*1024];
- for (int i = 0 ; i <7*1024*1024 ; i++) {
- sevenMB[i]=127;
- }
- //log.info("10 MB: "+calc.sizeOf(tenMB));
- //log.info("7 MB: "+calc.sizeOf(sevenMB));
- Cache<String, byte[]> cache=new Cache<>(11*1024*1024,3600, 100*1024*1024); // 11 MB
- assertTrue(cache.put("foo", sevenMB));
- assertTrue(cache.put("bar", tenMB));
- assertNull(cache.get("foo"));
- assertEquals(cache.get("bar"), tenMB);
- }
-
- public void testInvalidateLRU() {
- Cache<String, byte[]> cache=new Cache<>(10*1024*1024,3600, 100*1024*1024); // 10 MB
- byte[] fiveMB = new byte[5*1024*1024];
- for (int i = 0 ; i <5*1024*1024 ; i++) {
- fiveMB[i]=127;
- }
-
- byte[] twoMB = new byte[2*1024*1024];
- for (int i = 0 ; i <2*1024*1024 ; i++) {
- twoMB[i]=127;
- }
-
- byte[] fourMB = new byte[4*1024*1024];
- for (int i = 0 ; i <4*1024*1024 ; i++) {
- fourMB[i]=127;
- }
- assertTrue(cache.put("five", fiveMB));
- assertTrue(cache.put("two", twoMB));
- Object dummy = cache.get("five"); // Makes two LRU
- assertEquals(dummy, fiveMB);
- assertTrue(cache.put("four", fourMB));
- assertNull(cache.get("two"));
- assertEquals(cache.get("five"), fiveMB);
- assertEquals(cache.get("four"), fourMB);
-
- // Same, without the access, just to check
- cache=new Cache<>(10*1024*1024,3600, 100*1024*1024); // 10 MB
- assertTrue(cache.put("five", fiveMB));
- assertTrue(cache.put("two", twoMB));
- assertTrue(cache.put("four", fourMB));
- assertEquals(cache.get("two"), twoMB);
- assertNull(cache.get("five"));
- assertEquals(cache.get("four"), fourMB);
- }
-
- public void testPutSameKey() {
- Cache<String, byte[]> cache=new Cache<>(10*1024*1024,3600, 100*1024*1024); // 10 MB
- byte[] fiveMB = new byte[5*1024*1024];
- for (int i = 0 ; i <5*1024*1024 ; i++) {
- fiveMB[i]=127;
- }
-
- byte[] twoMB = new byte[2*1024*1024];
- for (int i = 0 ; i <2*1024*1024 ; i++) {
- twoMB[i]=127;
- }
-
- byte[] fourMB = new byte[4*1024*1024];
- for (int i = 0 ; i <4*1024*1024 ; i++) {
- fourMB[i]=127;
- }
- assertTrue(cache.put("five", fiveMB));
- assertTrue(cache.put("two", twoMB));
- assertEquals(cache.get("two"), twoMB);
- assertEquals(cache.get("five"), fiveMB);
- assertTrue(cache.put("five", twoMB));
- assertEquals(cache.get("five"), twoMB);
- assertEquals(cache.get("two"), twoMB);
- }
-
- public void testExpire() throws InterruptedException {
- Cache<String, String> cache=new Cache<>(10*1024*1024,400, 10000); // 10 MB, .4 sec expire
- cache.put("foo", "bar");
- cache.put("hey", "ho");
- assertEquals(cache.get("foo"), "bar");
- assertEquals(cache.get("hey"), "ho");
- Thread.sleep(600);
- assertNull(cache.get("foo"));
- assertNull(cache.get("hey"));
- }
-
- public void testInsertSame() {
- Cache<String, String> cache=new Cache<>(10*1024*1024,500, 10000); // 10 MB, .5 sec expire
- String k = "foo";
- String r = "bar";
- cache.put(k, r);
- assertEquals(cache.size(), 1);
- cache.put(k, r);
- assertEquals(cache.size(), 1);
- }
-
- public void testMaxSize() {
- Cache<String, byte[]> cache=new Cache<>(20*1024*1024,500, 3*1024*1024);
- byte[] fourMB = new byte[4*1024*1024];
- for (int i = 0 ; i <4*1024*1024 ; i++) {
- fourMB[i]=127;
- }
- byte[] twoMB = new byte[2*1024*1024];
- for (int i = 0 ; i <2*1024*1024 ; i++) {
- twoMB[i]=127;
- }
- assertFalse(cache.put("four", fourMB));
- assertTrue(cache.put("two", twoMB));
- assertNull(cache.get("four"));
- assertNotNull(cache.get("two"));
- }
-
- public void testMaxSizeNoLimit() {
- Cache<String, byte[]> cache=new Cache<>(20*1024*1024,500, -1);
- byte[] fourMB = new byte[4*1024*1024];
- for (int i = 0 ; i <4*1024*1024 ; i++) {
- fourMB[i]=127;
- }
- byte[] twoMB = new byte[2*1024*1024];
- for (int i = 0 ; i <2*1024*1024 ; i++) {
- twoMB[i]=127;
- }
- assertTrue(cache.put("four", fourMB));
- assertTrue(cache.put("two", twoMB));
- assertNotNull(cache.get("four"));
- assertNotNull(cache.get("two"));
- }
-
- public void testGetKeysAndValuesAndClear() {
- Cache<String, String> cache=new Cache<>(10*1024*1024,500, 10000); // 10 MB, .5 sec expire
- assertEquals(cache.getKeys().size(), 0);
- assertEquals(cache.getValues().size(), 0);
- cache.put("a", "b");
- cache.put("c", "d");
- cache.put("e", "f");
- Collection<String> keys = new ArrayList<>();
- keys.add("a");
- keys.add("c");
- keys.add("e");
- Collection<String> values = new ArrayList<>();
- values.add("b");
- values.add("d");
- values.add("f");
- assertEquals(cache.getKeys().size(), 3);
- assertEquals(cache.getValues().size(), 3);
- assertTrue(cache.getKeys().containsAll(keys));
- assertTrue(cache.getValues().containsAll(values));
- cache.clear();
- assertEquals(cache.getKeys().size(), 0);
- assertEquals(cache.getValues().size(), 0);
- }
-
-}
diff --git a/vespajlib/src/test/java/com/yahoo/cache/CalcTestCase.java b/vespajlib/src/test/java/com/yahoo/cache/CalcTestCase.java
deleted file mode 100644
index 6d9a6b6f422..00000000000
--- a/vespajlib/src/test/java/com/yahoo/cache/CalcTestCase.java
+++ /dev/null
@@ -1,178 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.cache;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static java.lang.Boolean.TRUE;
-
-public class CalcTestCase extends junit.framework.TestCase {
-
- private SizeCalculator calc;
-
-
- public CalcTestCase (String name) {
- super(name);
- }
-
- public void setUp() {
- calc = new SizeCalculator();
- }
-
- public void testCalc1() {
- assertEquals(calc.sizeOf(new Object()), 8);
- }
-
- public void testCalc2() {
- assertEquals(calc.sizeOf(new SixtyFourBooleans()), 8+64);
- }
-
- public void testBoolean() {
- assertEquals(8+1, calc.sizeOf(TRUE));
- }
-
- public void testArrayPrimitive() {
- byte[] eightBytes = new byte[]{1,1,1,1,1,1,1,1,};
- assertEquals(16+8, calc.sizeOf(eightBytes));
- }
-
- public void testArrayObjects() {
- SixtyFourBooleans[] bunchOfBooleans = new SixtyFourBooleans[]{new SixtyFourBooleans(),
- new SixtyFourBooleans(), new SixtyFourBooleans()};
- assertEquals(16+(3*(8+64)+(3*4)), calc.sizeOf(bunchOfBooleans));
-
- }
-
- public void testSizeOfList() {
- SixtyFourBooleans sfb = new SixtyFourBooleans();
- List<Object> dupList1 = new ArrayList<>();
- dupList1.add(new Object());
- dupList1.add(sfb);
- dupList1.add(sfb);
- dupList1.add(sfb);
- List<Object> dupList2 = new ArrayList<>();
- dupList2.addAll(dupList1);
- dupList2.add(sfb);
- dupList2.add(sfb);
- dupList2.add(sfb);
- dupList2.add(new Object());
- dupList2.add(new Object());
- assertEquals(calc.sizeOf(dupList2), calc.sizeOf(dupList1)+8+8);
- }
-
- public void testSizeOfTuple() {
- SixtyFourBooleans[] bunchOfBooleans = new SixtyFourBooleans[]{new SixtyFourBooleans(),
- new SixtyFourBooleans(), new SixtyFourBooleans()};
- SixtyFourBooleans[] bunchOfBooleans2 = new SixtyFourBooleans[]{new SixtyFourBooleans(),
- new SixtyFourBooleans(), new SixtyFourBooleans()};
- assertEquals(16+(3*(8+64)+(3*4)), calc.sizeOf(bunchOfBooleans));
- assertEquals(2* (16+(3*(8+64)+(3*4))), calc.sizeOf(bunchOfBooleans, bunchOfBooleans2));
- }
-
- /*public void testEmptyArrayList() {
- assertEquals(80, calc.sizeOf(new ArrayList()));
- }*/
-
- /*public void testFullArrayList() {
- ArrayList arrayList = new ArrayList(10000);
-
- for (int i = 0; i < 10000; i++) {
- arrayList.add(new Object());
- }
-
- assertEquals(120040, calc.sizeOf(arrayList));
- }*/
-
- /*public void testHashMap() {
- assertEquals(120, calc.sizeOf(new HashMap()));
-
- Byte[] all = new Byte[256];
- for (int i = -128; i < 128; i++) {
- all[i + 128] = new Byte((byte) i);
- }
- assertEquals(5136, calc.sizeOf(all));
-
- HashMap hm = new HashMap();
- for (int i = -128; i < 128; i++) {
- hm.put("" + i, new Byte((byte) i));
- }
- assertEquals(30776, calc.sizeOf(hm));
- }*/
-
- /*public void testThousandBooleansObjects() {
- Boolean[] booleans = new Boolean[1000];
-
- for (int i = 0; i < booleans.length; i++)
- booleans[i] = new Boolean(true);
-
- assertEquals(20016, calc.sizeOf(booleans));
- }*/
-
- @SuppressWarnings("unused")
- private static class SixtyFourBooleans {
- boolean a0;
- boolean a1;
- boolean a2;
- boolean a3;
- boolean a4;
- boolean a5;
- boolean a6;
- boolean a7;
- boolean b0;
- boolean b1;
- boolean b2;
- boolean b3;
- boolean b4;
- boolean b5;
- boolean b6;
- boolean b7;
- boolean c0;
- boolean c1;
- boolean c2;
- boolean c3;
- boolean c4;
- boolean c5;
- boolean c6;
- boolean c7;
- boolean d0;
- boolean d1;
- boolean d2;
- boolean d3;
- boolean d4;
- boolean d5;
- boolean d6;
- boolean d7;
- boolean e0;
- boolean e1;
- boolean e2;
- boolean e3;
- boolean e4;
- boolean e5;
- boolean e6;
- boolean e7;
- boolean f0;
- boolean f1;
- boolean f2;
- boolean f3;
- boolean f4;
- boolean f5;
- boolean f6;
- boolean f7;
- boolean g0;
- boolean g1;
- boolean g2;
- boolean g3;
- boolean g4;
- boolean g5;
- boolean g6;
- boolean g7;
- boolean h0;
- boolean h1;
- boolean h2;
- boolean h3;
- boolean h4;
- boolean h5;
- boolean h6;
- boolean h7;
- }
-}