~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic

« back to all changes in this revision

Viewing changes to benchmark/src/main/java/org/apache/james/mime4j/LongMultipartReadBench.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************
 
2
 * Licensed to the Apache Software Foundation (ASF) under one   *
 
3
 * or more contributor license agreements.  See the NOTICE file *
 
4
 * distributed with this work for additional information        *
 
5
 * regarding copyright ownership.  The ASF licenses this file   *
 
6
 * to you under the Apache License, Version 2.0 (the            *
 
7
 * "License"); you may not use this file except in compliance   *
 
8
 * with the License.  You may obtain a copy of the License at   *
 
9
 *                                                              *
 
10
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 
11
 *                                                              *
 
12
 * Unless required by applicable law or agreed to in writing,   *
 
13
 * software distributed under the License is distributed on an  *
 
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 
15
 * KIND, either express or implied.  See the License for the    *
 
16
 * specific language governing permissions and limitations      *
 
17
 * under the License.                                           *
 
18
 ****************************************************************/
 
19
 
 
20
package org.apache.james.mime4j;
 
21
 
 
22
import java.io.ByteArrayInputStream;
 
23
import java.io.ByteArrayOutputStream;
 
24
import java.io.IOException;
 
25
import java.io.InputStream;
 
26
 
 
27
import org.apache.james.mime4j.codec.CodecUtil;
 
28
import org.apache.james.mime4j.dom.Header;
 
29
import org.apache.james.mime4j.dom.MessageBuilder;
 
30
import org.apache.james.mime4j.message.DefaultMessageBuilder;
 
31
import org.apache.james.mime4j.message.SimpleContentHandler;
 
32
import org.apache.james.mime4j.parser.AbstractContentHandler;
 
33
import org.apache.james.mime4j.parser.ContentHandler;
 
34
import org.apache.james.mime4j.parser.MimeStreamParser;
 
35
import org.apache.james.mime4j.storage.DefaultStorageProvider;
 
36
import org.apache.james.mime4j.storage.MemoryStorageProvider;
 
37
import org.apache.james.mime4j.stream.BodyDescriptor;
 
38
import org.apache.james.mime4j.stream.EntityState;
 
39
import org.apache.james.mime4j.stream.MimeTokenStream;
 
