[logback-dev] svn commit: r2183 - in logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic: corpus spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Fri Mar 6 14:52:38 CET 2009


Author: ceki
Date: Fri Mar  6 14:52:38 2009
New Revision: 2183

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/Corpus.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/LogStatement.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageItem.java
      - copied, changed from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageEntry.java
Removed:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageEntry.java
Modified:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java

Log:
Still working on the Corpus

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/Corpus.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/Corpus.java	Fri Mar  6 14:52:38 2009
@@ -0,0 +1,42 @@
+package ch.qos.logback.classic.corpus;
+
+import java.io.IOException;
+import java.util.List;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.classic.spi.LoggerContextVO;
+import ch.qos.logback.classic.spi.PubLoggingEventVO;
+
+public class Corpus {
+  
+  static final int STANDARD_CORPUS_SIZE = 50 * 1000;
+  static final int STANDARD_SEED = 1123;
+
+  static public ILoggingEvent[] make(CorpusMaker corpusMaker, int n) {
+    LoggerContextVO lcVO = corpusMaker.getRandomlyNamedLoggerContextVO();
+    PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
+    for (int i = 0; i < n; i++) {
+      PubLoggingEventVO e = new PubLoggingEventVO();
+      plevoArray[i] = e;
+      e.loggerContextVO = lcVO;
+      e.timeStamp = corpusMaker.getRandomTimeStamp();
+      
+      LogStatement logStatement = corpusMaker.getRandomLogStatementFromPool();
+      e.loggerName = logStatement.loggerName;
+      e.level = logStatement.level;
+      e.message = logStatement.messagerItem.message;
+      e.argumentArray = corpusMaker.getRandomArgumentArray(logStatement.messagerItem.numberOfArguments);
+      e.throwableProxy = logStatement.throwableProxy;
+      e.threadName = corpusMaker.getRandomThreadNameFromPool();
+    }
+    return plevoArray;
+  }
+
+  static  public ILoggingEvent[] makeStandardCorpus() throws IOException {
+    List<String> worldList = TextFileUtil
+        .toWords("src/test/input/corpus/origin_of_species.txt");
+    CorpusMaker corpusMaker = new CorpusMaker(STANDARD_SEED, worldList);
+    return make(corpusMaker, STANDARD_CORPUS_SIZE);
+  }
+
+}

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java	Fri Mar  6 14:52:38 2009
@@ -9,32 +9,27 @@
  */
 package ch.qos.logback.classic.corpus;
 
-import java.io.IOException;
 import java.util.List;
 import java.util.Random;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.spi.ClassPackagingData;
