You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2013/03/20 17:15:02 UTC

svn commit: r1458916 - in /jackrabbit/oak/trunk/oak-mongomk/src: main/java/org/apache/jackrabbit/mongomk/prototype/Node.java test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java

Author: thomasm
Date: Wed Mar 20 16:15:02 2013
New Revision: 1458916

URL: http://svn.apache.org/r1458916
Log:
OAK-619 Lock-free MongoMK implementation (estimate memory usage for caching)

Added:
    jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java
Modified:
    jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/Node.java

Modified: jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/Node.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/Node.java?rev=1458916&r1=1458915&r2=1458916&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/Node.java (original)
+++ jackrabbit/oak/trunk/oak-mongomk/src/main/java/org/apache/jackrabbit/mongomk/prototype/Node.java Wed Mar 20 16:15:02 2013
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.mongomk.pr
 import java.util.ArrayList;
 import java.util.Map;
 import java.util.Set;
+import java.util.Map.Entry;
 
 import org.apache.jackrabbit.mk.json.JsopWriter;
 
@@ -99,6 +100,14 @@ public class Node {
         this.lastRevision = lastRevision;
     }
 
+    public int getMemory() {
+        int size = 180 + path.length() * 2;
+        for (Entry<String, String> e : properties.entrySet()) {
+            size += 136 + e.getKey().length() * 2 + e.getValue().length() * 2;
+        }
+        return size;
+    }
+
     /**
      * A list of children for a node.
      */

Added: jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java?rev=1458916&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java (added)
+++ jackrabbit/oak/trunk/oak-mongomk/src/test/java/org/apache/jackrabbit/mongomk/prototype/MeasureMemory.java Wed Mar 20 16:15:02 2013
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mongomk.prototype;
+
+import java.util.LinkedList;
+import java.util.concurrent.Callable;
+
+import org.junit.Test;
+import static org.junit.Assert.fail;
+
+/**
+ * Simple test to measure how much memory a certain object uses.
+ */
+public class MeasureMemory {
+    
+    static final boolean TRACE = true;
+    
+    static final int TEST_COUNT = 10000;
+    static final int OVERHEAD = 24;
+
+    @Test
+    public void overhead() throws Exception {
+        measureMemory(new Callable<Object[]>() {
+            @Override
+            public Object[] call() {
+                return new Object[]{"", OVERHEAD};
+            }
+        });
+    }
+    
+    @Test
+    public void node() throws Exception {
+        measureMemory(new Callable<Object[]>() {
+            @Override
+            public Object[] call() {
+                Node n = generateNode(5);
+                return new Object[]{n, n.getMemory() + OVERHEAD};
+            }
+        });
+    }
+    
+    @Test
+    public void nodeWithoutProperties() throws Exception {
+        measureMemory(new Callable<Object[]>() {
+            @Override
+            public Object[] call() {
+                Node n = generateNode(0);
+                return new Object[]{n, n.getMemory() + OVERHEAD};
+            }
+        });
+    }
+    
+    private static void measureMemory(Callable<Object[]> c) throws Exception {
+        LinkedList<Object> list = new LinkedList<Object>();
+        long base = getMemoryUsed();
+        long mem = 0;
+        for (int i = 0; i < TEST_COUNT; i++) {
+            Object[] om = c.call();
+            list.add(om[0]);
+            mem += (Integer) om[1];
+        }
+        long used = getMemoryUsed() - base;
+        int estimation = (int) (100 * mem / used);
+        String message = 
+                new Error().getStackTrace()[1].getMethodName() + "\n" +
+                "used: " + used + " calculated: " + mem + "\n" +
+                "estimation is " + estimation + "%\n";
+        if (TRACE) {
+            System.out.println(message);
+        }
+        if (estimation < 80 || estimation > 120) {
+            fail(message);
+        }
+        // need to keep the reference until here, otherwise
+        // the list might be garbage collected too early
+        list.clear();
+    }
+    
+    static Node generateNode(int propertyCount) {
+        Node n = new Node(new String("/hello/world"), new Revision(1, 2, 3));
+        for (int i = 0; i < propertyCount; i++) {
+            n.setProperty("property" + i, "values " + i);
+        }
+        n.setLastRevision(new Revision(1, 2, 3));
+        return n;
+    }
+    
+    private static long getMemoryUsed() {
+        for (int i = 0; i < 10; i++) {
+            System.gc();
+        }
+        return Runtime.getRuntime().totalMemory()
+                - Runtime.getRuntime().freeMemory();
+    }
+    
+}