40
 
 
41
public class LongMultipartReadBench {
 
42
 
 
43
    public static void main(String[] args) throws Exception {
 
44
 
 
45
        byte[] content = loadMessage("long-multipart.msg");
 
46
        if (content == null) {
 
47
            System.err.println("Test message not found");
 
48
            return;
 
49
        }
 
50
 
 
51
        int testNumber = args.length > 0 ? Integer.parseInt(args[0]) : 0;
 
52
 
 
53
        Test test = createTest(testNumber);
 
54
        if (test == null) {
 
55
            System.err.println("No such test: " + testNumber);
 
56
            return;
 
57
        }
 
58
 
 
59
        int repetitions = args.length > 1 ? Integer.parseInt(args[1]) : 25000;
 
60
 
 
61
        System.out.println("Multipart message read.");
 
62
        System.out.println("No of repetitions: " + repetitions);
 
63
        System.out.println("Content length: " + content.length);
 
64
        System.out.println("Test: " + test.getClass().getSimpleName());
 
65
 
 
66
        System.out.print("Warmup... ");
 
67
        long t0 = System.currentTimeMillis();
 
68
        while (System.currentTimeMillis() - t0 < 1500) {
 
69
            test.run(content, 10);
 
70
        }
 
71
        System.out.println("done");
 
72
 
 
73
        System.out.println("--------------------------------");
 
74
 
 
75
        long start = System.currentTimeMillis();
 
76
        test.run(content, repetitions);
 
77
        long finish = System.currentTimeMillis();
 
78
 
 
79
        double seconds = (finish - start) / 1000.0;
 
80
        double mb = content.length * repetitions / 1024.0 / 1024;
 
81
        System.out.printf("Execution time: %f sec\n", seconds);
 
82
        System.out.printf("%.2f messages/sec\n", repetitions / seconds);
 
83
        System.out.printf("%.2f mb/sec\n", mb / seconds);
 
84
    }
 
85
 
 
86
    private static Test createTest(int testNumber) {
 
87
        switch (testNumber) {
 
88
        case 0:
 
89
            return new MimeTokenStreamTest();
 
90
        case 1:
 
91
            return new AbstractContentHandlerTest();
 
92
        case 2:
 
93
            return new SimpleContentHandlerTest();
 
94
        case 3:
 
95
            return new MessageTest();
 
96
        default:
 
97
            return null;
 
98
        }
 
99
    }
 
100
 
 
101
    private static byte[] loadMessage(String resourceName) throws IOException {
 
102
        ClassLoader cl = LongMultipartReadBench.class.getClassLoader();
 
103
 
 
104
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
 
105
        InputStream instream = cl.getResourceAsStream(resourceName);
 
106
        if (instream == null) {
 
107
            return null;
 
108
        }
 
109
        try {
 
110
            CodecUtil.copy(instream, outstream);
 
111
        } finally {
 
112
            instream.close();
 
113
        }
 
114
 
 
115
        return outstream.toByteArray();
 
116
    }
 
117
 
 
118
    private interface Test {
 
119
        void run(byte[] content, int repetitions) throws Exception;
 
120
    }
 
121
 
 
122
    private static final class MimeTokenStreamTest implements Test {
 
123
        public void run(byte[] content, int repetitions) throws Exception {
 
124
            MimeTokenStream stream = new MimeTokenStream();
 
125
            for (int i = 0; i < repetitions; i++) {
 
126
                stream.parse(new ByteArrayInputStream(content));
 
127
                for (EntityState state = stream.getState(); state != EntityState.T_END_OF_STREAM; state = stream
 
128
                        .next()) {
 
129
                }
 
130
            }
 
131
        }
 
132
    }
 
133
 
 
134
    private static final class AbstractContentHandlerTest implements Test {
 
135
        public void run(byte[] content, int repetitions) throws Exception {
 
136
            ContentHandler contentHandler = new AbstractContentHandler() {
 
137
            };
 
138
 
 
139
            for (int i = 0; i < repetitions; i++) {
 
140
                MimeStreamParser parser = new MimeStreamParser();
 
141
                parser.setContentHandler(contentHandler);
 
142
                parser.parse(new ByteArrayInputStream(content));
 
143
            }
 
144
        }
 
145
    }
 
146
 
 
147
    private static final class SimpleContentHandlerTest implements Test {
 
148
        public void run(byte[] content, int repetitions) throws Exception {
 
149
            ContentHandler contentHandler = new SimpleContentHandler() {
 
150
                @Override
 
151
                public void body(BodyDescriptor bd, InputStream is)
 
152
                        throws IOException {
 
153
                    byte[] b = new byte[4096];
 
154
                    while (is.read(b) != -1);
 
155
                }
 
156
 
 
157
                @Override
 
158
                public void headers(Header header) {
 
159
                }
 
160
            };
 
161
 
 
162
            for (int i = 0; i < repetitions; i++) {
 
163
                MimeStreamParser parser = new MimeStreamParser();
 
164
                parser.setContentDecoding(true);
 
165
                parser.setContentHandler(contentHandler);
 
166
                parser.parse(new ByteArrayInputStream(content));
 
167
            }
 
168
        }
 
169
    }
 
170
 
 
171
    private static final class MessageTest implements Test {
 
172
        public void run(byte[] content, int repetitions) throws Exception {
 
173
            DefaultStorageProvider.setInstance(new MemoryStorageProvider());
 
174
            MessageBuilder builder = new DefaultMessageBuilder();
 
175
 
 
176
            for (int i = 0; i < repetitions; i++) {
 
177
                builder.parseMessage(new ByteArrayInputStream(content));
 
178
            }
 
179
        }
 
180
    }
 
181
 
 
182
    /*
 
183
    // requires mail.jar and activation.jar to be present
 
184
    private static final class MimeMessageTest implements Test {
 
185
        public void run(byte[] content, int repetitions) throws Exception {
 
186
            for (int i = 0; i < repetitions; i++) {
 
187
                MimeMessage mm = new MimeMessage(null, new ByteArrayInputStream(content));
 
188
                Multipart multipart = (Multipart) mm.getContent();
 
189
                multipart.getCount(); // force parsing
 
190
            }
 
191
        }
 
192
    }
 
193
    */
 
194
 
 
195
}