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;
015
016import org.junit.Before;
017import org.junit.Test;
018
019import ch.qos.logback.classic.corpus.CorpusModel;
020import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
021import ch.qos.logback.core.contention.ThreadedThroughputCalculator;
022
023public class LoggerContextPerfTest {
024
025    static int THREAD_COUNT = 10000;
026    int totalTestDuration = 4000;
027
028    LoggerContext loggerContext = new LoggerContext();
029
030    ThreadedThroughputCalculator harness = new ThreadedThroughputCalculator(totalTestDuration);
031    RunnableWithCounterAndDone[] runnableArray = buildRunnableArray();
032
033    CorpusModel corpusMaker;
034
035    @Before
036    public void setUp() throws Exception {
037    }
038
039    private RunnableWithCounterAndDone[] buildRunnableArray() {
040        RunnableWithCounterAndDone[] runnableArray = new RunnableWithCounterAndDone[THREAD_COUNT];
041        for (int i = 0; i < THREAD_COUNT; i++) {
042            runnableArray[i] = new GetLoggerRunnable();
043        }
044        return runnableArray;
045    }
046
047    // Results computed on a Intel i7
048    // 1 thread
049    // 13'107 ops per milli using Hashtable for LoggerContext.loggerCache
050    // 15'258 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
051
052    // 10 threads
053    // 8'468 ops per milli using Hashtable for LoggerContext.loggerCache
054    // 58'945 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
055
056    // 100 threads
057    // 8'863 ops per milli using Hashtable for LoggerContext.loggerCache
058    // 34'810 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
059
060    // 1'000 threads
061    // 8'188 ops per milli using Hashtable for LoggerContext.loggerCache
062    // 24'012 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
063
064    // 10'000 threads
065    // 7'595 ops per milli using Hashtable for LoggerContext.loggerCache
066    // 8'989 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
067
068    @Test
069    public void computeResults() throws InterruptedException {
070        harness.execute(runnableArray);
071        harness.printThroughput("getLogger performance: ", true);
072    }
073
074    private class GetLoggerRunnable extends RunnableWithCounterAndDone {
075
076        final int burstLength = 3;
077
078        public void run() {
079            while (!isDone()) {
080                long i = counter % burstLength;
081
082                loggerContext.getLogger("a" + i);
083                counter++;
084                if (i == 0) {
085                    Thread.yield();
086                }
087            }
088        }
089    }
090}