You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2010/09/26 22:25:39 UTC

svn commit: r1001504 - in /commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool: PoolClientThread.java Waiter.java WaiterFactory.java

Author: psteitz
Date: Sun Sep 26 20:25:39 2010
New Revision: 1001504

URL: http://svn.apache.org/viewvc?rev=1001504&view=rev
Log:
Added tracking for instance idle time; improved javadoc.

Modified:
    commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/PoolClientThread.java
    commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/Waiter.java
    commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/WaiterFactory.java

Modified: commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/PoolClientThread.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/PoolClientThread.java?rev=1001504&r1=1001503&r2=1001504&view=diff
==============================================================================
--- commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/PoolClientThread.java (original)
+++ commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/PoolClientThread.java Sun Sep 26 20:25:39 2010
@@ -30,21 +30,44 @@ import org.apache.commons.performance.Cl
 import org.apache.commons.performance.Statistics;
 
 /**
- * Client thread that borrows and returns objects from a pool in a loop.
+ * <p>Client thread that borrows and returns objects from a pool in a loop.
  * See {@link ClientThread ClientThread javadoc} for a description
- * of how times between requests are computed.
- *
+ * of how times between requests are computed.  In addition to request latency,
+ * pool numIdle and numActive statistics and stats on instance idle time are tracked.</p>
+ * 
+ * <p>Note that this class is *not* threadsafe.</p>
  */
 public class PoolClientThread extends ClientThread {
     
-    private ObjectPool pool;
-    private KeyedObjectPool keyedPool;
+    /** Object pool (if keyed = false) */
+    private final ObjectPool pool;
+    
+    /**Keyed pool (if keyed = true) */
+    private final KeyedObjectPool keyedPool;
+    
+    /** Whether or not pool being tested is keyed */
     private boolean keyed;
-    private List<Integer> keys;
-    private RandomData randomData = new RandomDataImpl();
+    
+    /** Randomly generated keys (if keyed = true) */
+    private List<Integer> keys; 
+    
+    /** Source of randomness (for keys, if keyed = true) */
+    private final RandomData randomData = new RandomDataImpl(); 
+    
+    /** Statistics on numActive */
     private SummaryStatistics numActiveStats = new SummaryStatistics();
+    
+    /** Statistics on numIdle */
     private SummaryStatistics numIdleStats = new SummaryStatistics();
-    private double samplingRate = 0;
+    
+    /** Sampling rate for numActive, numIdle */
+    private double samplingRate;
+    
+    /** Statistics on instance idle time */
+    private SummaryStatistics instanceIdleTimeStats = new SummaryStatistics();
+    
+    /** Just-borrowed Waiter instance (used to grab idle time stats in cleanUp) */
+    private Waiter waiter = null;
      
     /**
      * Create a pool client thread for an ObjectPool.
@@ -61,6 +84,8 @@ public class PoolClientThread extends Cl
      * @param logger common logger shared by all clients
      * @param stats Statistics container
      * @param pool ObjectPool
+     * @param samplingRate proportion of requests for which numIdle and numActive
+     * will be sampled for statistical analysis
      */
     public PoolClientThread(long iterations, long minDelay, long maxDelay,
             double sigma, String delayType, long rampPeriod, long peakPeriod,
@@ -71,6 +96,7 @@ public class PoolClientThread extends Cl
                 peakPeriod, troughPeriod, cycleType,rampType, logger, 
                 stats); 
         this.pool = pool;
+        this.keyedPool = null;
         this.keyed = false;
         this.samplingRate = samplingRate;
     }
@@ -90,6 +116,8 @@ public class PoolClientThread extends Cl
      * @param logger common logger shared by all clients
      * @param stats Statistics container 
      * @param keyedPool KeyedObjectPool
+     * @param samplingRate proportion of requests for which numIdle and numActive
+     * will be sampled for statistical analysis
      */
     public PoolClientThread(long iterations, long minDelay, long maxDelay,
             double sigma, String delayType, long rampPeriod, long peakPeriod,
@@ -101,24 +129,24 @@ public class PoolClientThread extends Cl
                 stats); 
         
         this.keyedPool = keyedPool;
+        this.pool = null;
         this.keyed = true;
         this.samplingRate = samplingRate;
         keys = new ArrayList<Integer>();
         for (int i = 0; i < 20; i++) { //TODO: make number of keys configurable
             keys.add(new Integer(i));
         }
-        randomData = new RandomDataImpl();
     }
     
     /** Borrow and return */
     public void execute() throws Exception {
        if (keyed) {
            Integer key = keys.get(randomData.nextInt(0, 19));
-           Waiter waiter = (Waiter) keyedPool.borrowObject(key);
+           waiter = (Waiter) keyedPool.borrowObject(key);
            waiter.doWait();
            keyedPool.returnObject(key, waiter);
        } else {
-           Waiter waiter = (Waiter) pool.borrowObject(); 
+           waiter = (Waiter) pool.borrowObject(); 
            waiter.doWait();
            pool.returnObject(waiter);
        }
@@ -135,13 +163,16 @@ public class PoolClientThread extends Cl
                 numActiveStats.addValue(pool.getNumActive());
             }
         }
