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.control;
15  
16  import java.util.LinkedList;
17  
18  import ch.qos.logback.classic.Level;
19  import ch.qos.logback.core.CoreConstants;
20  
21  public class ScenarioMaker {
22  
23      private final static int AVERAGE_LOGGER_DEPTH = 4;
24      private final static int LOGGER_DEPT_DEV = 2;
25      // the frequency of a set levelInt event for every create logger event
26      private final static int CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY = 5;
27      private final static int SECOND_SET_LEVEL_FREQUENCY = 3;
28  
29      private static long count = 0;
30  
31      /**
32       * Makes a scenario with len logger creations. Logger names are generated
33       * independently such that the overwhelming majority of logger names will be
34       * unrelated to each other. Each logger creation may be followed with a
35       * randomly generated set levelInt action on that logger.
36       * 
37       * @param len
38       * @return
39       */
40      static public Scenario makeTypeAScenario(int len) {
41          Scenario scenario = new Scenario();
42          ;
43          for (int i = 0; i < len; i++) {
44              String loggerName = ScenarioRandomUtil.randomLoggerName(AVERAGE_LOGGER_DEPTH, LOGGER_DEPT_DEV);
45              scenario.add(new CreateLogger(loggerName));
46          }
47          return scenario;
48      }
49  
50      static public Scenario makeRealisticCreationScenario(int len) {
51          Scenario scenario = new Scenario();
52          LinkedList<String> queue = new LinkedList<String>();
53          int loggerCreationCount = 0;
54  
55          // add an empty string to get going
56          queue.add("");
57  
58          while (loggerCreationCount < len) {
59              if ((count % 100) == 0) {
60                  System.out.println("count=" + count);
61              }
62  
63              String loggerName = (String) queue.removeFirst();
64              int randomChildrenCount = ScenarioRandomUtil.randomChildrenCount(loggerName);
65  
66              if (randomChildrenCount == 0) {
67                  scenario.add(new CreateLogger(loggerName));
68                  addSetLevelSubScenario(scenario, loggerName);
69                  loggerCreationCount++;
70              } else {
71                  for (int i = 0; i < randomChildrenCount; i++) {
72                      String childName;
73                      if (loggerName.equals("")) {
74                          childName = ScenarioRandomUtil.randomId();
75                          count += childName.length();
76                      } else {
77                          childName = loggerName + CoreConstants.DOT + ScenarioRandomUtil.randomId();
78                          count += childName.length();
79                      }
80                      queue.add(childName);
81                      addSetLevelSubScenario(scenario, loggerName);
82                      loggerCreationCount++;
83                  }
84              }
85          }
86          return scenario;
87      }
88  
89      static void addSetLevelSubScenario(Scenario scenario, String loggerName) {
90          if (ScenarioRandomUtil.oneInFreq(CREATE_LOGGER_TO_SET_LEVEL_FREQUENCY)) {
91              Level l = ScenarioRandomUtil.randomLevel();
92              scenario.add(new SetLevel(l, loggerName));
93              if (ScenarioRandomUtil.oneInFreq(SECOND_SET_LEVEL_FREQUENCY)) {
94                  l = ScenarioRandomUtil.randomLevel();
95                  scenario.add(new SetLevel(l, loggerName));
96              }
97          }
98      }
99  
100 }