1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.classic.multiJVM;
12  
13  import org.slf4j.Logger;
14  
15  import ch.qos.logback.classic.LoggerContext;
16  import ch.qos.logback.classic.PatternLayout;
17  import ch.qos.logback.classic.spi.LoggingEvent;
18  import ch.qos.logback.core.FileAppender;
19  import ch.qos.logback.core.testUtil.RandomUtil;
20  
21  public class FileAppenderPerf {
22    static String msgLong = "ABCDEGHIJKLMNOPQRSTUVWXYZabcdeghijklmnopqrstuvwxyz1234567890";
23  
24    static long LEN = 100 * 1000;
25    static int DIFF = RandomUtil.getPositiveInt() % 1000;
26    static String FILENAME;
27  
28    static LoggerContext buildLoggerContext(String filename, boolean safetyMode) {
29      LoggerContext loggerContext = new LoggerContext();
30  
31      FileAppender<LoggingEvent> fa = new FileAppender<LoggingEvent>();
32  
33      PatternLayout patternLayout = new PatternLayout();
34      patternLayout.setPattern("%5p %c - %m%n");
35      patternLayout.setContext(loggerContext);
36      patternLayout.start();
37  
38      fa.setLayout(patternLayout);
39      fa.setFile(filename);
40      fa.setAppend(false);
41      fa.setImmediateFlush(true);
42      fa.setBufferedIO(false);
43      fa.setPrudent(safetyMode);
44      fa.setContext(loggerContext);
45      fa.start();
46  
47      ch.qos.logback.classic.Logger root = loggerContext
48          .getLogger(LoggerContext.ROOT_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()
57          + " filename");
58  
59      System.exit(1);
60    }
61  
62    public static void main(String[] argv) throws Exception {
63      if (argv.length > 1) {
64        usage("Wrong number of arguments.");
65      }
66  
67      if (argv.length == 0) {
68        FILENAME = DIFF+"";
69      } else {
70        FILENAME = argv[0];
71      }
72  
73      perfCase(false);
74      perfCase(true);
75    }
76  
77    static void perfCase(boolean safetyMode) throws Exception {
78      LoggerContext lc = buildLoggerContext(FILENAME + "-" + safetyMode + ".log",
79          safetyMode);
80      Logger logger = lc.getLogger(FileAppenderPerf.class);
81  
82      long start = System.nanoTime();
83      for (int i = 0; i < LEN; i++) {
84        logger.debug(msgLong + " " + i);
85      }
86      // in microseconds
87      double durationPerLog = (System.nanoTime() - start) / (LEN * 1000.0);
88  
89      lc.stop();
90  
91      System.out.println("Average duration of " + (durationPerLog)
92          + " microseconds per log. Prudent mode=" + safetyMode);
93      System.out.println("------------------------------------------------");
94    }
95  
96  }