commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r749150 - in /commons/sandbox/performance/trunk/src: java/org/apache/commons/performance/ClientThread.java test/org/apache/commons/performance/ClientThreadTest.java test/org/apache/commons/performance/TesterClientThread.java
Date Mon, 02 Mar 2009 02:22:13 GMT
Author: psteitz
Date: Mon Mar  2 02:22:13 2009
New Revision: 749150

URL: http://svn.apache.org/viewvc?rev=749150&view=rev
Log:
Cleaned up ClientThread field access and added some tests.

Added:
    commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java
  (with props)
Modified:
    commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/ClientThread.java
    commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/ClientThreadTest.java

Modified: commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/ClientThread.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/ClientThread.java?rev=749150&r1=749149&r2=749150&view=diff
==============================================================================
--- commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/ClientThread.java
(original)
+++ commons/sandbox/performance/trunk/src/java/org/apache/commons/performance/ClientThread.java
Mon Mar  2 02:22:13 2009
@@ -50,48 +50,52 @@
 
     // Inter-arrival time configuration parameters 
     /** Minimum mean time between requests */
-    protected long minDelay;
-    /** Maxiimum mean time between requests */
-    protected long maxDelay;
+    private long minDelay;
+    /** Maximum mean time between requests */
+    private long maxDelay;
     /** Standard deviation of delay distribution */
-    protected double sigma;
+    private double sigma;
     /** Delay type - determines how next start times are computed */
-    protected String delayType;
+    private String delayType;
     /** Ramp length for cyclic mean delay */
-    protected long rampPeriod;
+    private long rampPeriod;
     /** Peak length for cyclic mean delay */
-    protected long peakPeriod;
+    private long peakPeriod;
     /** Trough length for cyclic mean delay */
-    protected long troughPeriod;
+    private long troughPeriod;
     /** Cycle type */
-    protected String cycleType;
+    private final String cycleType;
     /** Ramp type */
-    protected String rampType;
+    private String rampType;
     
     /** Number of iterations */
-    protected long iterations;
+    private final long iterations;
     
     // State data
     /** Start time of run */
-    protected long startTime;
+    private long startTime;
     /** Start time of current period */
-    protected long periodStart;
+    private long periodStart;
     /** Last mean delay */
-    protected double lastMean;
+    private double lastMean;
     /** Cycle state constants */
     protected static final int RAMPING_UP = 0;
     protected static final int RAMPING_DOWN = 1;
     protected static final int PEAK_LOAD = 2;
     protected static final int TROUGH_LOAD = 3;
     /** Cycle state */
-    protected int cycleState = RAMPING_UP;
+    private int cycleState = RAMPING_UP;
+    /** Number of errors */
+    private long numErrors = 0;
+    /** Number of misses */
+    private long numMisses = 0;
     
     /** Random data generator */
     protected RandomData randomData = new RandomDataImpl();
     /** Statistics container */
-    protected Statistics stats = new Statistics();
+    protected Statistics stats;
     /** Logger shared by client threads */
