001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.performance;
019    
020    import java.util.logging.Logger;
021    
022    /**
023     * Test ClientThread 
024     */
025    public class TesterClientThread extends ClientThread {
026        
027        /** 
028         *  Distribution parameters for simulated service latency.
029         *  To configure constant delay - i.e., the same latency each time,
030         *  supply serviceDelayType = "constant" and meanServiceDelay = the
031         *  desired delay to the constructor.
032         */
033        private final long minServiceDelay;
034        private final long maxServiceDelay;
035        private final double meanServiceDelay;
036        private final double sigmaServiceDelay;
037        private final String serviceDelayType;
038        
039        /** 
040         * Lifecycle events trackers
041         */
042        private boolean initialized = false;
043        private boolean finalized = false;
044        private long setups = 0;
045        private long tearDowns = 0;
046        
047        /** to hurl or not to hurl  */
048        private boolean hurling = false;
049        
050        public boolean isHurling() {
051            return hurling;
052        }
053    
054        public void setHurling(boolean hurling) {
055            this.hurling = hurling;
056        }
057    
058        /** Executed once at the beginning of the run */
059        protected void init() throws Exception {
060            initialized = true;
061        }
062        
063        /** Executed at the beginning of each iteration */
064        protected void setUp() throws Exception {
065            setups++;
066        }
067        
068        /** Executed in finally block of iteration try-catch */
069        protected void cleanUp() throws Exception {
070            tearDowns++;
071        }
072        
073        /** Executed once after the run finishes */
074        protected void finish() throws Exception {
075            finalized = true;
076        }
077        
078       public boolean isInitialized() {
079            return initialized;
080        }
081    
082        public boolean isFinalized() {
083            return finalized;
084        }
085    
086        public long getSetups() {
087            return setups;
088        }
089    
090        public long getTearDowns() {
091            return tearDowns;
092        }
093    
094        public TesterClientThread(long iterations, long minDelay, long maxDelay,
095                double sigma, String delayType, long rampPeriod, long peakPeriod,
096                long troughPeriod, String cycleType, String rampType,
097                Logger logger, Statistics stats, long minServiceDelay,
098                long maxServiceDelay, double meanServiceDelay,
099                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    }