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.message.MimeBuilder; |
---|
30 | import org.apache.james.mime4j.message.SimpleContentHandler; |
---|
31 | import org.apache.james.mime4j.parser.AbstractContentHandler; |
---|
32 | import org.apache.james.mime4j.parser.ContentHandler; |
---|
33 | import org.apache.james.mime4j.parser.MimeStreamParser; |
---|
34 | import org.apache.james.mime4j.storage.DefaultStorageProvider; |
---|
35 | import org.apache.james.mime4j.storage.MemoryStorageProvider; |
---|
36 | import org.apache.james.mime4j.stream.BodyDescriptor; |
---|
37 | import org.apache.james.mime4j.stream.EntityState; |
---|
38 | import org.apache.james.mime4j.stream.MimeTokenStream; |
---|
39 | |
---|
40 | public class LongMultipartReadBench { |
---|
41 | |
---|
42 | public static void main(String[] args) throws Exception { |
---|
43 | |
---|
44 | byte[] content = loadMessage("long-multipart.msg"); |
---|
45 | if (content == null) { |
---|
46 | System.err.println("Test message not found"); |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | int testNumber = args.length > 0 ? Integer.parseInt(args[0]) : 0; |
---|
51 | |
---|
52 | Test test = createTest(testNumber); |
---|
53 | if (test == null) { |
---|
54 | System.err.println("No such test: " + testNumber); |
---|
55 | return; |
---|
56 | } |
---|
57 | |
---|
58 | int repetitions = args.length > 1 ? Integer.parseInt(args[1]) : 25000; |
---|
59 | |
---|
60 | System.out.println("Multipart message read."); |
---|
61 | System.out.println("No of repetitions: " + repetitions); |
---|
62 | System.out.println("Content length: " + content.length); |
---|
63 | System.out.println("Test: " + test.getClass().getSimpleName()); |
---|
64 | |
---|
65 | System.out.print("Warmup... "); |
---|
66 | long t0 = System.currentTimeMillis(); |
---|
67 | while (System.currentTimeMillis() - t0 < 1500) { |
---|
68 | test.run(content, 10); |
---|
69 | } |
---|
70 | System.out.println("done"); |
---|
71 | |
---|
72 | System.out.println("--------------------------------"); |
---|
73 | |
---|
74 | long start = System.currentTimeMillis(); |
---|
75 | test.run(content, repetitions); |
---|
76 | long finish = System.currentTimeMillis(); |
---|
77 | |
---|
78 | double seconds = (finish - start) / 1000.0; |
---|
79 | double mb = content.length * repetitions / 1024.0 / 1024; |
---|
80 | System.out.printf("Execution time: %f sec\n", seconds); |
---|
81 | System.out.printf("%.2f messages/sec\n", repetitions / seconds); |
---|
82 | System.out.printf("%.2f mb/sec\n", mb / seconds); |
---|
83 | } |
---|
84 | |
---|
85 | private static Test createTest(int testNumber) { |
---|
86 | switch (testNumber) { |
---|
87 | case 0: |
---|
88 | return new MimeTokenStreamTest(); |
---|
89 | case 1: |
---|
90 | return new AbstractContentHandlerTest(); |
---|
91 | case 2: |
---|
92 | return new SimpleContentHandlerTest(); |
---|
93 | case 3: |
---|
94 | return new MessageTest(); |
---|
95 | default: |
---|
96 | return null; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | private static byte[] loadMessage(String resourceName) throws IOException { |
---|
101 | ClassLoader cl = LongMultipartReadBench.class.getClassLoader(); |
---|
102 | |
---|
103 | ByteArrayOutputStream outstream = new ByteArrayOutputStream(); |
---|
104 | InputStream instream = cl.getResourceAsStream(resourceName); |
---|
105 | if (instream == null) { |
---|
106 | return null; |
---|
107 | } |
---|
108 | try { |
---|
109 | CodecUtil.copy(instream, outstream); |
---|
110 | } finally { |
---|
111 | instream.close(); |
---|
112 | } |
---|
113 | |
---|
114 | return outstream.toByteArray(); |
---|
115 | } |
---|
116 | |
---|
117 | private interface Test { |
---|
118 | void run(byte[] content, int repetitions) throws Exception; |
---|
119 | } |
---|
120 | |
---|
121 | private static final class MimeTokenStreamTest implements Test { |
---|
122 | public void run(byte[] content, int repetitions) throws Exception { |
---|
123 | MimeTokenStream stream = new MimeTokenStream(); |
---|
124 | for (int i = 0; i < repetitions; i++) { |
---|
125 | stream.parse(new ByteArrayInputStream(content)); |
---|
126 | for (EntityState state = stream.getState(); state != EntityState.T_END_OF_STREAM; state = stream |
---|
127 | .next()) { |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | private static final class AbstractContentHandlerTest implements Test { |
---|
134 | public void run(byte[] content, int repetitions) throws Exception { |
---|
135 | ContentHandler contentHandler = new AbstractContentHandler() { |
---|
136 | }; |
---|
137 | |
---|
138 | for (int i = 0; i < repetitions; i++) { |
---|
139 | MimeStreamParser parser = new MimeStreamParser(); |
---|
140 | parser.setContentHandler(contentHandler); |
---|
141 | parser.parse(new ByteArrayInputStream(content)); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | private static final class SimpleContentHandlerTest implements Test { |
---|
147 | public void run(byte[] content, int repetitions) throws Exception { |
---|
148 | ContentHandler contentHandler = new SimpleContentHandler() { |
---|
149 | @Override |
---|
150 | public void body(BodyDescriptor bd, InputStream is) |
---|
151 | throws IOException { |
---|
152 | byte[] b = new byte[4096]; |
---|
153 | while (is.read(b) != -1); |
---|
154 | } |
---|
155 | |
---|
156 | @Override |
---|
157 | public void headers(Header header) { |
---|
158 | } |
---|
159 | }; |
---|
160 | |
---|
161 | for (int i = 0; i < repetitions; i++) { |
---|
162 | MimeStreamParser parser = new MimeStreamParser(); |
---|
163 | parser.setContentDecoding(true); |
---|
164 | parser.setContentHandler(contentHandler); |
---|
165 | parser.parse(new ByteArrayInputStream(content)); |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | private static final class MessageTest implements Test { |
---|
171 | public void run(byte[] content, int repetitions) throws Exception { |
---|
172 | DefaultStorageProvider.setInstance(new MemoryStorageProvider()); |
---|
173 | |
---|
174 | for (int i = 0; i < repetitions; i++) { |
---|
175 | MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(content)); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | /* |
---|
181 | // requires mail.jar and activation.jar to be present |
---|
182 | private static final class MimeMessageTest implements Test { |
---|
183 | public void run(byte[] content, int repetitions) throws Exception { |
---|
184 | for (int i = 0; i < repetitions; i++) { |
---|
185 | MimeMessage mm = new MimeMessage(null, new ByteArrayInputStream(content)); |
---|
186 | Multipart multipart = (Multipart) mm.getContent(); |
---|
187 | multipart.getCount(); // force parsing |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | */ |
---|
192 | |
---|
193 | } |
---|