-    protected Logger logger = null;
+    protected Logger logger;
     
     /**
      * Create a client thread.
@@ -138,8 +142,6 @@
         long start = 0;
         startTime = System.currentTimeMillis();
         long lastStart = startTime;
-        long numMisses = 0;
-        long numErrors = 0;
         periodStart = System.currentTimeMillis();
         lastMean = (double) maxDelay; // Ramp up, if any, starts here
         SummaryStatistics responseStats = new SummaryStatistics();
@@ -472,4 +474,129 @@
             }
         }
     }
+
+    public long getMinDelay() {
+        return minDelay;
+    }
+
+    public long getMaxDelay() {
+        return maxDelay;
+    }
+
+    public double getSigma() {
+        return sigma;
+    }
+
+    public String getDelayType() {
+        return delayType;
+    }
+
+    public long getRampPeriod() {
+        return rampPeriod;
+    }
+
+    public long getPeakPeriod() {
+        return peakPeriod;
+    }
+
+    public long getTroughPeriod() {
+        return troughPeriod;
+    }
+
+    public String getCycleType() {
+        return cycleType;
+    }
+
+    public String getRampType() {
+        return rampType;
+    }
+
+    public long getIterations() {
+        return iterations;
+    }
+
+    public long getStartTime() {
+        return startTime;
+    }
+
+    public long getPeriodStart() {
+        return periodStart;
+    }
+
+    public double getLastMean() {
+        return lastMean;
+    }
+
+    public int getCycleState() {
+        return cycleState;
+    }
+
+    public long getNumErrors() {
+        return numErrors;
+    }
+
+    public long getNumMisses() {
+        return numMisses;
+    }
+
+    public Statistics getStats() {
+        return stats;
+    }
+
+    public void setStartTime(long startTime) {
+        this.startTime = startTime;
+    }
+
+    public void setPeriodStart(long periodStart) {
+        this.periodStart = periodStart;
+    }
+
+    public void setLastMean(double lastMean) {
+        this.lastMean = lastMean;
+    }
+
+    public void setCycleState(int cycleState) {
+        this.cycleState = cycleState;
+    }
+
+    public void setNumErrors(long numErrors) {
+        this.numErrors = numErrors;
+    }
+
+    public void setNumMisses(long numMisses) {
+        this.numMisses = numMisses;
+    }
+
+    public void setRampType(String rampType) {
+        this.rampType = rampType;
+    }
+
+    public void setMinDelay(long minDelay) {
+        this.minDelay = minDelay;
+    }
+
+    public void setMaxDelay(long maxDelay) {
+        this.maxDelay = maxDelay;
+    }
+
+    public void setSigma(double sigma) {
+        this.sigma = sigma;
+    }
+
+    public void setDelayType(String delayType) {
+        this.delayType = delayType;
+    }
+
+    public void setRampPeriod(long rampPeriod) {
+        this.rampPeriod = rampPeriod;
+    }
+
+    public void setPeakPeriod(long peakPeriod) {
+        this.peakPeriod = peakPeriod;
+    }
+
+    public void setTroughPeriod(long troughPeriod) {
+        this.troughPeriod = troughPeriod;
+    }
+    
 }

Modified: commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/ClientThreadTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/ClientThreadTest.java?rev=749150&r1=749149&r2=749150&view=diff
==============================================================================
--- commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/ClientThreadTest.java
(original)
+++ commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/ClientThreadTest.java
Mon Mar  2 02:22:13 2009
@@ -41,7 +41,6 @@
       public void execute() {}
   }
   
-
   public ClientThreadTest(String name) {
     super(name);
   }
@@ -71,100 +70,158 @@
   // ======================================================
   
   public void testComputeCyclicDelayRamp() throws Exception {
-      clientThread.cycleState = ClientThread.RAMPING_UP;
-      clientThread.periodStart = 1000;
+      clientThread.setCycleState(ClientThread.RAMPING_UP);
+      clientThread.setPeriodStart(1000);
       assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
       assertEquals(110, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
-      clientThread.cycleState = ClientThread.RAMPING_DOWN;
+      clientThread.setCycleState(ClientThread.RAMPING_DOWN);
       assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
       assertEquals(190, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
   }
   
   public void testComputeCyclicDelayConst() throws Exception {
-      clientThread.cycleState = ClientThread.PEAK_LOAD;
-      clientThread.periodStart = 1000;
+      clientThread.setCycleState(ClientThread.PEAK_LOAD);
+      clientThread.setPeriodStart(1000);
       assertEquals(100, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
       assertEquals(100, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
-      clientThread.cycleState = ClientThread.TROUGH_LOAD;
+      clientThread.setCycleState(ClientThread.TROUGH_LOAD);
       assertEquals(200, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
       assertEquals(200, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
   }
   
   public void testCyclicDelayRandom() throws Exception {
-      clientThread.rampType = "random";
-      clientThread.cycleState = ClientThread.RAMPING_UP;
-      clientThread.periodStart = 1000;
-      clientThread.lastMean = 200;
+      clientThread.setRampType("random");
+      clientThread.setCycleState(ClientThread.RAMPING_UP);
+      clientThread.setPeriodStart(1000);
+      clientThread.setLastMean(200);
       for (int i = 1; i < 10; i++) {
           double nextMean = clientThread.computeCyclicDelay(1500, 100, 200);
           assertTrue(nextMean <= 200 && nextMean >= 100 &&
-                  nextMean <= clientThread.lastMean);
-          clientThread.lastMean = nextMean;
+                  nextMean <= clientThread.getLastMean());
+          clientThread.setLastMean(nextMean);
       }
-      clientThread.cycleState = ClientThread.RAMPING_DOWN;
-      clientThread.periodStart = 1000;
-      clientThread.lastMean = 100;
+      clientThread.setCycleState(ClientThread.RAMPING_DOWN);
+      clientThread.setPeriodStart(1000);
+      clientThread.setLastMean(100);
       for (int i = 1; i < 10; i++) {
           double nextMean = clientThread.computeCyclicDelay(1500, 100, 200);
           assertTrue(nextMean <= 200 && nextMean >= 100 &&
-                  nextMean >= clientThread.lastMean);
-          clientThread.lastMean = nextMean;
+                  nextMean >= clientThread.getLastMean());
+          clientThread.setLastMean(nextMean);
       }
   }
-  
 
   // ======================================================
   //   adjustState tests
   // ======================================================
   public void testAdjustStateNoChange() throws Exception {
-      clientThread.periodStart = 1000;
-      clientThread.cycleState = ClientThread.RAMPING_UP;
-      clientThread.rampPeriod = 1000;
+      clientThread.setPeriodStart(1000);
+      clientThread.setCycleState(ClientThread.RAMPING_UP);
+      clientThread.setRampPeriod(1000);
       clientThread.adjustState(1100);
-      assertEquals(ClientThread.RAMPING_UP, clientThread.cycleState);
-      clientThread.cycleState = ClientThread.RAMPING_DOWN;
+      assertEquals(ClientThread.RAMPING_UP, clientThread.getCycleState());
+      clientThread.setCycleState(ClientThread.RAMPING_DOWN);
       clientThread.adjustState(1100);
-      assertEquals(ClientThread.RAMPING_DOWN, clientThread.cycleState);
-      clientThread.cycleState = ClientThread.PEAK_LOAD;
-      clientThread.peakPeriod = 1000;
+      assertEquals(ClientThread.RAMPING_DOWN, clientThread.getCycleState());
+      clientThread.setCycleState(ClientThread.PEAK_LOAD);
+      clientThread.setPeakPeriod(1000);
       clientThread.adjustState(1100);
-      assertEquals(ClientThread.PEAK_LOAD, clientThread.cycleState);
-      clientThread.cycleState = ClientThread.TROUGH_LOAD;
-      clientThread.peakPeriod = 1000;
+      assertEquals(ClientThread.PEAK_LOAD, clientThread.getCycleState());
+      clientThread.setCycleState(ClientThread.TROUGH_LOAD);
+      clientThread.setPeakPeriod(1000);
       clientThread.adjustState(1100);
-      assertEquals(ClientThread.TROUGH_LOAD, clientThread.cycleState);
+      assertEquals(ClientThread.TROUGH_LOAD, clientThread.getCycleState());
   }
   
   public void testStateTransitions() throws Exception {
-      clientThread.peakPeriod = 1500;
-      clientThread.rampPeriod = 1000;
-      clientThread.troughPeriod = 2000;
+      clientThread.setPeakPeriod(1500);
+      clientThread.setRampPeriod(1000);
+      clientThread.setTroughPeriod(2000);
       
       // Ramping up to peak
-      clientThread.periodStart = 1000;
-      clientThread.cycleState = ClientThread.RAMPING_UP;
+      clientThread.setPeriodStart(1000);
+      clientThread.setCycleState(ClientThread.RAMPING_UP);
       clientThread.adjustState(2100);
-      assertEquals(ClientThread.PEAK_LOAD, clientThread.cycleState);
-      assertEquals(2100, clientThread.periodStart);
-      assertEquals((double) clientThread.minDelay, clientThread.lastMean);
+      assertEquals(ClientThread.PEAK_LOAD, clientThread.getCycleState());
+      assertEquals(2100, clientThread.getPeriodStart());
+      assertEquals((double) clientThread.getMinDelay(), clientThread.getLastMean());
       
       // Peak to ramping down
       clientThread.adjustState(3700);
-      assertEquals(ClientThread.RAMPING_DOWN, clientThread.cycleState);
-      assertEquals(3700, clientThread.periodStart);
-      assertEquals((double) clientThread.minDelay, clientThread.lastMean);
+      assertEquals(ClientThread.RAMPING_DOWN, clientThread.getCycleState());
+      assertEquals(3700, clientThread.getPeriodStart());
+      assertEquals((double) clientThread.getMinDelay(), clientThread.getLastMean());
       
       // Ramping down to trough
       clientThread.adjustState(4800);
-      assertEquals(ClientThread.TROUGH_LOAD, clientThread.cycleState);
-      assertEquals(4800, clientThread.periodStart);
-      assertEquals((double) clientThread.maxDelay, clientThread.lastMean); 
+      assertEquals(ClientThread.TROUGH_LOAD, clientThread.getCycleState());
+      assertEquals(4800, clientThread.getPeriodStart());
+      assertEquals((double) clientThread.getMaxDelay(), clientThread.getLastMean()); 
       
       // Trough to ramping up
       clientThread.adjustState(6900);
-      assertEquals(ClientThread.RAMPING_UP, clientThread.cycleState);
-      assertEquals(6900, clientThread.periodStart);
-      assertEquals((double) clientThread.maxDelay, clientThread.lastMean);   
+      assertEquals(ClientThread.RAMPING_UP, clientThread.getCycleState());
+      assertEquals(6900, clientThread.getPeriodStart());
+      assertEquals((double) clientThread.getMaxDelay(), clientThread.getLastMean());   
+  }
+  
+  //=======================================================
+  // Lifecycle tests
+  //=======================================================
+  
+  public void testLifeCycle() {
+      TesterClientThread testerThread = new TesterClientThread(
+              10, // iterations
+              100,  // minDelay
+              1000, // maxDelay
+              100,  // sigma
+              "constant", // delayType
+              1000, // ramp period
+              2000, // peak period
+              3000, // trough period
+              "oscillating", // cycle type
+              "linear", // ramp type
+              logger,
+              stats, 
+              0,          // min service delay
+              100,        // max service delay
+              50,         // mean service delay
+              10,         // std dev of service delay
+              "uniform");   // service delay distribution
+       assertFalse(testerThread.isInitialized());
+       testerThread.run();
+       assertEquals(10, testerThread.getSetups());
+       assertEquals(10, testerThread.getTearDowns());
+       assertTrue(testerThread.isFinalized());
+       assertTrue(testerThread.isInitialized());
+  }
+  
+  public void testLifeCycleThrowing() {
+      TesterClientThread testerThread = new TesterClientThread(
+              10, // iterations
+              100,  // minDelay
+              1000, // maxDelay
+              100,  // sigma
+              "constant", // delayType
+              1000, // ramp period
+              2000, // peak period
+              3000, // trough period
+              "oscillating", // cycle type
+              "linear", // ramp type
+              logger,
+              stats, 
+              0,          // min service delay
+              100,        // max service delay
+              50,         // mean service delay
+              10,         // std dev of service delay
+              "uniform");   // service delay distribution
+       assertFalse(testerThread.isInitialized());
+       testerThread.setHurling(true);
+       testerThread.run();
+       assertEquals(10, testerThread.getSetups());
+       assertEquals(10, testerThread.getTearDowns());
+       assertTrue(testerThread.isFinalized());
+       assertTrue(testerThread.isInitialized());
   }
   
 }

Added: commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java
URL: http://svn.apache.org/viewvc/commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java?rev=749150&view=auto
==============================================================================
--- commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java
(added)
+++ commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java
Mon Mar  2 02:22:13 2009
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.performance;
+
+import java.util.logging.Logger;
+
+/**
+ * Test ClientThread 
+ */
+public class TesterClientThread extends ClientThread {
+    
+    /** 
+     *  Distribution parameters for simulated service latency.
+     *  To configure constant delay - i.e., the same latency each time,
+     *  supply serviceDelayType = "constant" and meanServiceDelay = the
+     *  desired delay to the constructor.
+     */
+    private final long minServiceDelay;
+    private final long maxServiceDelay;
+    private final double meanServiceDelay;
+    private final double sigmaServiceDelay;
+    private final String serviceDelayType;
+    
+    /** 
+     * Lifecycle events trackers
+     */
+    private boolean initialized = false;
+    private boolean finalized = false;
+    private long setups = 0;
+    private long tearDowns = 0;
+    
+    /** to hurl or not to hurl  */
+    private boolean hurling = false;
+    
+    public boolean isHurling() {
+        return hurling;
+    }
+
+    public void setHurling(boolean hurling) {
+        this.hurling = hurling;
+    }
+
+    /** Executed once at the beginning of the run */
+    protected void init() throws Exception {
+        initialized = true;
+    }
+    
+    /** Executed at the beginning of each iteration */
+    protected void setUp() throws Exception {
+        setups++;
+    }
+    
+    /** Executed in finally block of iteration try-catch */
+    protected void cleanUp() throws Exception {
+        tearDowns++;
+    }
+    
+    /** Executed once after the run finishes */
+    protected void finish() throws Exception {
+        finalized = true;
+    }
+    
+   public boolean isInitialized() {
+        return initialized;
+    }
+
+    public boolean isFinalized() {
+        return finalized;
+    }
+
+    public long getSetups() {
+        return setups;
+    }
+
+    public long getTearDowns() {
+        return tearDowns;
+    }
+
+    public TesterClientThread(long iterations, long minDelay, long maxDelay,
+            double sigma, String delayType, long rampPeriod, long peakPeriod,
+            long troughPeriod, String cycleType, String rampType,
+            Logger logger, Statistics stats, long minServiceDelay,
+            long maxServiceDelay, double meanServiceDelay,
+            double sigmaServiceDelay, String serviceDelayType) {
+    
+        super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
+                peakPeriod, troughPeriod, cycleType, rampType, logger,
+                stats);
+        this.minServiceDelay = minServiceDelay;
+        this.maxServiceDelay = maxServiceDelay;
+        this.meanServiceDelay = meanServiceDelay;
+        this.sigmaServiceDelay = sigmaServiceDelay;
+        this.serviceDelayType = serviceDelayType;
+    }
+    
+    /** 
+     * Simulate server latency using service latency parameters
+     */
+   public void execute() throws Exception {
+       if (hurling) {
+           throw new RuntimeException("Bang!");
+       }
+       if (meanServiceDelay <= 0) {
+           return;
+       }
+       if (serviceDelayType.equals("constant")) {
+           Thread.sleep(Math.round(meanServiceDelay));
+       } else if (serviceDelayType.equals("gaussian")) {
+           Thread.sleep(Math.round(randomData.nextGaussian(
+                   meanServiceDelay, sigmaServiceDelay))); 
+       } else if (serviceDelayType.equals("poisson")) {
+           Thread.sleep(Math.round(
+                   randomData.nextPoisson(meanServiceDelay)));
+       }
+       else if (serviceDelayType.equals("uniform")) {
+           Thread.sleep(Math.round(
+                   randomData.nextUniform(minServiceDelay, maxServiceDelay)));
+       }
+        
+    }
+}

Propchange: commons/sandbox/performance/trunk/src/test/org/apache/commons/performance/TesterClientThread.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message