    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.helpers;
    026
    027import java.text.MessageFormat;
    028
    029import org.junit.Ignore;
    030import org.junit.Test;
    031
    032@Ignore
    033public class MessageFormatterPerfTest { // extends TestCase {
    034
    035    Integer i1 = new Integer(1);
    036    Integer i2 = new Integer(2);
    037    static long RUN_LENGTH = 100 * 1000;
    038    //
    039    static long REFERENCE_BIPS = 48416;
    040
    041    public void XtestJDKFormatterPerf() {
    042        jdkMessageFormatter(RUN_LENGTH);
    043        double duration = jdkMessageFormatter(RUN_LENGTH);
    044        System.out.println("jdk duration = " + duration + " nanos");
    045    }
    046
    047    @Test
    048    public void testSLF4JPerf_OneArg() {
    049        slf4jMessageFormatter_OneArg(RUN_LENGTH);
    050        double duration = slf4jMessageFormatter_OneArg(RUN_LENGTH);
    051        System.out.println("duration=" + duration);
    052        long referencePerf = 36;
    053        BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
    054    }
    055
    056    @Test
    057    public void testSLF4JPerf_TwoArg() {
    058        slf4jMessageFormatter_TwoArg(RUN_LENGTH);
    059        double duration = slf4jMessageFormatter_TwoArg(RUN_LENGTH);
    060        long referencePerf = 60;
    061        BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
    062    }
    063
    064    public double slf4jMessageFormatter_OneArg(long len) {
    065        long start = System.nanoTime();
    066        for (int i = 0; i < len; i++) {
    067            final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
    068            tp.getMessage();
    069            tp.getArgArray();
    070            tp.getThrowable();
    071
    072            MessageFormatter.format("This is some rather short message {} ", i1);
    073        }
    074        long end = System.nanoTime();
    075        return (end - start) / (1000 * 1000.0);
    076    }
    077
    078    public double slf4jMessageFormatter_TwoArg(long len) {
    079        long start = System.nanoTime();
    080        for (int i = 0; i < len; i++) {
    081            final FormattingTuple tp = MessageFormatter.format("This is some {} short message {} ", i1, i2);
    082            tp.getMessage();
    083            tp.getArgArray();
    084            tp.getThrowable();
    085        }
    086        long end = System.nanoTime();
    087        return (end - start) / (1000 * 1000.0);
    088    }
    089
    090    public double jdkMessageFormatter(long len) {
    091        @SuppressWarnings("unused")
    092        String s = "";
    093        s += ""; // keep compiler happy
    094        long start = System.currentTimeMillis();
    095        Object[] oa = new Object[] { i1 };
    096        for (int i = 0; i < len; i++) {
    097            s = MessageFormat.format("This is some rather short message {0}", oa);
    098        }
    099        long end = System.currentTimeMillis();
    100        return (1.0 * end - start);
    101    }
    102
    103}