james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r731074 - /james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java
Date Sat, 03 Jan 2009 19:53:43 GMT
Author: mwiederkehr
Date: Sat Jan  3 11:53:43 2009
New Revision: 731074

URL: http://svn.apache.org/viewvc?rev=731074&view=rev
Log:
Added more benchmark tests in addition to the existing MimeTokenStream test: a test that uses
an empty AbstractContentHandler, one that uses an empty SimpleContentHandler and one that
creates a Message object.

Modified:
    james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java

Modified: james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java?rev=731074&r1=731073&r2=731074&view=diff
==============================================================================
--- james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java
(original)
+++ james/mime4j/trunk/benchmarks/src/org/apache/james/mime4j/LongMultipartReadBench.java
Sat Jan  3 11:53:43 2009
@@ -21,22 +21,77 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.james.mime4j.decoder.CodecUtil;
+import org.apache.james.mime4j.descriptor.BodyDescriptor;
+import org.apache.james.mime4j.message.Header;
+import org.apache.james.mime4j.message.Message;
+import org.apache.james.mime4j.message.SimpleContentHandler;
+import org.apache.james.mime4j.message.storage.DefaultStorageProvider;
+import org.apache.james.mime4j.message.storage.MemoryStorageProvider;
+import org.apache.james.mime4j.parser.AbstractContentHandler;
+import org.apache.james.mime4j.parser.ContentHandler;
+import org.apache.james.mime4j.parser.MimeStreamParser;
 import org.apache.james.mime4j.parser.MimeTokenStream;
 
 public class LongMultipartReadBench {
 
     public static void main(String[] args) throws Exception {
-        
+
+        byte[] content = loadMessage("long-multipart.msg");
+        if (content == null) {
+            System.err.println("Test message not found");
+            return;
+        }
+
+        int testNumber = args.length > 0 ? Integer.parseInt(args[0]) : 0;
+
+        Test test = createTest(testNumber);
+        if (test == null) {
+            System.err.println("No such test: " + testNumber);
+            return;
+        }
+
+        int repetitions = args.length > 1 ? Integer.parseInt(args[1]) : 25000;
+
+        System.out.println("Multipart message read.");
+        System.out.println("No of repetitions: " + repetitions);
+        System.out.println("Content length: " + content.length);
+        System.out.println("Test: " + test.getClass().getSimpleName());
+        System.out.println("--------------------------------");
+
+        long start = System.currentTimeMillis();
+        test.run(content, repetitions);
+        long finish = System.currentTimeMillis();
+
+        System.out.println("Execution time: "
+                + ((double) (finish - start) / 1000) + " s");
+    }
+
+    private static Test createTest(int testNumber) {
+        switch (testNumber) {
+        case 0:
+            return new MimeTokenStreamTest();
+        case 1:
+            return new AbstractContentHandlerTest();
+        case 2:
+            return new SimpleContentHandlerTest();
+        case 3:
+            return new MessageTest();
+        default:
+            return null;
+        }
+    }
+
+    private static byte[] loadMessage(String resourceName) throws IOException {
         ClassLoader cl = LongMultipartReadBench.class.getClassLoader();
-        
+
         ByteArrayOutputStream outstream = new ByteArrayOutputStream();
-        InputStream instream = cl.getResourceAsStream("long-multipart.msg");
+        InputStream instream = cl.getResourceAsStream(resourceName);
         if (instream == null) {
-            System.out.println("Test message not found");
-            return;
+            return null;
         }
         try {
             CodecUtil.copy(instream, outstream);
@@ -44,30 +99,80 @@
             instream.close();
         }
 
-        byte[] content = outstream.toByteArray();
+        return outstream.toByteArray();
+    }
+
+    private interface Test {
+        void run(byte[] content, int repetitions) throws Exception;
+    }
+
+    private static final class MimeTokenStreamTest implements Test {
+        public void run(byte[] content, int repetitions) throws Exception {
+            MimeTokenStream stream = new MimeTokenStream();
+            for (int i = 0; i < repetitions; i++) {
+                stream.parse(new ByteArrayInputStream(content));
+                for (int state = stream.getState(); state != MimeTokenStream.T_END_OF_STREAM;
state = stream
+                        .next()) {
+                }
+            }
+        }
+    }
+
+    private static final class AbstractContentHandlerTest implements Test {
+        public void run(byte[] content, int repetitions) throws Exception {
+            ContentHandler contentHandler = new AbstractContentHandler() {
+            };
+
+            for (int i = 0; i < repetitions; i++) {
+                MimeStreamParser parser = new MimeStreamParser();
+                parser.setContentHandler(contentHandler);
+                parser.parse(new ByteArrayInputStream(content));
+            }
+        }
+    }
 
-        int reps = 25000;
-        if (args.length > 0) {
-            reps = Integer.parseInt(args[0]);
+    private static final class SimpleContentHandlerTest implements Test {
+        public void run(byte[] content, int repetitions) throws Exception {
+            ContentHandler contentHandler = new SimpleContentHandler() {
+                @Override
+                public void bodyDecoded(BodyDescriptor bd, InputStream is)
+                        throws IOException {
+                }
+
+                @Override
+                public void headers(Header header) {
+                }
+            };
+
+            for (int i = 0; i < repetitions; i++) {
+                MimeStreamParser parser = new MimeStreamParser();
+                parser.setContentHandler(contentHandler);
+                parser.parse(new ByteArrayInputStream(content));
+            }
         }
-        
-        System.out.println("Multipart message read.");
-        System.out.println("No of repetitions: " + reps);
-        System.out.println("Content length: " + content.length);
-        System.out.println("----------------------------");
-        
-        MimeTokenStream stream = new MimeTokenStream();
-        long start = System.currentTimeMillis();
-        for (int i = 0; i < reps; i++) {
-            stream.parse(new ByteArrayInputStream(content));
-            for (int state = stream.getState(); 
-                state != MimeTokenStream.T_END_OF_STREAM; 
-                state = stream.next()) {
+    }
+
+    private static final class MessageTest implements Test {
+        public void run(byte[] content, int repetitions) throws Exception {
+            DefaultStorageProvider.setInstance(new MemoryStorageProvider());
+
+            for (int i = 0; i < repetitions; i++) {
+                new Message(new ByteArrayInputStream(content));
             }
         }
-        long finish = System.currentTimeMillis();
-        System.out.println("Execution time: " 
-                + ((double)(finish - start) / 1000) + " s" );
     }
-    
+
+    /*
+    // requires mail.jar and activation.jar to be present
+    private static final class MimeMessageTest implements Test {
+        public void run(byte[] content, int repetitions) throws Exception {
+            for (int i = 0; i < repetitions; i++) {
+                MimeMessage mm = new MimeMessage(null, new ByteArrayInputStream(content));
+                Multipart multipart = (Multipart) mm.getContent();
+                multipart.getCount(); // force parsing
+            }
+        }
+    }
+    */
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message