Package org.apache.logging.log4j

Source Code of org.apache.logging.log4j.ReflectionComparison

/*
* 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;

import org.apache.logging.log4j.core.Timer;
import org.apache.logging.log4j.core.helpers.Loader;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.message.StringFormattedMessage;
import org.junit.BeforeClass;
import org.junit.Test;
import sun.reflect.Reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Tests the cost of invoking Reflection.getCallerClass via reflection vs calling it directly.
*/
public class ReflectionComparison {

    private static Method getCallerClass;

    private static final int COUNT = 1000000;

    private static Class[] paramTypes = new Class[] {String.class, Object[].class};

    @BeforeClass
    public static void setupCallerCheck() {
        try {
            ClassLoader loader = Loader.getClassLoader();
            Class clazz = loader.loadClass("sun.reflect.Reflection");
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                int modifier = method.getModifiers();
                if (method.getName().equals("getCallerClass") && Modifier.isStatic(modifier)) {
                    getCallerClass = method;
                    break;
                }
            }
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            throw  new RuntimeException(cnfe);
        }
    }

    @Test
    public void test1() {
        Timer timer = new Timer("Reflection", COUNT);
        timer.start();
        Object[] arr = new Object[1];
        arr[0] = 3;
        for (int i= 0; i < COUNT; ++i) {
            getCallerClass(arr);
        }
        timer.stop();
        System.out.println(timer.toString());
    }


    @Test
    public void test2() {
        Timer timer = new Timer("Reflection", COUNT);
        timer.start();
        for (int i= 0; i < COUNT; ++i) {

            Reflection.getCallerClass(3);
        }
        timer.stop();
        System.out.println(timer.toString());
    }


    @Test
    public void createObjects() throws Exception {
        Timer timer = new Timer("NewObject", COUNT);
        timer.start();
        Message msg;
        for (int i = 0; i < COUNT; ++i) {
            msg = new StringFormattedMessage("Hello %1", i);
        }
        timer.stop();
        System.out.println(timer.toString());
        Class<? extends Message> clazz = StringFormattedMessage.class;

        timer = new Timer("ReflectionObject", COUNT);
        timer.start();

        for (int i = 0; i < COUNT; ++i) {
            createMessage(clazz, "Hello %1", i);
        }
        timer.stop();
        System.out.println(timer.toString());
    }

    private Class getCallerClass(Object[] array) {
        if (getCallerClass != null) {
            try {
                /*Object[] params = new Object[]{index}; */
                return (Class) getCallerClass.invoke(null, array);
            } catch (Exception ex) {
                fail(ex.getMessage());
                // logger.debug("Unable to determine caller class via Sun Reflection", ex);
            }
        }
        return null;
    }

    private Message createMessage(Class<? extends Message> clazz, String msg, Object... params) throws Exception {
        Constructor constructor = clazz.getConstructor(paramTypes);
        return (Message) constructor.newInstance(msg, params);
    }

}
TOP

Related Classes of org.apache.logging.log4j.ReflectionComparison

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.