inti.util.DateFormatterPerformanceTest.java Source code

Java tutorial

Introduction

Here is the source code for inti.util.DateFormatterPerformanceTest.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.util;

import inti.util.DateFormatter.Format;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.time.FastDateFormat;
import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class DateFormatterPerformanceTest {

    @Test
    public void format() throws Exception {
        for (int i = 0; i < 5; i++) {
            format(10000000);
            System.out.println();
        }
    }

    public void format(int count) throws Exception {
        Date date = new Date();
        DateFormatter dateFormatter = new DateFormatter();
        FastDateFormat fastDateFormat = FastDateFormat.getInstance("EEE, dd-MMM-yyyy hh:mm:ss 'GMT'");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss 'GMT'");
        StringBuilder builder = new StringBuilder();
        long start, end;
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                dateFormatter.formatDate(date.getTime(), Format.RFC1123, builder, cal);
                builder.delete(0, builder.length());
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            dateFormatter.formatDate(date.getTime(), Format.RFC1123, builder, cal);
            builder.delete(0, builder.length());
        }
        end = System.currentTimeMillis();
        System.out.format("format(DateFormatter-special) - count: %d, duration: %dms, ratio: %#.4f", count,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                dateFormatter.formatDate(date.getTime());
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            dateFormatter.formatDate(date.getTime());
        }
        end = System.currentTimeMillis();
        System.out.format("format(DateFormatter-simple) - count: %d, duration: %dms, ratio: %#.4f", count,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                fastDateFormat.format(date);
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            fastDateFormat.format(date);
        }
        end = System.currentTimeMillis();
        System.out.format("format(FastDateFormat) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
                count / ((end - start) / 1000.0));
        System.out.println();

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

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            simpleDateFormat.format(date);
        }
        end = System.currentTimeMillis();
        System.out.format("format(SimpleDateFormat) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
                count / ((end - start) / 1000.0));
        System.out.println();
    }

    @Test
    public void parse() throws Exception {
        for (int i = 0; i < 5; i++) {
            parse(1000000);
            System.out.println();
        }
    }

    public void parse(int count) throws Exception {
        DateFormatter dateFormatter = new DateFormatter();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss 'GMT'");
        String date = simpleDateFormat.format(new Date());
        long start, end;
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                dateFormatter.parseDate(date, cal);
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            dateFormatter.parseDate(date, cal);
        }
        end = System.currentTimeMillis();
        System.out.format("parse(DateFormatter-date) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
                count / ((end - start) / 1000.0));
        System.out.println();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                dateFormatter.parseDate(date, cal);
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            dateFormatter.parseDate(date, cal);
        }
        end = System.currentTimeMillis();
        System.out.format("parse(DateFormatter-special) - count: %d, duration: %dms, ratio: %#.4f", count,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                dateFormatter.parseDate(date);
            }
            Thread.sleep(10);
        }

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            dateFormatter.parseDate(date);
        }
        end = System.currentTimeMillis();
        System.out.format("parse(DateFormatter-simple) - count: %d, duration: %dms, ratio: %#.4f", count,
                end - start, count / ((end - start) / 1000.0));
        System.out.println();

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

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            simpleDateFormat.parse(date);
        }
        end = System.currentTimeMillis();
        System.out.format("parse(SimpleDateFormat) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
                count / ((end - start) / 1000.0));
        System.out.println();
    }

}