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

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 267 lines · 207 code · 39 blank · 21 comment · 13 complexity · 3bc62c47448ba933157f1755e6a92ec5 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.config.XMLConfigurationFactory;
  19. import org.apache.logging.log4j.core.util.Profiler;
  20. import org.junit.AfterClass;
  21. import org.junit.BeforeClass;
  22. import org.junit.Test;
  23. import java.io.BufferedOutputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.FileWriter;
  26. import java.io.OutputStream;
  27. import java.io.Writer;
  28. import java.nio.ByteBuffer;
  29. import java.nio.channels.FileChannel;
  30. /**
  31. *
  32. */
  33. public class DebugDisabledPerformanceComparison {
  34. private final Logger logger = LogManager.getLogger(DebugDisabledPerformanceComparison.class.getName());
  35. private final org.slf4j.Logger logbacklogger = org.slf4j.LoggerFactory.getLogger(DebugDisabledPerformanceComparison.class);
  36. private final org.apache.log4j.Logger log4jlogger = org.apache.log4j.Logger.getLogger(DebugDisabledPerformanceComparison.class);
  37. // How many times should we try to log:
  38. private static final int COUNT = 10000000;
  39. private static final int PROFILE_COUNT = 500000;
  40. private static final int WARMUP = 1000;
  41. private static final String CONFIG = "log4j2-perf2.xml";
  42. private static final String LOGBACK_CONFIG = "logback-perf2.xml";
  43. private static final String LOG4J_CONFIG = "log4j12-perf2.xml";
  44. private static final String LOGBACK_CONF = "logback.configurationFile";
  45. private static final String LOG4J_CONF = "log4j.configuration";
  46. @BeforeClass
  47. public static void setupClass() {
  48. System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
  49. System.setProperty(LOGBACK_CONF, LOGBACK_CONFIG);
  50. System.setProperty(LOG4J_CONF, LOG4J_CONFIG);
  51. }
  52. @AfterClass
  53. public static void cleanupClass() {
  54. System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
  55. System.clearProperty(LOGBACK_CONF);
  56. System.clearProperty(LOG4J_CONF);
  57. }
  58. @Test
  59. public void testPerformance() throws Exception {
  60. log4j(WARMUP);
  61. logback(WARMUP);
  62. log4j2(WARMUP);
  63. if (Profiler.isActive()) {
  64. System.out.println("Profiling Log4j 2.0");
  65. Profiler.start();
  66. final long result = log4j2(PROFILE_COUNT);
  67. Profiler.stop();
  68. System.out.println("###############################################");
  69. System.out.println("Log4j 2.0: " + result);
  70. System.out.println("###############################################");
  71. } else {
  72. System.out.println("Starting isDebugEnabled tests");
  73. System.out.println("Starting Log4j 2.0");
  74. long result3 = log4j2IsDebug(COUNT);
  75. System.out.println("Starting Log4j");
  76. long result1 = log4j2IsDebug(COUNT);
  77. System.out.println("Starting Logback");
  78. long result2 = logbackIsDebug(COUNT );
  79. System.out.println("###############################################");
  80. System.out.println("Log4j: " + result1);
  81. System.out.println("Logback: " + result2);
  82. System.out.println("Log4j 2.0: " + result3);
  83. System.out.println("###############################################");
  84. System.out.println("Starting logger.debug tests with String concatenation");
  85. System.out.println("Starting Log4j 2.0");
  86. result3 = log4j2(COUNT);
  87. System.out.println("Starting Log4j");
  88. result1 = log4j(COUNT);
  89. System.out.println("Starting Logback");
  90. result2 = logback(COUNT);
  91. System.out.println("###############################################");
  92. System.out.println("Log4j: " + result1);
  93. System.out.println("Logback: " + result2);
  94. System.out.println("Log4j 2.0: " + result3);
  95. System.out.println("###############################################");
  96. System.out.println("Starting logger.debug tests without String concatenation");
  97. System.out.println("Starting Log4j 2.0");
  98. result3 = log4j2Debug(COUNT);
  99. System.out.println("Starting Logback");
  100. result2 = logbackDebug(COUNT);
  101. System.out.println("###############################################");
  102. System.out.println("Log4j: Not supported");
  103. System.out.println("Logback: " + result2);
  104. System.out.println("Log4j 2.0: " + result3);
  105. System.out.println("###############################################");
  106. }
  107. }
  108. //@Test
  109. public void testRawPerformance() throws Exception {
  110. final OutputStream os = new FileOutputStream("target/testos.log", true);
  111. final long result1 = writeToStream(COUNT, os);
  112. os.close();
  113. final OutputStream bos = new BufferedOutputStream(new FileOutputStream("target/testbuffer.log", true));
  114. final long result2 = writeToStream(COUNT, bos);
  115. bos.close();
  116. final Writer w = new FileWriter("target/testwriter.log", true);
  117. final long result3 = writeToWriter(COUNT, w);
  118. w.close();
  119. final FileOutputStream cos = new FileOutputStream("target/testchannel.log", true);
  120. final FileChannel channel = cos.getChannel();
  121. final long result4 = writeToChannel(COUNT, channel);
  122. cos.close();
  123. System.out.println("###############################################");
  124. System.out.println("FileOutputStream: " + result1);
  125. System.out.println("BufferedOutputStream: " + result2);
  126. System.out.println("FileWriter: " + result3);
  127. System.out.println("FileChannel: " + result4);
  128. System.out.println("###############################################");
  129. }
  130. private long log4jIsDebug(final int loop) {
  131. final Integer j = new Integer(2);
  132. final long start = System.nanoTime();
  133. for (int i = 0; i < loop; i++) {
  134. log4jlogger.isDebugEnabled();
  135. }
  136. return (System.nanoTime() - start) / loop;
  137. }
  138. private long logbackIsDebug(final int loop) {
  139. final Integer j = new Integer(2);
  140. final long start = System.nanoTime();
  141. for (int i = 0; i < loop; i++) {
  142. logbacklogger.isDebugEnabled();
  143. }
  144. return (System.nanoTime() - start) / loop;
  145. }
  146. private long log4j2IsDebug(final int loop) {
  147. final Integer j = new Integer(2);
  148. final long start = System.nanoTime();
  149. for (int i = 0; i < loop; i++) {
  150. logger.isDebugEnabled();
  151. }
  152. return (System.nanoTime() - start) / loop;
  153. }
  154. private long log4j(final int loop) {
  155. final Integer j = new Integer(2);
  156. final long start = System.nanoTime();
  157. for (int i = 0; i < loop; i++) {
  158. log4jlogger.debug("SEE IF THIS IS LOGGED " + j + '.');
  159. }
  160. return (System.nanoTime() - start) / loop;
  161. }
  162. private long logback(final int loop) {
  163. final Integer j = new Integer(2);
  164. final long start = System.nanoTime();
  165. for (int i = 0; i < loop; i++) {
  166. logbacklogger.debug("SEE IF THIS IS LOGGED " + j + '.');
  167. }
  168. return (System.nanoTime() - start) / loop;
  169. }
  170. private long logbackDebug(final int loop) {
  171. final Integer j = new Integer(2);
  172. final long start = System.nanoTime();
  173. for (int i = 0; i < loop; i++) {
  174. logbacklogger.debug("SEE IF THIS IS LOGGED {} .", j);
  175. }
  176. return (System.nanoTime() - start) / loop;
  177. }
  178. private long log4j2(final int loop) {
  179. final Integer j = new Integer(2);
  180. final long start = System.nanoTime();
  181. for (int i = 0; i < loop; i++) {
  182. logger.debug("SEE IF THIS IS LOGGED " + j + '.');
  183. }
  184. return (System.nanoTime() - start) / loop;
  185. }
  186. private long log4j2Debug(final int loop) {
  187. final Integer j = new Integer(2);
  188. final long start = System.nanoTime();
  189. for (int i = 0; i < loop; i++) {
  190. logger.debug("SEE IF THIS IS LOGGED {} .", j);
  191. }
  192. return (System.nanoTime() - start) / loop;
  193. }
  194. private long writeToWriter(final int loop, final Writer w) throws Exception {
  195. final Integer j = new Integer(2);
  196. final long start = System.nanoTime();
  197. for (int i = 0; i < loop; i++) {
  198. w.write("SEE IF THIS IS LOGGED " + j + '.');
  199. }
  200. return (System.nanoTime() - start) / loop;
  201. }
  202. private long writeToStream(final int loop, final OutputStream os) throws Exception {
  203. final Integer j = new Integer(2);
  204. final long start = System.nanoTime();
  205. for (int i = 0; i < loop; i++) {
  206. os.write(getBytes("SEE IF THIS IS LOGGED " + j + '.'));
  207. }
  208. return (System.nanoTime() - start) / loop;
  209. }
  210. private long writeToChannel(final int loop, final FileChannel channel) throws Exception {
  211. final Integer j = new Integer(2);
  212. final ByteBuffer buf = ByteBuffer.allocateDirect(8*1024);
  213. final long start = System.nanoTime();
  214. for (int i = 0; i < loop; i++) {
  215. channel.write(getByteBuffer(buf, "SEE IF THIS IS LOGGED " + j + '.'));
  216. }
  217. return (System.nanoTime() - start) / loop;
  218. }
  219. private ByteBuffer getByteBuffer(final ByteBuffer buf, final String s) {
  220. buf.clear();
  221. buf.put(s.getBytes());
  222. buf.flip();
  223. return buf;
  224. }
  225. private byte[] getBytes(final String s) {
  226. return s.getBytes();
  227. }
  228. }