/projects/log4j-2.0-beta/core/src/test/java/org/apache/logging/log4j/ReflectionComparison.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 130 lines · 90 code · 19 blank · 21 comment · 9 complexity · 151c4810241566072711dcf39c367b64 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache license, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the license for the specific language governing permissions and
  15. * limitations under the license.
  16. */
  17. package org.apache.logging.log4j;
  18. import org.apache.logging.log4j.core.Timer;
  19. import org.apache.logging.log4j.core.helpers.Loader;
  20. import org.apache.logging.log4j.message.Message;
  21. import org.apache.logging.log4j.message.StringFormattedMessage;
  22. import org.junit.BeforeClass;
  23. import org.junit.Test;
  24. import sun.reflect.Reflection;
  25. import java.lang.reflect.Constructor;
  26. import java.lang.reflect.Method;
  27. import java.lang.reflect.Modifier;
  28. import static org.junit.Assert.fail;
  29. /**
  30. * Tests the cost of invoking Reflection.getCallerClass via reflection vs calling it directly.
  31. */
  32. public class ReflectionComparison {
  33. private static Method getCallerClass;
  34. private static final int COUNT = 1000000;
  35. private static Class[] paramTypes = new Class[] {String.class, Object[].class};
  36. @BeforeClass
  37. public static void setupCallerCheck() {
  38. try {
  39. final ClassLoader loader = Loader.getClassLoader();
  40. final Class clazz = loader.loadClass("sun.reflect.Reflection");
  41. final Method[] methods = clazz.getMethods();
  42. for (final Method method : methods) {
  43. final int modifier = method.getModifiers();
  44. if (method.getName().equals("getCallerClass") && Modifier.isStatic(modifier)) {
  45. getCallerClass = method;
  46. break;
  47. }
  48. }
  49. } catch (final ClassNotFoundException cnfe) {
  50. cnfe.printStackTrace();
  51. throw new RuntimeException(cnfe);
  52. }
  53. }
  54. @Test
  55. public void test1() {
  56. final Timer timer = new Timer("Reflection", COUNT);
  57. timer.start();
  58. final Object[] arr = new Object[1];
  59. arr[0] = 3;
  60. for (int i= 0; i < COUNT; ++i) {
  61. getCallerClass(arr);
  62. }
  63. timer.stop();
  64. System.out.println(timer.toString());
  65. }
  66. @Test
  67. public void test2() {
  68. final Timer timer = new Timer("Reflection", COUNT);
  69. timer.start();
  70. for (int i= 0; i < COUNT; ++i) {
  71. Reflection.getCallerClass(3);
  72. }
  73. timer.stop();
  74. System.out.println(timer.toString());
  75. }
  76. @Test
  77. public void createObjects() throws Exception {
  78. Timer timer = new Timer("NewObject", COUNT);
  79. timer.start();
  80. Message msg;
  81. for (int i = 0; i < COUNT; ++i) {
  82. msg = new StringFormattedMessage("Hello %1", i);
  83. }
  84. timer.stop();
  85. System.out.println(timer.toString());
  86. final Class<? extends Message> clazz = StringFormattedMessage.class;
  87. timer = new Timer("ReflectionObject", COUNT);
  88. timer.start();
  89. for (int i = 0; i < COUNT; ++i) {
  90. createMessage(clazz, "Hello %1", i);
  91. }
  92. timer.stop();
  93. System.out.println(timer.toString());
  94. }
  95. private Class getCallerClass(final Object[] array) {
  96. if (getCallerClass != null) {
  97. try {
  98. /*Object[] params = new Object[]{index}; */
  99. return (Class) getCallerClass.invoke(null, array);
  100. } catch (final Exception ex) {
  101. fail(ex.getMessage());
  102. // logger.debug("Unable to determine caller class via Sun Reflection", ex);
  103. }
  104. }
  105. return null;
  106. }
  107. private Message createMessage(final Class<? extends Message> clazz, final String msg, final Object... params) throws Exception {
  108. final Constructor<? extends Message> constructor = clazz.getConstructor(paramTypes);
  109. return constructor.newInstance(msg, params);
  110. }
  111. }