[logback-dev] svn commit: r2295 - logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue

noreply.ceki at qos.ch noreply.ceki at qos.ch
Mon Jun 15 21:55:47 CEST 2009


Author: ceki
Date: Mon Jun 15 21:55:46 2009
New Revision: 2295

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/NoLockThroughput.java
Modified:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/RunnableForThrougputComputation.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/SelectiveLockRunnable.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/ThreadedThroughputCalculator.java

Log:
- testing behaviour when there are no locks.

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/NoLockThroughput.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/NoLockThroughput.java	Mon Jun 15 21:55:46 2009
@@ -0,0 +1,38 @@
+package ch.qos.logback.core.issue;
+
+import ch.qos.logback.core.issue.SelectiveLockRunnable.LockingModel;
+
+/**
+ * Short sample code testing the throughput of a fair lock.
+ * 
+ * @author Joern Huxhorn
+ * @author Ceki Gulcu
+ */
+public class NoLockThroughput {
+
+  static int THREAD_COUNT = 10;
+  static long OVERALL_DURATION_IN_MILLIS = 5000;
+
+  public static void main(String args[]) throws InterruptedException {
+
+    ThreadedThroughputCalculator tp = new ThreadedThroughputCalculator(
+        OVERALL_DURATION_IN_MILLIS);
+    tp.printEnvironmentInfo("NoLockThroughput");
+
+    for (int i = 0; i < 2; i++) {
+      tp.execute(buildArray(LockingModel.NOLOCK));
+    }
+
+    tp.execute(buildArray(LockingModel.NOLOCK));
+    tp.printThroughput("No lock:   ", true);
+  }
+
+  static SelectiveLockRunnable[] buildArray(LockingModel model) {
+    SelectiveLockRunnable[] array = new SelectiveLockRunnable[THREAD_COUNT];
+    for (int i = 0; i < THREAD_COUNT; i++) {
+      array[i] = new SelectiveLockRunnable(model);
+    }
+    return array;
+  }
+
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/RunnableForThrougputComputation.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/RunnableForThrougputComputation.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/RunnableForThrougputComputation.java	Mon Jun 15 21:55:46 2009
@@ -9,9 +9,9 @@
 abstract public class RunnableForThrougputComputation implements Runnable {
 
   protected boolean done = false;
-  protected int counter = 0;
+  protected long counter = 0;
   
-  public int getCounter() {
+  public long getCounter() {
     return counter;
   }
 

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/SelectiveLockRunnable.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/SelectiveLockRunnable.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/SelectiveLockRunnable.java	Mon Jun 15 21:55:46 2009
@@ -12,22 +12,24 @@
 public class SelectiveLockRunnable extends RunnableForThrougputComputation {
 
   enum LockingModel {
-    SYNC, FAIR, UNFAIR;
+    NOLOCK, SYNC, FAIR, UNFAIR;
   }
 
   static Object LOCK = new Object();
   static Lock FAIR_LOCK = new ReentrantLock(true);
   static Lock UNFAIR_LOCK = new ReentrantLock(false);
 
-
   LockingModel model;
-  
+
   SelectiveLockRunnable(LockingModel model) {
     this.model = model;
   }
 
   public void run() {
     switch (model) {
+    case NOLOCK:
+      nolockRun();
+      break;
     case SYNC:
       synchronizedRUn();
       break;
@@ -39,6 +41,7 @@
       break;
     }
   }
+
   void fairLockRun() {
     for (;;) {
       FAIR_LOCK.lock();
@@ -61,6 +64,15 @@
     }
   }
 
+  void nolockRun() {
+    for (;;) {
+      counter++;
+      if (done) {
+        return;
+      }
+    }
+  }
+
   void synchronizedRUn() {
     for (;;) {
       synchronized (LOCK) {
@@ -71,4 +83,9 @@
       }
     }
   }
+  
+  @Override
+  public String toString() {
+    return "SelectiveLockRunnable "+model;
+  }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/ThreadedThroughputCalculator.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/ThreadedThroughputCalculator.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/issue/ThreadedThroughputCalculator.java	Mon Jun 15 21:55:46 2009
@@ -20,7 +20,7 @@
   }
 
   public void printEnvironmentInfo(String msg) {
-    System.out.println("=== "+ msg +" ===");
+    System.out.println("=== " + msg + " ===");
     System.out.println("java.runtime.version = "
         + System.getProperty("java.runtime.version"));
     System.out.println("java.vendor          = "
@@ -54,14 +54,20 @@
       t.join();
     }
   }
-
   public void printThroughput(String msg) throws InterruptedException {
-    int sum = 0;
+    printThroughput(msg, false);
+  }
+  
+  public void printThroughput(String msg, boolean detailed) throws InterruptedException {
+    long sum = 0;
     for (RunnableForThrougputComputation r : runnableArray) {
+      if(detailed) {
+        System.out.println(r +" count="+r.getCounter());
+      }
       sum += r.getCounter();
     }
+    
     System.out.println(msg + "total of " + sum + " operations, or "
-        + (sum / overallDurationInMillis) + " operations per millisecond");
+        + ((sum) / overallDurationInMillis) + " operations per millisecond");
   }
-
 }


More information about the logback-dev mailing list