View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.multiJVM;
15  
16  import org.slf4j.Logger;
17  
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.core.FileAppender;
22  import ch.qos.logback.core.testUtil.RandomUtil;
23  
24  public class FileAppenderPerf {
25      static String msgLong = "ABCDEGHIJKLMNOPQRSTUVWXYZabcdeghijklmnopqrstuvwxyz1234567890";
26  
27      static long LEN = 100 * 1000;
28      static int DIFF = RandomUtil.getPositiveInt() % 1000;
29      static String FILENAME;
30  
31      static LoggerContext buildLoggerContext(String filename, boolean safetyMode) {
32          LoggerContext loggerContext = new LoggerContext();
33  
34          FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
35  
36          PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
37          patternLayout.setPattern("%5p %c - %m%n");
38          patternLayout.setContext(loggerContext);
39          patternLayout.start();
40  
41          fa.setEncoder(patternLayout);
42          fa.setFile(filename);
43          fa.setAppend(false);
44          fa.setPrudent(safetyMode);
45          fa.setContext(loggerContext);
46          fa.start();
47  
48          ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
49          root.addAppender(fa);
50  
51          return loggerContext;
52      }
53  
54      static void usage(String msg) {
55          System.err.println(msg);
56          System.err.println("Usage: java " + FileAppenderPerf.class.getName() + " filename");
57  
58          System.exit(1);
59      }
60  
61      public static void main(String[] argv) throws Exception {
62          if (argv.length > 1) {
63              usage("Wrong number of arguments.");
64          }
65  
66          if (argv.length == 0) {
67              FILENAME = DIFF + "";
68          } else {
69              FILENAME = argv[0];
70          }
71  
72          perfCase(false);
73          perfCase(true);
74      }
75  
76      static void perfCase(boolean safetyMode) throws Exception {
77          LoggerContext lc = buildLoggerContext(FILENAME + "-" + safetyMode + ".log", safetyMode);
78          Logger logger = lc.getLogger(FileAppenderPerf.class);
79  
80          long start = System.nanoTime();
81          for (int i = 0; i < LEN; i++) {
82              logger.debug(msgLong + " " + i);
83          }
84          // in microseconds
85          double durationPerLog = (System.nanoTime() - start) / (LEN * 1000.0);
86  
87          lc.stop();
88  
89          System.out.println(
90                  "Average duration of " + (durationPerLog) + " microseconds per log. Prudent mode=" + safetyMode);
91          System.out.println("------------------------------------------------");
92      }
93  
94  }