[jboss-svn-commits] JBL Code SVN: r19267 - in labs/jbossrules/branches/ming-serialization: drools-core/src/main/java/org/drools/common and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Mar 27 15:30:45 EDT 2008


Author: mingjin
Date: 2008-03-27 15:30:45 -0400 (Thu, 27 Mar 2008)
New Revision: 19267

Modified:
   labs/jbossrules/branches/ming-serialization/drools-compiler/src/test/java/org/drools/integrationtests/LargeRuleBaseSerializationTest.java
   labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
Log:
DroolsObjectInputStream - performance optimization.

Modified: labs/jbossrules/branches/ming-serialization/drools-compiler/src/test/java/org/drools/integrationtests/LargeRuleBaseSerializationTest.java
===================================================================
--- labs/jbossrules/branches/ming-serialization/drools-compiler/src/test/java/org/drools/integrationtests/LargeRuleBaseSerializationTest.java	2008-03-27 16:04:41 UTC (rev 19266)
+++ labs/jbossrules/branches/ming-serialization/drools-compiler/src/test/java/org/drools/integrationtests/LargeRuleBaseSerializationTest.java	2008-03-27 19:30:45 UTC (rev 19267)
@@ -2,12 +2,17 @@
 
 import org.drools.compiler.DrlParser;
 import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.DroolsParserException;
 import org.drools.lang.descr.PackageDescr;
 import org.drools.rule.Package;
 import org.drools.RuleBase;
 import org.drools.RuleBaseFactory;
+import org.drools.common.DroolsObjectOutputStream;
+import org.drools.common.DroolsObjectInputStream;
 
 import java.io.StringReader;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
 
 import junit.framework.TestCase;
 
