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.Random;
17  
18  import ch.qos.logback.classic.Level;
19  import ch.qos.logback.classic.corpus.RandomUtil;
20  
21  public class ScenarioRandomUtil {
22      private final static long SEED = 74130;
23  
24      private final static Random random = new Random(SEED);
25      private final static int AVERAGE_ID_LEN = 32;
26      private final static int AVERAGE_ID_DEV = 16;
27  
28      private final static int AVERAGE_CHILDREN_COUNT = 30;
29      private final static int CHILDREN_COUNT_VAR = 10;
30  
31      public static boolean oneInFreq(int freq) {
32          return (random.nextInt(freq) % freq) == 0;
33      }
34  
35      public static Level randomLevel() {
36          int rl = random.nextInt(6);
37          switch (rl) {
38          case 0:
39              return null;
40          case 1:
41              return Level.TRACE;
42          case 2:
43              return Level.DEBUG;
44          case 3:
45              return Level.INFO;
46          case 4:
47              return Level.WARN;
48          case 5:
49              return Level.ERROR;
50          default:
51              throw new IllegalStateException("rl should have been a value between 0 to 5, but it is " + rl);
52          }
53      }
54  
55      public static String randomLoggerName(int average, int stdDeviation) {
56          int depth = RandomUtil.gaussianAsPositiveInt(random, average, stdDeviation);
57          StringBuilder buf = new StringBuilder();
58          for (int i = 0; i < depth; i++) {
59              if (i != 0) {
60                  buf.append('.');
61              }
62              buf.append(randomId());
63          }
64          return buf.toString();
65      }
66  
67      public static String randomId() {
68  
69          int len = RandomUtil.gaussianAsPositiveInt(random, AVERAGE_ID_LEN, AVERAGE_ID_DEV);
70          StringBuilder buf = new StringBuilder();
71          for (int i = 0; i < len; i++) {
72              int offset = random.nextInt(26);
73              char c = (char) ('a' + offset);
74              buf.append(c);
75          }
76          return buf.toString();
77      }
78  
79      /**
80       * Returns 3 for root, 3 for children of root, 9 for offspring of generation 2
81       * and 3, and for generations 4 and later, return 0 with probability 0.5 and a
82       * gaussian (average=AVERAGE_CHILDREN_COUNT) with probability 0.5.
83       * 
84       * @param name
85       * @return
86       */
87      public static int randomChildrenCount(String name) {
88          int dots = dotCount(name);
89          if (dots <= 1) {
90              return 3;
91          } else if (dots == 2 || dots == 3) {
92              return 9;
93          } else {
94              if (shouldHaveChildrenWithProbabilitz(0.5)) {
95                  return RandomUtil.gaussianAsPositiveInt(random, AVERAGE_CHILDREN_COUNT, CHILDREN_COUNT_VAR);
96              } else {
97                  return 0;
98              }
99          }
100 
101     }
102 
103     /**
104      * Returns true with probability p.
105      * 
106      * @param p
107      * @return
108      */
109     static boolean shouldHaveChildrenWithProbabilitz(double p) {
110         if (p < 0 || p > 1.0) {
111             throw new IllegalArgumentException("p must be a value between 0 and 1.0, it was " + p + " instead.");
112         }
113         double r = random.nextDouble();
114         if (r < p) {
115             return true;
116         } else {
117             return false;
118         }
119     }
120 
121     static int dotCount(String s) {
122         int count = 0;
123         int len = s.length();
124         for (int i = 0; i < len; i++) {
125             char c = s.charAt(i);
126             if (c == '.') {
127                 count++;
128             }
129         }
130         return count;
131     }
132 }