001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.multiJVM;
015
016import org.slf4j.Logger;
017
018import ch.qos.logback.classic.LoggerContext;
019import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
020import ch.qos.logback.classic.spi.ILoggingEvent;
021import ch.qos.logback.core.FileAppender;
022import ch.qos.logback.core.testUtil.RandomUtil;
023
024public class FileAppenderPerf {
025    static String msgLong = "ABCDEGHIJKLMNOPQRSTUVWXYZabcdeghijklmnopqrstuvwxyz1234567890";
026
027    static long LEN = 100 * 1000;
028    static int DIFF = RandomUtil.getPositiveInt() % 1000;
029    static String FILENAME;
030
031    static LoggerContext buildLoggerContext(String filename, boolean safetyMode) {
032        LoggerContext loggerContext = new LoggerContext();
033
034        FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
035
036        PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
037        patternLayout.setPattern("%5p %c - %m%n");
038        patternLayout.setContext(loggerContext);
039        patternLayout.start();
040
041        fa.setEncoder(patternLayout);
042        fa.setFile(filename);
043        fa.setAppend(false);
044        fa.setPrudent(safetyMode);
045        fa.setContext(loggerContext);
046        fa.start();
047
048        ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
049        root.addAppender(fa);
050
051        return loggerContext;
052    }
053
054    static void usage(String msg) {
055        System.err.println(msg);
056        System.err.println("Usage: java " + FileAppenderPerf.class.getName() + " filename");
057
058        System.exit(1);
059    }
060
061    public static void main(String[] argv) throws Exception {
062        if (argv.length > 1) {
063            usage("Wrong number of arguments.");
064        }
065
066        if (argv.length == 0) {
067            FILENAME = DIFF + "";
068        } else {
069            FILENAME = argv[0];
070        }
071
072        perfCase(false);
073        perfCase(true);
074    }
075
076    static void perfCase(boolean safetyMode) throws Exception {
077        LoggerContext lc = buildLoggerContext(FILENAME + "-" + safetyMode + ".log", safetyMode);
078        Logger logger = lc.getLogger(FileAppenderPerf.class);
079
080        long start = System.nanoTime();
081        for (int i = 0; i < LEN; i++) {
082            logger.debug(msgLong + " " + i);
083        }
084        // in microseconds
085        double durationPerLog = (System.nanoTime() - start) / (LEN * 1000.0);
086
087        lc.stop();
088
089        System.out.println("Average duration of " + (durationPerLog) + " microseconds per log. Prudent mode=" + safetyMode);
090        System.out.println("------------------------------------------------");
091    }
092
093}