@@ -16,9 +21,61 @@
  * Settings | File Templates.
  */
 public class LargeRuleBaseSerializationTest extends TestCase {
-    private static final int    RULE_COUNT  = 1000;
+    private static final int    RULE_COUNT = Integer.parseInt(System.getProperty("test.count", "1000"));
+    private static final int    ITERATIONS = Integer.parseInt(System.getProperty("test.iterations", "5"));
 
-    public void testLargeRuleBase() throws Exception{
+    private static RuleBase ruleBase;
+    private static byte[]   bytes;
+
+    protected void setUp() throws Exception {
+        if (ruleBase == null)
+            ruleBase    = createRuleBase();
+        if (bytes == null) {
+            bytes   = SerializationHelper.serializeOut(ruleBase);
+        }
+    }
+
+    public void testUnmarshallingPerformance() throws Exception {
+        SerializationHelper.serializeIn(bytes);
+        long    time    = System.currentTimeMillis();
+
+        for (int i = ITERATIONS; i-- > 0; ) {
+            SerializationHelper.serializeIn(bytes);
+        }
+        System.out.println("Total time of unmarshalling "+ITERATIONS+" times is "+
+                           format(System.currentTimeMillis()-time));
+    }
+
+    public void testMarshallingPerformance() throws Exception {
+        long    time    = System.currentTimeMillis();
+        for (int i = ITERATIONS; i-- > 0; ) {
+            SerializationHelper.serializeOut(ruleBase);
+        }
+        System.out.println("Total time of marshalling "+ITERATIONS+" times is "+
+                           format(System.currentTimeMillis()-time)+" with size of "+bytes.length+" bytes");
+    }
+
+    private static final int    MILLIS_IN_SECOND    = 1000;
+    private static final int    MILLIS_IN_MINUTE    = 60*MILLIS_IN_SECOND;
+    private static final int    MILLIS_IN_HOUR      = 60*MILLIS_IN_MINUTE;
+
+    private static String format(long time) {
+        StringBuilder   sb  = new StringBuilder();
+
+        if (time/MILLIS_IN_HOUR > 0) {
+            sb.append(time/MILLIS_IN_HOUR).append(':');
+            time    -= time/MILLIS_IN_HOUR*MILLIS_IN_HOUR;
+        }
+        if (time/MILLIS_IN_MINUTE > 0) {
+            sb.append(time/MILLIS_IN_MINUTE).append(':');
+            time    -= time/MILLIS_IN_MINUTE*MILLIS_IN_MINUTE;
+        }
+        sb.append(time*1.0/MILLIS_IN_SECOND);
+
+        return sb.toString();
+    }
+
+    private static RuleBase createRuleBase() throws DroolsParserException {
         System.out.println("Generating "+RULE_COUNT+" rules");
         StringBuilder   sb  = new StringBuilder(LargeRuleBase.getHeader());
 
@@ -33,11 +90,10 @@
         pkgBuilder.addPackage(pkgDescr);
 
         Package pkg = pkgBuilder.getPackage();
-        RuleBase rb = RuleBaseFactory.newRuleBase();
+        ruleBase = RuleBaseFactory.newRuleBase();
 
-        rb.addPackage(pkg);
-
-        rb  = SerializationHelper.serializeObject(rb);
+        ruleBase.addPackage(pkg);
+        return ruleBase;
     }
 
 }

Modified: labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
===================================================================
--- labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java	2008-03-27 16:04:41 UTC (rev 19266)
+++ labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java	2008-03-27 19:30:45 UTC (rev 19267)
@@ -212,8 +212,70 @@
         byte type = readRecordType();
 
         switch (type) {
+            case RT_REFERENCE:
+                return objectsByHandle.get(dataInput.readInt());
             case RT_NULL:
-                return readNull();
+                return null;
+            case RT_EXTERNALIZABLE: {
+                int handle = dataInput.readInt();
+                Class clazz = (Class) readObject();
+                Externalizable externalizable;
+                try {
+                    externalizable = (Externalizable) clazz.newInstance();
+                } catch (InstantiationException e) {
+                    throw newInvalidClassException(clazz, e);
+                } catch (IllegalAccessException e) {
+                    throw newInvalidClassException(clazz, e);
+                }
+                registerObject(handle, externalizable);
+                externalizable.readExternal(this);
+                return externalizable;
+            }
+            case RT_MAP: {
+                int handle = dataInput.readInt();
+                Class clazz = (Class) readObject();
+                int size = dataInput.readInt();
+                Map<Object, Object> map = (Map<Object, Object>) newCollection(handle, clazz, size);
+                while (size-- > 0) {
+                    Object key = readObject();
+                    Object value = readObject();
+                    map.put(key, value);
+                }
+                return map;
+            }
+            case RT_ARRAY: {
+                int handle = dataInput.readInt();
+                Class clazz = (Class) readObject();
+                int length = dataInput.readInt();
+                Class componentType = clazz.getComponentType();
+                Object array = Array.newInstance(componentType, length);
+                registerObject(handle, array);
+                if (componentType.isPrimitive()) {
+                    readPrimitiveArray(array, length, componentType);
+                } else {
+                    Object[] objects    = (Object[])array;
+                    for (int i = 0; i < length; ++i) {
+                        objects[i] = readObject();
+                    }
+                }
+                return array;
+            }
+            case RT_COLLECTION: {
+                int handle = dataInput.readInt();
+                Class clazz = (Class) readObject();
+                int size = dataInput.readInt();
+                Collection<Object> collection = (Collection<Object>) newCollection(handle, clazz, size);
+                while (size-- > 0) {
+                    collection.add(readObject());
+                }
+                return collection;
+            }
+            case RT_SERIALIZABLE: {
+                int handle = dataInput.readInt();
+                Object  object  =  dataInput.readObject();
+                registerObject(handle, object);
+                return object;
+            }
             case RT_EMPTY_SET:
                 return readEmptySet();
             case RT_EMPTY_LIST:
@@ -224,62 +286,10 @@
                 int handle = readHandle();
 
                 switch (type) {
-                    case RT_EXTERNALIZABLE: {
-                        Class clazz = (Class) readObject();
-                        Externalizable externalizable;
-                        try {
-                            externalizable = (Externalizable) clazz.newInstance();
-                        } catch (InstantiationException e) {
-                            throw newInvalidClassException(clazz, e);
-                        } catch (IllegalAccessException e) {
-                            throw newInvalidClassException(clazz, e);
-                        }
-                        registerObject(handle, externalizable);
-                        externalizable.readExternal(this);
-                        return externalizable;
-                    }
                     case RT_STRING:
                         return readString(handle);
-                    case RT_MAP: {
-                        Class clazz = (Class) readObject();
-                        int size = dataInput.readInt();
-                        Map<Object, Object> map = (Map<Object, Object>) newCollection(handle, clazz, size);
-                        while (size-- > 0) {
-                            Object key = readObject();
-                            Object value = readObject();
-                            map.put(key, value);
-                        }
-                        return map;
-                    }
-                    case RT_COLLECTION: {
-                        Class clazz = (Class) readObject();
-                        int size = dataInput.readInt();
-                        Collection<Object> collection = (Collection<Object>) newCollection(handle, clazz, size);
-                        while (size-- > 0) {
-                            collection.add(readObject());
-                        }
-                        return collection;
-                    }
-                    case RT_ARRAY: {
-                        Class clazz = (Class) readObject();
-                        int length = dataInput.readInt();
-                        Class componentType = clazz.getComponentType();
-                        Object array = Array.newInstance(componentType, length);
-                        registerObject(handle, array);
-                        if (componentType.isPrimitive()) {
-                            readPrimitiveArray(array, length, componentType);
-                        } else {
-                            Object[] objects    = (Object[])array;
-                            for (int i = 0; i < length; ++i) {
-                                objects[i] = readObject();
-                            }
-                        }
-                        return array;
-                    }
                     case RT_CLASS:
                         return readClass(handle);
-                    case RT_REFERENCE:
-                        return readReference(handle);
                     case RT_ATOMICREFERENCEARRAY: {
                         int length  = dataInput.readInt();
                         AtomicReferenceArray<Object>    array   = new AtomicReferenceArray<Object>(length);
@@ -289,11 +299,6 @@
                         }
                         return array;
                     }
-                    case RT_SERIALIZABLE: {
-                        Object  object  =  dataInput.readObject();
-                        registerObject(handle, object);
-                        return object;
-                    }
                     default:
                         throw new StreamCorruptedException("Unsupported object type: " + type);
                 }




More information about the jboss-svn-commits mailing list