inti.core.codec.base64.Base64OutputStreamPerformanceTest.java Source code

Java tutorial

Introduction

Here is the source code for inti.core.codec.base64.Base64OutputStreamPerformanceTest.java

Source

/**
 * Copyright 2012 the project-owners
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package inti.core.codec.base64;

import inti.core.codec.base64.Base64OutputStream;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;
import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class Base64OutputStreamPerformanceTest {

    Base64OutputStream<Object> output = new Base64OutputStream<Object>();

    @Test
    public void writePerformance_Message10M() throws Exception {
        writePerformance(100, 10000000);
    }

    @Test
    public void writePerformance_Message1M() throws Exception {
        writePerformance(1000, 1000000);
    }

    @Test
    public void writePerformance_Message100K() throws Exception {
        writePerformance(10000, 100000);
    }

    @Test
    public void writePerformance_Message10K() throws Exception {
        writePerformance(100000, 10000);
    }

    @Test
    public void writePerformance_Message1K() throws Exception {
        writePerformance(1000000, 1000);
    }

    @Test
    public void writePerformance_Message100B() throws Exception {
        writePerformance(10000000, 100);
    }

    @Test
    public void writePerformance_Message10B() throws Exception {
        writePerformance(10000000, 10);
    }

    public void writePerformance(int count, int bufferSize) throws Exception {
        ByteArrayOutputStream underlying = new ByteArrayOutputStream(bufferSize);
        byte[] data;
        long start, end;
        Base64 b64 = new Base64();
        StringBuilder message = new StringBuilder();

        for (int i = 0; i < bufferSize / 10; i++) {
            message.append("0123456789");
        }
        data = message.toString().getBytes(Charset.forName("utf-8"));

        output.reset(underlying, null);

        for (int i = 0; i < 10; i++) {
            underlying.reset();
            output.write(data);
            output.flush();
        }

        for (int i = 0; i < 100; i++) {
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            underlying.reset();
            output.write(data);
            output.flush();
        }
        end = System.currentTimeMillis();

        System.out.format("writePerformance(Inti) - size: %d, duration: %dms, ratio: %#.4f", bufferSize,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();

        for (int i = 0; i < 10; i++) {
            b64.encode(data);
        }

        for (int i = 0; i < 100; i++) {
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            b64.encode(data);
        }
        end = System.currentTimeMillis();
        System.out.format("writePerformance(commons.codec) - size: %d, duration: %dms, ratio: %#.4f", bufferSize,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();
    }

}