source: contrib/MailArchiver/sources/vendor/mime4j/custom/benchmark/src/main/java/org/apache/james/mime4j/QuotedPrintableInputStreamBench.java @ 6785

Revision 6785, 4.0 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.OutputStream;
27import java.util.Random;
28
29import org.apache.commons.io.output.NullOutputStream;
30import org.apache.james.mime4j.codec.CodecUtil;
31import org.apache.james.mime4j.codec.QuotedPrintableInputStream;
32
33public class QuotedPrintableInputStreamBench {
34
35    public static void main(String[] args) throws Exception {
36        byte[] data = initData(2 * 1024 * 1024);
37        byte[] encoded = encode(data);
38
39        // decoder test to make sure everything is okay
40
41        testDecode(data, encoded);
42
43        // warmup
44
45        OutputStream nullOut = new NullOutputStream();
46
47        for (int i = 0; i < 5; i++) {
48            ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
49            InputStream in = new QuotedPrintableInputStream(ed);
50            CodecUtil.copy(in, nullOut);
51        }
52        Thread.sleep(100);
53
54        // test
55
56        long t0 = System.currentTimeMillis();
57
58        final int repetitions = 50;
59        for (int i = 0; i < repetitions; i++) {
60            ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
61            InputStream in = new QuotedPrintableInputStream(ed);
62            CodecUtil.copy(in, nullOut);
63        }
64
65        long dt = System.currentTimeMillis() - t0;
66        long totalBytes = data.length * (long) repetitions;
67
68        double mbPerSec = (totalBytes / 1024.0 / 1024) / (dt / 1000.0);
69
70        System.out.println(dt + " ms");
71        System.out.println(totalBytes + " bytes");
72        System.out.println(mbPerSec + " mb/sec");
73    }
74
75    private static byte[] initData(int size) {
76        Random random = new Random(size);
77        byte[] data = new byte[size];
78        random.nextBytes(data);
79        return data;
80    }
81
82    private static byte[] encode(byte[] data) throws IOException {
83        InputStream in = new ByteArrayInputStream(data);
84        ByteArrayOutputStream out = new ByteArrayOutputStream();
85        CodecUtil.encodeQuotedPrintableBinary(in, out);
86        return out.toByteArray();
87    }
88
89    private static void testDecode(byte[] data, final byte[] encoded)
90            throws IOException {
91        ByteArrayInputStream ed = new ByteArrayInputStream(encoded);
92        InputStream in = new QuotedPrintableInputStream(ed);
93        ByteArrayOutputStream out = new ByteArrayOutputStream();
94        CodecUtil.copy(in, out);
95
96        compare(data, out.toByteArray());
97    }
98
99    private static void compare(byte[] expected, byte[] actual) {
100        if (expected.length != actual.length)
101            throw new AssertionError("length: " + expected.length + ", "
102                    + actual.length);
103
104        for (int i = 0; i < expected.length; i++)
105            if (expected[i] != actual[i])
106                throw new AssertionError("value @ " + i);
107    }
108
109}
Note: See TracBrowser for help on using the repository browser.