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

Java tutorial

Introduction

Here is the source code for inti.core.codec.base64.Base64InputStreamPerformanceTest.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.Base64InputStream;

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

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

@Ignore
public class Base64InputStreamPerformanceTest {

    Base64InputStream<Object> input = new Base64InputStream<Object>();

    @Test
    public void readPerformance_Message10B() throws Exception {
        readPerformance(10000000, 10);
    }

    @Test
    public void readPerformance_Message100B() throws Exception {
        readPerformance(10000000, 100);
    }

    @Test
    public void readPerformance_Message1K() throws Exception {
        readPerformance(1000000, 1000);
    }

    @Test
    public void readPerformance_Message10K() throws Exception {
        readPerformance(100000, 10000);
    }

    @Test
    public void readPerformance_Message100K() throws Exception {
        readPerformance(10000, 100000);
    }

    @Test
    public void readPerformance_Message1M() throws Exception {
        readPerformance(1000, 1000000);
    }

    @Test
    public void readPerformance_Message10M() throws Exception {
        readPerformance(100, 10000000);
    }

    @Test
    public void readPerformance_Message100M() throws Exception {
        readPerformance(10, 100000000);
    }

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

        for (int i = 0; i < bufferSize / 10; i++) {
            message.append("0123456789");
        }

        byte[] inputBytes = message.toString().getBytes(Charset.forName("utf-8"));

        inputData = new ByteArrayInputStream(inputBytes);

        for (int i = 0; i < 10; i++) {
            input.reset(inputData, null);
            inputData.reset();
            input.read(data);
        }

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

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            input.reset(inputData, null);
            inputData.reset();
            input.read(data);
        }
        end = System.currentTimeMillis();
        System.out.format("readPerformance(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.decode(inputBytes);
        }

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

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

}