/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.logging.log4j.message;

import org.apache.logging.log4j.Timer;
import org.junit.AfterClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 *
 */
public class MessageFormatsPerfTest {

    private static final int LOOP_CNT = 500;
    String[] array = new String[LOOP_CNT];
    private static long stringTime = 0;
    private static long paramTime = 0;
    private static long msgFormatTime = 0;

    @AfterClass
    public static void after() {
        if (stringTime > paramTime) {
            System.out.println(String.format("Parameterized is %1$.2f times faster than StringFormat.",
                ((float) stringTime / paramTime)));
        } else if (stringTime > paramTime) {
            System.out.println(String.format("Parameterized is %1$.2f times slower than StringFormat.",
                ((float) paramTime / stringTime)));
        } else {
            System.out.println("The speed of Parameterized and StringFormat are the same");
        }
        if (msgFormatTime > paramTime) {
            System.out.println(String.format("Parameterized is %1$.2f times faster than MessageFormat.",
                ((float) msgFormatTime / paramTime)));
        } else if (msgFormatTime > paramTime) {
            System.out.println(String.format("Parameterized is %1$.2f times slower than MessageFormat.",
                ((float) paramTime / msgFormatTime)));
        } else {
            System.out.println("The speed of Parameterized and MessageFormat are the same");
        }
    }

    @Test
    public void testStringPerf() {
        String testMsg = "Test message %1s %2s";
        Timer timer = new Timer("StringFormat", LOOP_CNT);
        timer.start();
        for (int i = 0; i < LOOP_CNT; ++i) {
            StringFormattedMessage msg = new StringFormattedMessage(testMsg, "Apache", "Log4j");
            array[i] = msg.getFormattedMessage();
        }
        timer.stop();
        stringTime = timer.getElapsedNanoTime();
        System.out.println(timer.toString());
    }

    @Test
    public void testMessageFormatPerf() {
        String testMsg = "Test message {0} {1}";
        Timer timer = new Timer("MessageFormat", LOOP_CNT);
        timer.start();
        for (int i = 0; i < LOOP_CNT; ++i) {
            MessageFormatMessage msg = new MessageFormatMessage(testMsg, "Apache", "Log4j");
            array[i] = msg.getFormattedMessage();
        }
        timer.stop();
        msgFormatTime = timer.getElapsedNanoTime();
        System.out.println(timer.toString());
    }

    @Test
    public void testParameterizedPerf() {
        String testMsg = "Test message {} {}";
        Timer timer = new Timer("Parameterized", LOOP_CNT);
        timer.start();
        for (int i = 0; i < LOOP_CNT; ++i) {
            ParameterizedMessage msg = new ParameterizedMessage(testMsg, "Apache", "Log4j");
            array[i] = msg.getFormattedMessage();
        }
        timer.stop();
        paramTime = timer.getElapsedNanoTime();
        System.out.println(timer.toString());
    }
}

@@ -1234,16 +1234,59 @@
 ime = 0;
+%0A    private static long formattedTime = 0;
 %0A%0A    @A
@@ -1544,33 +1544,33 @@
  if (stringTime 
-%3E
+%3C
  paramTime) %7B%0A  
@@ -2068,33 +2068,33 @@
  (msgFormatTime 
-%3E
+%3C
  paramTime) %7B%0A  
@@ -2367,24 +2367,541 @@
 ;%0A        %7D%0A
+        if (formattedTime %3E paramTime) %7B%0A            System.out.println(String.format(%22Parameterized is %251$.2f times faster than Formatted.%22,%0A                ((float) formattedTime / paramTime)));%0A        %7D else if (formattedTime %3C paramTime) %7B%0A            System.out.println(String.format(%22Parameterized is %251$.2f times slower than Formatted.%22,%0A                ((float) paramTime / formattedTime)));%0A        %7D else %7B%0A            System.out.println(%22The speed of Parameterized and Formatted are the same%22);%0A        %7D%0A
     %7D%0A%0A    @
@@ -4300,32 +4300,32 @@
   timer.stop();%0A
-
         paramTim
@@ -4407,11 +4407,528 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testFormattedParameterizedPerf() %7B%0A        String testMsg = %22Test message %7B%7D %7B%7D%22;%0A        Timer timer = new Timer(%22FormattedParameterized%22, LOOP_CNT);%0A        timer.start();%0A        for (int i = 0; i %3C LOOP_CNT; ++i) %7B%0A            FormattedMessage msg = new FormattedMessage(testMsg, %22Apache%22, %22Log4j%22);%0A            array%5Bi%5D = msg.getFormattedMessage();%0A        %7D%0A        timer.stop();%0A        formattedTime = timer.getElapsedNanoTime();%0A        System.out.println(timer.toString());%0A    %7D
 %0A%7D%0A

Add FormattedMessage to performance test