Author: frm
Date: Thu Jun 22 16:08:12 2017
New Revision: 1799591

URL: http://svn.apache.org/viewvc?rev=1799591&view=rev
Log:
OAK-6325 - Turn CacheWeightsTest into a command-line utility

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightEstimator.java
      - copied, changed from r1799590, 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightsTest.java
Removed:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightsTest.java

Copied: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightEstimator.java
 (from r1799590, 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightsTest.java)
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightEstimator.java?p2=jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightEstimator.java&p1=jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightsTest.java&r1=1799590&r2=1799591&rev=1799591&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightsTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/CacheWeightEstimator.java
 Thu Jun 22 16:08:12 2017
@@ -20,25 +20,19 @@ package org.apache.jackrabbit.oak.segmen
 
 import static org.apache.jackrabbit.oak.segment.Segment.GC_GENERATION_OFFSET;
 import static org.apache.jackrabbit.oak.segment.SegmentVersion.LATEST_VERSION;
-import static org.junit.Assume.assumeTrue;
 
 import java.nio.ByteBuffer;
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Map.Entry;
 import java.util.UUID;
+import java.util.function.Supplier;
 
-import com.google.common.base.Supplier;
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.jackrabbit.oak.cache.AbstractCacheStats;
 import org.apache.jackrabbit.oak.commons.StringUtils;
 import org.apache.jackrabbit.oak.segment.CacheWeights.StringCacheWeigher;
 import org.apache.jackrabbit.oak.segment.file.PriorityCache;
 import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Test/Utility class to measure size in memory for common segment-tar objects.
@@ -53,13 +47,7 @@ import org.slf4j.LoggerFactory;
  * {@code cat 
target/surefire-reports/org.apache.jackrabbit.oak.segment.CacheWeightsTest-output.txt}
  * </p>
  */