+        instanceIdleTimeStats.addValue(waiter.getLastIdleTimeMs());
     }
     
     protected void finish() throws Exception {
-        // Add pool metrics to stats
+        // Add metrics to stats
         stats.addStatistics(
                 numIdleStats, Thread.currentThread().getName(), "numIdle");
         stats.addStatistics(
                 numActiveStats, Thread.currentThread().getName(), "numActive");
+        stats.addStatistics(
+                instanceIdleTimeStats, Thread.currentThread().getName(), "instance idle time");
     }
 }

Modified: commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/Waiter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/Waiter.java?rev=1001504&r1=1001503&r2=1001504&view=diff
==============================================================================
--- commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/Waiter.java (original)
+++ commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/Waiter.java Sun Sep 26 20:25:39 2010
@@ -18,20 +18,29 @@
 package org.apache.commons.performance.pool;
 
 /**
- * Object created by WaiterFactory. Maintains active / valid state.
+ * <p>Object created by {@link WaiterFactory}. Maintains active / valid state,
+ * last passivated and idle times.  Waits with configurable latency when 
+ * {@link #doWait()} method is called.</p>
  *
+ * <p>This class is *not* threadsafe.</p>
  */
 public class Waiter {
     private boolean active = false;
     private boolean valid = true;
-    long latency = 0;
+    private long latency = 0;
+    private long lastPassivated = 0;
+    private long lastIdleTimeMs = 0;
     
     public Waiter(boolean active, boolean valid, long latency) {
         this.active = active;
         this.valid = valid;
         this.latency = latency;
+        this.lastPassivated = System.currentTimeMillis();
     }
 
+    /**
+     * Wait for {@link #getLatency()} ms.
+     */
     public void doWait() {
         try {
             Thread.sleep(latency);
@@ -40,12 +49,43 @@ public class Waiter {
         }
     }
 
+    /**
+     * Whether or not the instance is active.
+     * 
+     * @return true if the last lifecycle event for this instance was activation.
+     */
     public boolean isActive() {
         return active;
     }
 
+    /**
+     * <p>Sets the active state and updates {@link #getLastIdleTimeMs() lastIdleTime}
+     * or {@link #getLastPassivated() lastPassivated} as appropriate.</p>
+     * 
+     * <p>If the active state is changing from inactive to active, lastIdleTime
+     * is updated with the current time minus lastPassivated.  If the state is
+     * changing from active to inactive, lastPassivated is updated with the
+     * current time.</p>
+     * 
+     * <p>{@link WaiterFactory#activateObject(Object)} and
+     * {@link WaiterFactory#passivateObject(Object)} invoke this method on their
+     * actual parameter, passing <code>true</code> and <code>false</code>,
+     * respectively.</p>
+     * 
+     * @param active new active state
+     */
     public void setActive(boolean active) {
+        final boolean activeState = this.active;
+        if (activeState == active) {
+            return;
+        }
         this.active = active;
+        final long currentTime = System.currentTimeMillis();
+        if (active) {  // activating
+            lastIdleTimeMs = currentTime - lastPassivated;
+        } else {       // passivating
+            lastPassivated = currentTime;
+        }
     }
 
     public long getLatency() {
@@ -63,4 +103,30 @@ public class Waiter {
     public void setValid(boolean valid) {
         this.valid = valid;
     }
+    
+    /**
+     * <p>Returns the system time of this instance's last passivation.</p>
+     * 
+     * <p>When an instance is created, this field is initialized to the system time.</p>
+     * 
+     * @return time of last passivation
+     */
+    public long getLastPassivated() {
+        return lastPassivated;
+    }
+    
+    /**
+     * <p>Returns the last idle time for this instance in ms.</p>
+     * 
+     * <p>When an instance is created, and each subsequent time it is passivated,
+     * the {@link #getLastPassivated() lastPassivated} property is updated with the
+     * current time.  When the next activation occurs, <code>lastIdleTime</code> is
+     * updated with the elapsed time since passivation.<p>
+     * 
+     * @return last idle time
+     */
+    public long getLastIdleTimeMs() {
+        return lastIdleTimeMs;
+    }
+    
 }

Modified: commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/WaiterFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/WaiterFactory.java?rev=1001504&r1=1001503&r2=1001504&view=diff
==============================================================================
--- commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/WaiterFactory.java (original)
+++ commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/pool/WaiterFactory.java Sun Sep 26 20:25:39 2010
@@ -85,19 +85,21 @@ KeyedPoolableObjectFactory {
                 validateLatency, waiterLatency, maxActive, Long.MAX_VALUE);
     }
 
-    public void activateObject(Object arg0) throws Exception {
+    public void activateObject(Object obj) throws Exception {
         if (logger.isLoggable(Level.FINEST)) {
             logger.finest("activate");
         }
         doWait(activateLatency);
+        ((Waiter) obj).setActive(true);
     }
 
-    public void destroyObject(Object arg0) throws Exception {
+    public void destroyObject(Object obj) throws Exception {
         if (logger.isLoggable(Level.FINE)) {
             logger.fine("destroy");
         }
         doWait(destroyLatency);
-        ((Waiter) arg0).setValid(false);
+        ((Waiter) obj).setValid(false);
+        ((Waiter) obj).setActive(false);
         // Decrement *after* destroy 
         synchronized (this) {
             activeCount--;