~ubuntu-branches/ubuntu/saucy/apache-mime4j/saucy

« back to all changes in this revision

Viewing changes to benchmarks/src/org/apache/james/mime4j/Base64InputStreamBench.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-07-13 09:28:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100713092828-g6wafdtidgmtx7su
Tags: upstream-0.6
ImportĀ upstreamĀ versionĀ 0.6

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
import java.io.OutputStream;
 
27
import java.util.Random;
 
28
 
 
29
import org.apache.commons.io.output.NullOutputStream;
 
30
import org.apache.james.mime4j.codec.Base64InputStream;
 
31
import org.apache.james.mime4j.codec.CodecUtil;
 
32
 
 
33
public class Base64InputStreamBench {
 
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 Base64InputStream(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 Base64InputStream(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.encodeBase64(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 Base64InputStream(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
}
 
 
b'\\ No newline at end of file'