[6785] | 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.OutputStream; |
---|
| 23 | import java.util.Random; |
---|
| 24 | |
---|
| 25 | import org.apache.commons.io.output.NullOutputStream; |
---|
| 26 | import org.apache.james.mime4j.codec.Base64OutputStream; |
---|
| 27 | |
---|
| 28 | public class Base64OutputStreamBench { |
---|
| 29 | |
---|
| 30 | public static void main(String[] args) throws Exception { |
---|
| 31 | byte[] data = initData(1024); |
---|
| 32 | |
---|
| 33 | OutputStream nullOut = new NullOutputStream(); |
---|
| 34 | Base64OutputStream base64Out = new Base64OutputStream(nullOut); |
---|
| 35 | |
---|
| 36 | // warmup |
---|
| 37 | |
---|
| 38 | for (int i = 0; i < 2000; i++) { |
---|
| 39 | base64Out.write(data); |
---|
| 40 | } |
---|
| 41 | Thread.sleep(100); |
---|
| 42 | |
---|
| 43 | // test |
---|
| 44 | |
---|
| 45 | long t0 = System.currentTimeMillis(); |
---|
| 46 | |
---|
| 47 | final int repetitions = 500000; |
---|
| 48 | for (int i = 0; i < repetitions; i++) { |
---|
| 49 | base64Out.write(data); |
---|
| 50 | } |
---|
| 51 | base64Out.close(); |
---|
| 52 | |
---|
| 53 | long dt = System.currentTimeMillis() - t0; |
---|
| 54 | long totalBytes = data.length * (long) repetitions; |
---|
| 55 | |
---|
| 56 | double mbPerSec = (totalBytes / 1024.0 / 1024) / (dt / 1000.0); |
---|
| 57 | |
---|
| 58 | System.out.println(dt + " ms"); |
---|
| 59 | System.out.println(totalBytes + " bytes"); |
---|
| 60 | System.out.println(mbPerSec + " mb/sec"); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | private static byte[] initData(int size) { |
---|
| 64 | Random random = new Random(size); |
---|
| 65 | byte[] data = new byte[size]; |
---|
| 66 | random.nextBytes(data); |
---|
| 67 | return data; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | } |
---|