1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.performance;
19  
20  import java.util.logging.Logger;
21  
22  /**
23   * Test ClientThread 
24   */
25  public class TesterClientThread extends ClientThread {
26      
27      /** 
28       *  Distribution parameters for simulated service latency.
29       *  To configure constant delay - i.e., the same latency each time,
30       *  supply serviceDelayType = "constant" and meanServiceDelay = the
31       *  desired delay to the constructor.
32       */
33      private final long minServiceDelay;
34      private final long maxServiceDelay;
35      private final double meanServiceDelay;
36      private final double sigmaServiceDelay;
37      private final String serviceDelayType;
38      
39      /** 
40       * Lifecycle events trackers
41       */
42      private boolean initialized = false;
43      private boolean finalized = false;
44      private long setups = 0;
45      private long tearDowns = 0;
46      
47      /** to hurl or not to hurl  */
48      private boolean hurling = false;
49      
50      public boolean isHurling() {
51          return hurling;
52      }
53  
54      public void setHurling(boolean hurling) {
55          this.hurling = hurling;
56      }
57  
58      /** Executed once at the beginning of the run */
59      protected void init() throws Exception {
60          initialized = true;
61      }
62      
63      /** Executed at the beginning of each iteration */
64      protected void setUp() throws Exception {
65          setups++;
66      }
67      
68      /** Executed in finally block of iteration try-catch */
69      protected void cleanUp() throws Exception {
70          tearDowns++;
71      }
72      
73      /** Executed once after the run finishes */
74      protected void finish() throws Exception {
75          finalized = true;
76      }
77      
78     public boolean isInitialized() {
79          return initialized;
80      }
81  
82      public boolean isFinalized() {
83          return finalized;
84      }
85  
86      public long getSetups() {
87          return setups;
88      }
89  
90      public long getTearDowns() {
91          return tearDowns;
92      }
93  
94      public TesterClientThread(long iterations, long minDelay, long maxDelay,
95              double sigma, String delayType, long rampPeriod, long peakPeriod,
96              long troughPeriod, String cycleType, String rampType,
97              Logger logger, Statistics stats, long minServiceDelay,
98              long maxServiceDelay, double meanServiceDelay,
99              double sigmaServiceDelay, String serviceDelayType) {
100     
101         super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
102                 peakPeriod, troughPeriod, cycleType, rampType, logger,
103                 stats);
104         this.minServiceDelay = minServiceDelay;
105         this.maxServiceDelay = maxServiceDelay;
106         this.meanServiceDelay = meanServiceDelay;
107         this.sigmaServiceDelay = sigmaServiceDelay;
108         this.serviceDelayType = serviceDelayType;
109     }
110     
111     /** 
112      * Simulate server latency using service latency parameters
113      */
114    public void execute() throws Exception {
115        if (hurling) {
116            throw new RuntimeException("Bang!");
117        }
118        if (meanServiceDelay <= 0) {
119            return;
120        }
121        if (serviceDelayType.equals("constant")) {
122            Thread.sleep(Math.round(meanServiceDelay));
123        } else if (serviceDelayType.equals("gaussian")) {
124            Thread.sleep(Math.round(randomData.nextGaussian(
125                    meanServiceDelay, sigmaServiceDelay))); 
126        } else if (serviceDelayType.equals("poisson")) {
127            Thread.sleep(Math.round(
128                    randomData.nextPoisson(meanServiceDelay)));
129        }
130        else if (serviceDelayType.equals("uniform")) {
131            Thread.sleep(Math.round(
132                    randomData.nextUniform(minServiceDelay, maxServiceDelay)));
133        }
134         
135     }
136 }