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.http;
019    
020    import java.util.logging.Logger;
021    
022    import org.apache.commons.performance.ClientThread;
023    import org.apache.commons.performance.LoadGenerator;
024    import org.apache.commons.performance.Statistics;
025    
026    /**
027     * Simple http load / performance tester, providing another LoadGenerator
028     * example.
029     * 
030     * Uses Commons Digester to parse and load configuration and spawns
031     * HTTPClientThread instances to generate load and gather statistics.
032     *
033     */
034    
035    public class HttpSoak extends LoadGenerator {
036        
037        private String url;
038        private String method;
039        private int socketTimeout;
040        private String successKey;
041        
042        /** Nothing to do here yet */
043        protected void init() throws Exception {}
044        
045        protected ClientThread makeClientThread(
046                long iterations, long minDelay, long maxDelay, double sigma,
047                String delayType, long rampPeriod, long peakPeriod,
048                long troughPeriod, String cycleType, String rampType,
049                Logger logger, Statistics stats) {
050            
051            return new HttpClientThread(iterations, minDelay, maxDelay,
052                sigma, delayType, rampPeriod, peakPeriod, 
053                troughPeriod, cycleType, rampType, logger,
054                stats, url, method, socketTimeout, successKey);
055        }
056        
057        /**
058         * Add http client configuration to parameters loaded by super.
059         * Also set config file name.
060         */
061        protected void configure() throws Exception {
062            
063            super.configure(); // loads run configuration
064            
065            digester.addCallMethod("configuration/http",
066                    "configureHttp", 4);
067            digester.addCallParam("configuration/http/url", 0);
068            digester.addCallParam("configuration/http/method", 1);
069            digester.addCallParam("configuration/http/socket-timeout", 2);
070            digester.addCallParam("configuration/http/success-key", 3);
071            
072            this.configFile = "config-http.xml";
073        }
074        
075        // ------------------------------------------------------------------------
076        // Configuration methods specific to this LoadGenerator invoked by Digester
077        // when superclass execute calls digester.parse.
078        // ------------------------------------------------------------------------
079        public void configureHttp(String url, 
080                String method, String socketTimeout, String successKey) {
081            this.url = url;
082            this.method = method;
083            this.socketTimeout = Integer.parseInt(socketTimeout);
084            this.successKey = successKey;
085        }
086    }