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;
15  
16  import org.junit.Before;
17  import org.junit.Test;
18  
19  import ch.qos.logback.classic.corpus.CorpusModel;
20  import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
21  import ch.qos.logback.core.contention.ThreadedThroughputCalculator;
22  
23  public class LoggerContextPerfTest {
24  
25      static int THREAD_COUNT = 10000;
26      int totalTestDuration = 4000;
27  
28      LoggerContext loggerContext = new LoggerContext();
29  
30      ThreadedThroughputCalculator harness = new ThreadedThroughputCalculator(totalTestDuration);
31      RunnableWithCounterAndDone[] runnableArray = buildRunnableArray();
32  
33      CorpusModel corpusMaker;
34  
35      @Before
36      public void setUp() throws Exception {
37      }
38  
39      private RunnableWithCounterAndDone[] buildRunnableArray() {
40          RunnableWithCounterAndDone[] runnableArray = new RunnableWithCounterAndDone[THREAD_COUNT];
41          for (int i = 0; i < THREAD_COUNT; i++) {
42              runnableArray[i] = new GetLoggerRunnable();
43          }
44          return runnableArray;
45      }
46  
47      // Results computed on a Intel i7
48      // 1 thread
49      // 13'107 ops per milli using Hashtable for LoggerContext.loggerCache
50      // 15'258 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
51  
52      // 10 threads
53      // 8'468 ops per milli using Hashtable for LoggerContext.loggerCache
54      // 58'945 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
55  
56      // 100 threads
57      // 8'863 ops per milli using Hashtable for LoggerContext.loggerCache
58      // 34'810 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
59  
60      // 1'000 threads
61      // 8'188 ops per milli using Hashtable for LoggerContext.loggerCache
62      // 24'012 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
63  
64      // 10'000 threads
65      // 7'595 ops per milli using Hashtable for LoggerContext.loggerCache
66      // 8'989 ops per milli using ConcurrentHashMap for LoggerContext.loggerCache
67  
68      @Test
69      public void computeResults() throws InterruptedException {
70          harness.execute(runnableArray);
71          harness.printThroughput("getLogger performance: ", true);
72      }
73  
74      private class GetLoggerRunnable extends RunnableWithCounterAndDone {
75  
76          final int burstLength = 3;
77  
78          public void run() {
79              while (!isDone()) {
80                  long i = counter % burstLength;
81  
82                  loggerContext.getLogger("a" + i);
83                  counter++;
84                  if (i == 0) {
85                      Thread.yield();
86                  }
87              }
88          }
89      }
90  }