-public class CacheWeightsTest {
-
-    private static final Logger LOG = LoggerFactory
-            .getLogger(CacheWeightsTest.class);
-
-    private static final boolean ENABLED = Boolean
-            .getBoolean(CacheWeightsTest.class.getSimpleName());
+public class CacheWeightEstimator {
 
     // 
http://www.javaworld.com/article/2077496/testing-debugging/java-tip-130--do-you-know-your-data-size-.html
     // http://www.javaspecialists.eu/archive/Issue029.html
@@ -119,65 +107,62 @@ public class CacheWeightsTest {
 
      */
 
-    private MemoryStore store;
+    private static MemoryStore store;
 
-    @BeforeClass
-    public static void assumptions() {
-        assumeTrue(ENABLED);
+    public static void main(String... args) throws Exception {
+        run(CacheWeightEstimator::testObjects);
+        run(CacheWeightEstimator::testSegmentIds);
+        run(CacheWeightEstimator::testSegmentIdsWGc);
+        run(CacheWeightEstimator::testRecordIds);
+        run(CacheWeightEstimator::testRecordIdsWGc);
+        run(CacheWeightEstimator::testStringCache);
+        run(CacheWeightEstimator::testNodeCache);
+        run(CacheWeightEstimator::testSegments);
+        run(CacheWeightEstimator::testSegmentCache);
+        run(CacheWeightEstimator::testStrings);
     }
 
-    @Before
-    public void setup() throws Exception {
+    private static void run(Runnable runnable) throws Exception {
         store = new MemoryStore();
+        try {
+            runnable.run();
+        } finally {
+            store = null;
+        }
     }
 
-    @Test
-    public void testObjects() {
+    private static void testObjects() {
         final int count = 1000000;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-
-                Object[] objects = new Object[count];
-                for (int i = 0; i < count; ++i) {
-                    Object o = new Object();
-                    objects[i] = o;
-                }
-                long weight = CacheWeights.OBJECT_HEADER_SIZE * count;
-                return new SimpleImmutableEntry<Object, Long[]>(objects,
-                        new Long[] { (long) count, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            Object[] objects = new Object[count];
+            for (int i = 0; i < count; ++i) {
+                Object o = new Object();
+                objects[i] = o;
             }
+            long weight = CacheWeights.OBJECT_HEADER_SIZE * count;
+            return new SimpleImmutableEntry<>(objects, new Long[] {(long) 
count, weight});
         };
         runTest(factory, "Object[x" + count + "]");
     }
 
-    @Test
-    public void testSegmentIds() {
+    private static void testSegmentIds() {
         runSegmentIds(1000000, false);
     }
 
-    @Test
-    public void testSegmentIdsWGc() {
+    private static void testSegmentIdsWGc() {
         runSegmentIds(1000000, true);
     }
 
-    private void runSegmentIds(final int count, final boolean gcInfo) {
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-                long weight = 0;
-                Object[] objects = new Object[count];
-                for (int i = 0; i < count; ++i) {
-                    SegmentId o = randomSegmentId(gcInfo);
-                    weight += o.estimateMemoryUsage();
-                    objects[i] = o;
-                }
-                return new SimpleImmutableEntry<Object, Long[]>(objects,
-                        new Long[] { (long) count, weight });
+    private static void runSegmentIds(final int count, final boolean gcInfo) {
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            long weight = 0;
+            Object[] objects = new Object[count];
+            for (int i = 0; i < count; ++i) {
+                SegmentId o = randomSegmentId(gcInfo);
+                weight += o.estimateMemoryUsage();
+                objects[i] = o;
             }
+            return new SimpleImmutableEntry<>(objects, new Long[] {(long) 
count, weight});
         };
         String name = "SegmentId";
         if (gcInfo) {
@@ -188,31 +173,24 @@ public class CacheWeightsTest {
         runTest(factory, name);
     }
 
-    @Test
-    public void testRecordIds() {
+    private static void testRecordIds() {
         runRecordIds(1000000, false);
     }
 
-    @Test
-    public void testRecordIdsWGc() {
+    private static void testRecordIdsWGc() {
         runRecordIds(1000000, true);
     }
 
-    private void runRecordIds(final int count, final boolean gcInfo) {
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-                long weight = 0;
-                Object[] objects = new Object[count];
-                for (int i = 0; i < count; ++i) {
-                    RecordId o = randomRecordId(gcInfo);
-                    weight += o.estimateMemoryUsage();
-                    objects[i] = o;
-                }
-                return new SimpleImmutableEntry<Object, Long[]>(objects,
-                        new Long[] { (long) count, weight });
+    private static void runRecordIds(final int count, final boolean gcInfo) {
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            long weight = 0;
+            Object[] objects = new Object[count];
+            for (int i = 0; i < count; ++i) {
+                RecordId o = randomRecordId(gcInfo);
+                weight += o.estimateMemoryUsage();
+                objects[i] = o;
             }
+            return new SimpleImmutableEntry<>(objects, new Long[] {(long) 
count, weight});
         };
         String name = "RecordId";
         if (gcInfo) {
@@ -223,152 +201,109 @@ public class CacheWeightsTest {
         runTest(factory, name);
     }
 
-    @Test
-    public void testStringCache() {
+    private static void testStringCache() {
         final int count = 1000000;
         final int keySize = 96;
         final boolean gcInfo = true;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-                RecordCache<String> cache = RecordCache.factory(count,
-                        new StringCacheWeigher()).get();
-                for (int i = 0; i < count; ++i) {
-                    String k = randomString(keySize);
-                    RecordId v = randomRecordId(gcInfo);
-                    cache.put(k, v);
-                }
-                long weight = cache.estimateCurrentWeight();
-                return new SimpleImmutableEntry<Object, Long[]>(cache,
-                        new Long[] { (long) count, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            RecordCache<String> cache = RecordCache.factory(count, new 
StringCacheWeigher()).get();
+            for (int i = 0; i < count; ++i) {
+                String k = randomString(keySize);
+                RecordId v = randomRecordId(gcInfo);
+                cache.put(k, v);
             }
+            long weight = cache.estimateCurrentWeight();
+            return new SimpleImmutableEntry<>(cache, new Long[] {(long) count, 
weight});
         };
         runTest(factory, "StringCache[x" + count
                 + "|RecordCache<String, RecordId>]");
     }
 
-    @Test
-    public void testNodeCache() {
+    private static void testNodeCache() {
         final int count = 1000000;
         // key usually is a stableid, see SegmentNodeState#getStableId
         // 2fdd370e-423c-43d6-aad7-6e336c551a38:xxxxxx
         final int keySize = 43;
         final boolean gcInfo = true;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-                int size = (int) PriorityCache.nextPowerOfTwo(count);
-                PriorityCache<String, RecordId> cache = PriorityCache
-                        .<String, RecordId> factory(size,
-                                new CacheWeights.NodeCacheWeigher()).get();
-
-                for (int i = 0; i < count; ++i) {
-                    String k = randomString(keySize);
-                    RecordId v = randomRecordId(gcInfo);
-                    cache.put(k, v, 0, (byte) 0);
-                }
-                long weight = cache.estimateCurrentWeight();
-                return new SimpleImmutableEntry<Object, Long[]>(cache,
-                        new Long[] { (long) count, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            int size = (int) PriorityCache.nextPowerOfTwo(count);
+            PriorityCache<String, RecordId> cache = 
PriorityCache.factory(size, new CacheWeights.NodeCacheWeigher()).get();
+            for (int i = 0; i < count; ++i) {
+                String k = randomString(keySize);
+                RecordId v = randomRecordId(gcInfo);
+                cache.put(k, v, 0, (byte) 0);
             }
+            long weight = cache.estimateCurrentWeight();
+            return new SimpleImmutableEntry<>(cache, new Long[] {(long) count, 
weight});
         };
         runTest(factory, "NodeCache[x" + count
                 + "|PriorityCache<String, RecordId>]");
     }
 
-    @Test
-    public void testSegments() {
+    private static void testSegments() {
         final int count = 10000;
         final int bufferSize = 5 * 1024;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-
-                long weight = 0;
-                Object[] objects = new Object[count];
-                for (int i = 0; i < count; ++i) {
-                    Segment o = randomSegment(bufferSize);
-                    weight += o.estimateMemoryUsage();
-                    objects[i] = o;
-                }
-                return new SimpleImmutableEntry<Object, Long[]>(objects,
-                        new Long[] { (long) count, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            long weight = 0;
+            Object[] objects = new Object[count];
+            for (int i = 0; i < count; ++i) {
+                Segment o = randomSegment(bufferSize);
+                weight += o.estimateMemoryUsage();
+                objects[i] = o;
             }
+            return new SimpleImmutableEntry<>(objects, new Long[] {(long) 
count, weight});
         };
         runTest(factory, "Segment[x" + count + "|" + bufferSize + "]");
     }
 
-    @Test
-    public void testSegmentCache() {
+    private static void testSegmentCache() {
         final int count = 10000;
         final int cacheSizeMB = 100;
         final int bufferSize = 5 * 1024;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-                SegmentCache cache = new SegmentCache(cacheSizeMB);
-                for (int i = 0; i < count; ++i) {
-                    Segment segment = randomSegment(bufferSize);
-                    cache.putSegment(segment);
-                }
-                AbstractCacheStats stats = cache.getCacheStats();
-                long elements = stats.getElementCount();
-                long weight = stats.estimateCurrentWeight();
-                return new SimpleImmutableEntry<Object, Long[]>(cache,
-                        new Long[] { elements, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            SegmentCache cache = new SegmentCache(cacheSizeMB);
+            for (int i = 0; i < count; ++i) {
+                Segment segment = randomSegment(bufferSize);
+                cache.putSegment(segment);
             }
+            AbstractCacheStats stats = cache.getCacheStats();
+            long elements = stats.getElementCount();
+            long weight = stats.estimateCurrentWeight();
+            return new SimpleImmutableEntry<>(cache, new Long[] {elements, 
weight});
         };
-        runTest(factory, "SegmentCache[x" + cacheSizeMB + "MB|" + bufferSize
-                + "|Cache<SegmentId, Segment>]");
+        runTest(factory, "SegmentCache[x" + cacheSizeMB + "MB|" + bufferSize + 
"|Cache<SegmentId, Segment>]");
     }
 
-    @Test
-    public void testStrings() {
+    private static void testStrings() {
         final int count = 10000;
         final int length = 256;
-
-        Supplier<Entry<Object, Long[]>> factory = new Supplier<Entry<Object, 
Long[]>>() {
-
-            @Override
-            public Entry<Object, Long[]> get() {
-
-                long weight = 0;
-                Object[] objects = new Object[count];
-                for (int i = 0; i < count; ++i) {
-                    String s = randomString(length);
-                    weight += StringUtils.estimateMemoryUsage(s);
-                    objects[i] = s;
-                }
-                return new SimpleImmutableEntry<Object, Long[]>(objects,
-                        new Long[] { (long) count, weight });
+        Supplier<Entry<Object, Long[]>> factory = () -> {
+            long weight = 0;
+            Object[] objects = new Object[count];
+            for (int i = 0; i < count; ++i) {
+                String s = randomString(length);
+                weight += StringUtils.estimateMemoryUsage(s);
+                objects[i] = s;
             }
+            return new SimpleImmutableEntry<>(objects, new Long[] {(long) 
count, weight});
         };
         runTest(factory, "String[x" + count + "|" + length + "]");
     }
 
-    private SegmentId randomSegmentId(boolean withGc) {
+    private static SegmentId randomSegmentId(boolean withGc) {
         UUID u = UUID.randomUUID();
-        SegmentId id = new SegmentId(store, u.getMostSignificantBits(),
-                u.getLeastSignificantBits());
+        SegmentId id = new SegmentId(store, u.getMostSignificantBits(), 
u.getLeastSignificantBits());
         if (withGc) {
             id.reclaimed(randomString(80));
         }
         return id;
     }
 
-    private RecordId randomRecordId(boolean withGc) {
+    private static RecordId randomRecordId(boolean withGc) {
         return new RecordId(randomSegmentId(withGc), 128);
     }
 
-    private Segment randomSegment(int bufferSize) {
+    private static Segment randomSegment(int bufferSize) {
         byte[] buffer = new byte[bufferSize];
         buffer[0] = '0';
         buffer[1] = 'a';
@@ -406,9 +341,7 @@ public class CacheWeightsTest {
     }
 
     @SuppressWarnings("unused")
-    private static void runTest(Supplier<Entry<Object, Long[]>> factory,
-            String name) {
-
+    private static void runTest(Supplier<Entry<Object, Long[]>> factory, 
String name) {
         long start = memory();
         Entry<Object, Long[]> e = factory.get();
         Object object = e.getKey(); // prevent gc
@@ -420,22 +353,20 @@ public class CacheWeightsTest {
         long itemH = delta / count;
         long itemW = weight / count;
 
-        LOG.info(":: {} Test", name);
-        LOG.info("heap delta is       {}, {} bytes per item ({} -> {})", delta,
-                itemH, start, end);
-        LOG.info("estimated weight is {}, {} bytes per item", weight, itemW);
+        System.out.printf(":: %s Test\n", name);
+        System.out.printf("heap delta is       %d, %d bytes per item (%d -> 
%d)\n", delta, itemH, start, end);
+        System.out.printf("estimated weight is %d, %d bytes per item\n", 
weight, itemW);
         if (itemW > itemH * 1.1) {
-            LOG.info("*warn* estimated weight is over 10% bigger than heap 
based weight");
+            System.out.printf("*warn* estimated weight is over 10%% bigger 
than heap based weight\n");
         }
         if (itemW * 1.1 < itemH) {
-            LOG.info("*warn* estimated weight is over 10% smaller than heap 
based weight");
+            System.out.printf("*warn* estimated weight is over 10%% smaller 
than heap based weight\n");
         }
     }
 
     private static long memory() {
         gc();
-        return Runtime.getRuntime().totalMemory()
-                - Runtime.getRuntime().freeMemory();
+        return Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory();
     }
 
     private static void gc() {


Reply via email to