001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.mime4j;
021
022 import java.io.OutputStream;
023 import java.util.Random;
024
025 import org.apache.commons.io.output.NullOutputStream;
026 import org.apache.james.mime4j.codec.Base64OutputStream;
027
028 public class Base64OutputStreamBench {
029
030 public static void main(String[] args) throws Exception {
031 byte[] data = initData(1024);
032
033 OutputStream nullOut = new NullOutputStream();
034 Base64OutputStream base64Out = new Base64OutputStream(nullOut);
035
036 // warmup
037
038 for (int i = 0; i < 2000; i++) {
039 base64Out.write(data);
040 }
041 Thread.sleep(100);
042
043 // test
044
045 long t0 = System.currentTimeMillis();
046
047 final int repetitions = 500000;
048 for (int i = 0; i < repetitions; i++) {
049 base64Out.write(data);
050 }
051 base64Out.close();
052
053 long dt = System.currentTimeMillis() - t0;
054 long totalBytes = data.length * (long) repetitions;
055
056 double mbPerSec = (totalBytes / 1024.0 / 1024) / (dt / 1000.0);
057
058 System.out.println(dt + " ms");
059 System.out.println(totalBytes + " bytes");
060 System.out.println(mbPerSec + " mb/sec");
061 }
062
063 private static byte[] initData(int size) {
064 Random random = new Random(size);
065 byte[] data = new byte[size];
066 random.nextBytes(data);
067 return data;
068 }
069
070 }