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.issue.lbclassic135;
015
016import ch.qos.logback.classic.Logger;
017import ch.qos.logback.classic.LoggerContext;
018import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
019import ch.qos.logback.classic.spi.ILoggingEvent;
020import ch.qos.logback.core.FileAppender;
021import ch.qos.logback.core.contention.ThreadedThroughputCalculator;
022
023/**
024 * Short sample code testing the throughput of a fair lock.
025 * 
026 * @author Ceki Gulcu
027 */
028public class LoggingToFileThroughput {
029
030    static int THREAD_COUNT = 1;
031    static long OVERALL_DURATION_IN_MILLIS = 5000;
032
033    public static void main(String args[]) throws InterruptedException {
034
035        ThreadedThroughputCalculator tp = new ThreadedThroughputCalculator(OVERALL_DURATION_IN_MILLIS);
036        tp.printEnvironmentInfo("lbclassic135  LoggingToFileThrouhput");
037
038        LoggerContext lc = new LoggerContext();
039        Logger logger = buildLoggerContext(lc);
040
041        for (int i = 0; i < 2; i++) {
042            tp.execute(buildArray(logger));
043        }
044
045        tp.execute(buildArray(logger));
046        tp.printThroughput("File:   ");
047        lc.stop();
048    }
049
050    static Logger buildLoggerContext(LoggerContext lc) {
051        Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
052
053        PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
054        patternLayout.setContext(lc);
055        patternLayout.setPattern("%d %l [%t] - %msg%n");
056        patternLayout.start();
057        FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
058        fileAppender.setContext(lc);
059        fileAppender.setFile("target/lbclassic135.log");
060        fileAppender.setEncoder(patternLayout);
061        fileAppender.setAppend(false);
062        fileAppender.start();
063        root.addAppender(fileAppender);
064        return lc.getLogger(LoggingToFileThroughput.class);
065    }
066
067    static LoggingRunnable[] buildArray(Logger logger) {
068
069        LoggingRunnable[] array = new LoggingRunnable[THREAD_COUNT];
070        for (int i = 0; i < THREAD_COUNT; i++) {
071            array[i] = new LoggingRunnable(logger);
072        }
073        return array;
074    }
075}
076
077// === lbclassic135 LoggingToFileThrouhput ===
078// ******** 10 Threads *****
079// synchronized doAppend() method
080//
081// java.runtime.version = 1.6.0_05-b13
082// java.vendor = Sun Microsystems Inc.
083// os.name = Windows XP
084//
085// Threads 1: total of 485077 operations, or 97 operations per millisecond
086// Threads 10: total of 309402 operations, or 61 operations per millisecond
087
088// * After revision 2310
089// * Threads 1: total of 462465 operations, or 92 operations per millisecond
090// * Threads 10: total of 243362 operations, or 48 operations per millisecond
091
092// ==================== Linux ========================
093
094// java.runtime.version = 1.6.0_11-b03
095// java.vendor = Sun Microsystems Inc.
096// os.name = Linux
097// os.version = 2.6.25-gentoo-r6
098// Threads 1: total of 356355 operations, or 71 operations per millisecond
099// Threads 10: total of 287943 operations, or 57 operations per millisecond
100
101// * After revision 2310
102// * Threads 1: total of 331494 operations, or 66 operations per millisecond
103// * Threads 10: total of 311104 operations, or 58 operations per millisecond
104
105// java.runtime.version = jvmxa6460-20081105_25433
106// java.vendor = IBM Corporation
107// java.version = 1.6.0
108// os.name = Linux
109// os.version = 2.6.25-gentoo-r6
110// Threads 1: total of 280381 operations, or 56 operations per millisecond
111// Threads 10 total of 142989 operations, or 28 operations per millisecond
112
113// * After revision 2310
114// * Threads 1: total of 305638 operations, or 61 operations per millisecond
115// * Threads 10: total of 147660 operations, or 29 operations per millisecond