-import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.classic.spi.LoggerContextVO;
-import ch.qos.logback.classic.spi.PubLoggingEventVO;
 import ch.qos.logback.classic.spi.StackTraceElementProxy;
 import ch.qos.logback.classic.spi.ThrowableProxy;
 import ch.qos.logback.classic.spi.ThrowableProxyVO;
 
 public class CorpusMaker {
 
- static final int STANDARD_CORPUS_SIZE = 100*1000;
-  
   // level distribution is determined by the following table
   // it corresponds to TRACE 20%, DEBUG 30%, INFO 30%, WARN 10%,
   // ERROR 10%. See also getRandomLevel() method.
   static final double[] LEVEL_DISTRIBUTION = new double[] { .2, .5, .8, .9 };
 
-  // messages will have no arguments 90% of the time, one argument in 3%, two
-  // arguments in 3% and three arguments in 3% of cases
-  static final double[] ARGUMENT_DISTRIBUTION = new double[] { .90, .933, 0.966 };
+  // messages will have no arguments 80% of the time, one argument in 8%, two
+  // arguments in 7% and three arguments in 5% of cases
+  static final double[] ARGUMENT_DISTRIBUTION = new double[] { .80, .88, 0.95 };
 
   static final double THROWABLE_PROPABILITY_FOR_WARNING = .1;
   static final double THROWABLE_PROPABILITY_FOR_ERRORS = .3;
@@ -46,86 +41,95 @@
   static final int AVERAGE_MESSAGE_WORDS = 8;
   static final int STD_DEV_FOR_MESSAGE_WORDS = 4;
 
+  static final int AVERAGE_MILLIS_INCREMENT = 10;
+  static final int STD_DEV_FOR_MILLIS_INCREMENT = 5;
+
   static final int THREAD_POOL_SIZE = 10;
+  static final int LOGGER_POOL_SIZE = 1000;
+  static final int LOG_STATEMENT_POOL_SIZE = LOGGER_POOL_SIZE * 8;
 
   final Random random;
   List<String> worldList;
   String[] threadNamePool;
 
+  LogStatement[] logStatementPool;
+
+  // 2009-03-06 13:08 GMT
+  long lastTimeStamp = 1236344888578L;
+
   public CorpusMaker(long seed, List<String> worldList) {
     random = new Random(seed);
     this.worldList = worldList;
     buildThreadNamePool();
+    buildLogStatementPool();
   }
 
-  void buildThreadNamePool() {
+  private void buildThreadNamePool() {
     threadNamePool = new String[THREAD_POOL_SIZE];
     for (int i = 0; i < THREAD_POOL_SIZE; i++) {
       threadNamePool[i] = "CorpusMakerThread-" + i;
     }
   }
 
-  static public ILoggingEvent[] makeStandardCorpus() throws IOException {
-    List<String> worldList = TextFileUtil
-        .toWords("src/test/input/corpus/origin_of_species.txt");
-    CorpusMaker corpusMaker = new CorpusMaker(10, worldList);
-    return corpusMaker.make(STANDARD_CORPUS_SIZE);
+  private void buildLogStatementPool() {
+    String[] loggerNamePool = new String[LOGGER_POOL_SIZE];
+    for (int i = 0; i < LOGGER_POOL_SIZE; i++) {
+      loggerNamePool[i] = makeRandomLoggerName();
+    }
+    logStatementPool = new LogStatement[LOG_STATEMENT_POOL_SIZE];
+    for (int i = 0; i < LOG_STATEMENT_POOL_SIZE; i++) {
+      logStatementPool[i] = makeRandomLogStatement(loggerNamePool);
+    }
   }
 
-  public ILoggingEvent[] make(int n) {
-
-    LoggerContextVO lcVO = getRandomlyNamedLoggerContextVO();
+  private int[] getRandomAnchorPositions(int wordCount, int numAnchors) {
+    // note that the same position may appear multiple times in
+    // positionsIndex, but without serious consequences
+    int[] positionsIndex = new int[numAnchors];
+    for (int i = 0; i < numAnchors; i++) {
+      positionsIndex[i] = random.nextInt(wordCount);
+    }
+    return positionsIndex;
+  }
 
-    PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
+  private String[] getRandomWords(int n) {
+    String[] wordArray = new String[n];
     for (int i = 0; i < n; i++) {
-      PubLoggingEventVO e = new PubLoggingEventVO();
-      plevoArray[i] = e;
-      e.timeStamp = getRandomLong();
-      e.loggerName = getRandomLoggerName();
-      e.level = getRandomLevel();
-      MessageEntry me = getRandomMessageEntry();
-      e.message = me.message;
-      e.argumentArray = me.argumentArray;
-      e.loggerContextVO = lcVO;
-      Throwable t = getRandomThrowable(e.level);
-      if (t != null) {
-        e.throwableProxy = ThrowableProxyVO.build(new ThrowableProxy(t));
-        pupulateWithPackagingData(e.throwableProxy
-            .getStackTraceElementProxyArray());
-      }
-      e.threadName = getRandomThreadName();
+      wordArray[i] = getRandomWord();
     }
-    return plevoArray;
+    return wordArray;
   }
 
-  void pupulateWithPackagingData(StackTraceElementProxy[] stepArray) {
-    int i = 0;
-    for (StackTraceElementProxy step : stepArray) {
-      String identifier = "na";
-      String version = "na";
-      if (i++ % 2 == 0) {
-        identifier = getRandomJavaIdentifier();
-        version = getRandomJavaIdentifier();
-      }
-      ClassPackagingData cpd = new ClassPackagingData(identifier, version);
-      step.setClassPackagingData(cpd);
-    }
+  public long getRandomLong() {
+    return random.nextLong();
+  }
 
+  public String getRandomThreadNameFromPool() {
+    int index = random.nextInt(THREAD_POOL_SIZE);
+    return threadNamePool[index];
   }
 
-  LoggerContextVO getRandomlyNamedLoggerContextVO() {
-    LoggerContext lc = new LoggerContext();
-    lc.setName(getRandomJavaIdentifier());
-    return new LoggerContextVO(lc);
+  public LogStatement getRandomLogStatementFromPool() {
+    int index = random.nextInt(logStatementPool.length);
+    return logStatementPool[index];
   }
 
-  long getRandomLong() {
-    return random.nextLong();
+  private String getRandomLoggerNameFromPool(String[] loggerNamePool) {
+    int index = random.nextInt(loggerNamePool.length);
+    return loggerNamePool[index];
   }
 
-  String getRandomThreadName() {
-    int index = random.nextInt(THREAD_POOL_SIZE);
-    return threadNamePool[index];
+  public long getRandomTimeStamp() {
+    // subtract 1 so that 0 is allowed
+    lastTimeStamp += RandomUtil.gaussianAsPositiveInt(random,
+        AVERAGE_MILLIS_INCREMENT, STD_DEV_FOR_MILLIS_INCREMENT) - 1;
+    return lastTimeStamp;
+  }
+
+  LoggerContextVO getRandomlyNamedLoggerContextVO() {
+    LoggerContext lc = new LoggerContext();
+    lc.setName(getRandomJavaIdentifier());
+    return new LoggerContextVO(lc);
   }
 
   String getRandomWord() {
@@ -134,56 +138,54 @@
     return worldList.get(randomIndex);
   }
 
-  int[] getRandomAnchorPositions(int wordCount, int numAnchors) {
-    // note that the same position may appear multiple times in
-    // positionsIndex, but without serious consequences
-    int[] positionsIndex = new int[numAnchors];
-    for (int i = 0; i < numAnchors; i++) {
-      positionsIndex[i] = random.nextInt(wordCount);
+  public Object[] getRandomArgumentArray(int numOfArguments) {
+    if (numOfArguments == 0) {
+      return null;
     }
-    return positionsIndex;
-  }
-
-  private String[] getRandomWords(int n) {
-    String[] wordArray = new String[n];
-    for (int i = 0; i < n; i++) {
-      wordArray[i] = getRandomWord();
+    Object[] argumentArray = new Object[numOfArguments];
+    for (int i = 0; i < numOfArguments; i++) {
+      argumentArray[i] = new Long(random.nextLong());
     }
-    return wordArray;
+    return argumentArray;
   }
 
-  MessageEntry getRandomMessageEntry() {
+  private MessageItem makeRandomMessageEntry() {
     int numOfArguments = getNumberOfMessageArguments();
-    Object[] argumentArray = null;
-    if (numOfArguments > 0) {
-      argumentArray = new Object[numOfArguments];
-      for (int i = 0; i < numOfArguments; i++) {
-        argumentArray[i] = new Long(random.nextLong());
-      }
-    }
+
     int wordCount = RandomUtil.gaussianAsPositiveInt(random,
         AVERAGE_MESSAGE_WORDS, STD_DEV_FOR_MESSAGE_WORDS);
     String[] wordArray = getRandomWords(wordCount);
 
     int[] anchorPositions = getRandomAnchorPositions(wordCount, numOfArguments);
+
     for (int anchorIndex : anchorPositions) {
       wordArray[anchorIndex] = "{}";
     }
 
     StringBuilder sb = new StringBuilder();
     for (int i = 1; i < wordCount; i++) {
-      sb.append(getRandomWord()).append(' ');
+      sb.append(wordArray[i]).append(' ');
     }
     sb.append(getRandomWord());
-
-    return new MessageEntry(sb.toString(), argumentArray);
+    return new MessageItem(sb.toString(), numOfArguments);
   }
 
-  Throwable buildThrowable(double i) {
-    return null;
+  private LogStatement makeRandomLogStatement(String[] loggerNamePool) {
+    MessageItem mi = makeRandomMessageEntry();
+    String loggerName = getRandomLoggerNameFromPool(loggerNamePool);
+    Level randomLevel = getRandomLevel();
+    Throwable t = getRandomThrowable(randomLevel);
+    ThrowableProxyVO throwableProxy = null;
+    if (t != null) {
+      throwableProxy = ThrowableProxyVO.build(new ThrowableProxy(t));
+      pupulateWithPackagingData(throwableProxy.getStackTraceElementProxyArray());
+    }
+    LogStatement logStatement = new LogStatement(loggerName, randomLevel, mi,
+        throwableProxy);
+    return logStatement;
   }
 
-  Throwable getRandomThrowable(Level level) {
+  private Throwable getRandomThrowable(Level level) {
     double rn = random.nextDouble();
     if ((level == Level.WARN && rn < THROWABLE_PROPABILITY_FOR_WARNING)
         || (level == Level.ERROR && rn < THROWABLE_PROPABILITY_FOR_ERRORS)) {
@@ -193,7 +195,21 @@
     }
   }
 
-  int getNumberOfMessageArguments() {
+  private void pupulateWithPackagingData(StackTraceElementProxy[] stepArray) {
+    int i = 0;
+    for (StackTraceElementProxy step : stepArray) {
+      String identifier = "na";
+      String version = "na";
+      if (i++ % 2 == 0) {
+        identifier = getRandomJavaIdentifier();
+        version = getRandomJavaIdentifier();
+      }
+      ClassPackagingData cpd = new ClassPackagingData(identifier, version);
+      step.setClassPackagingData(cpd);
+    }
+  }
+  
+  private int getNumberOfMessageArguments() {
     double rn = random.nextDouble();
     if (rn < ARGUMENT_DISTRIBUTION[0]) {
       return 0;
@@ -213,7 +229,7 @@
     return w;
   }
 
-  String getRandomLoggerName() {
+  private String makeRandomLoggerName() {
     int parts = RandomUtil.gaussianAsPositiveInt(random,
         AVERAGE_LOGGER_NAME_PARTS, STD_DEV_FOR_LOGGER_NAME_PARTS);
     StringBuilder sb = new StringBuilder();
@@ -224,7 +240,7 @@
     return sb.toString();
   }
 
-  Level getRandomLevel() {
+  private Level getRandomLevel() {
     double rn = random.nextDouble();
     if (rn < LEVEL_DISTRIBUTION[0]) {
       return Level.TRACE;

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/LogStatement.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/LogStatement.java	Fri Mar  6 14:52:38 2009
@@ -0,0 +1,29 @@
+package ch.qos.logback.classic.corpus;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.spi.IThrowableProxy;
+
+public class LogStatement {
+
+  final String loggerName;
+  final MessageItem messagerItem;
+  final Level level;
+  final IThrowableProxy throwableProxy;
+
+  public LogStatement(String loggerName, Level level, MessageItem messagerItem,
+      IThrowableProxy tp) {
+    this.loggerName = loggerName;
+    this.level = level;
+    this.messagerItem = messagerItem;
+    this.throwableProxy = tp;
+  }
+
+  public String getLoggerName() {
+    return loggerName;
+  }
+
+  public MessageItem getMessagerItem() {
+    return messagerItem;
+  }
+
+}

Copied: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageItem.java (from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageEntry.java)
==============================================================================
--- /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageEntry.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/MessageItem.java	Fri Mar  6 14:52:38 2009
@@ -1,27 +1,18 @@
 package ch.qos.logback.classic.corpus;
 
-public class MessageEntry {
+public class MessageItem {
 
   
   final String message;
-  final Object[] argumentArray;
+  final int numberOfArguments;
 
-  public MessageEntry(String message) {
-    this(message, null);
+  MessageItem(String message) {
+    this(message, 0);
   }
 
-  
-  public MessageEntry(String message, Object[] argumentArray) {
+  public MessageItem(String message, int numberOfArguments) {
     this.message = message;
-    this.argumentArray = argumentArray;
-  }
-
-  public String getMessage() {
-    return message;
+    this.numberOfArguments = numberOfArguments;
   }
 
-  public Object[] getArgumentArray() {
-    return argumentArray;
-  }
-  
 }

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java	Fri Mar  6 14:52:38 2009
@@ -36,7 +36,7 @@
 
   public Object[] argumentArray;
 
-  public ThrowableProxyVO throwableProxy;
+  public IThrowableProxy throwableProxy;
   public CallerData[] callerDataArray;
   public Marker marker;
   public Map<String, String> mdcPropertyMap;


More information about the logback-dev mailing list