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  package org.apache.commons.performance;
18  
19  import java.util.logging.Logger;
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  import org.apache.commons.math.stat.descriptive.SummaryStatistics;
24  import org.apache.commons.math.random.RandomData;
25  import org.apache.commons.math.random.RandomDataImpl;
26  
27  public class LoadGeneratorTest extends TestCase {
28      
29    protected TestLoadGenerator generator = null;
30    protected static Logger logger = Logger.getLogger(LoadGenerator.class.getName());
31    protected static Statistics stats = new Statistics();
32    
33    class TestClientThread extends ClientThread {
34        private long latency = 50;
35        private double metricOne = 10d;
36        private double metricTwo = 20d;
37        private SummaryStatistics oneStats = new SummaryStatistics();
38        private SummaryStatistics twoStats = new SummaryStatistics();
39        private SummaryStatistics randomStats = new SummaryStatistics();
40        private RandomData randomData = new RandomDataImpl();
41        public TestClientThread(long iterations, long minDelay, long maxDelay,
42                double sigma, String delayType, long rampPeriod,
43                long peakPeriod, long troughPeriod, String cycleType,
44                String rampType, Logger logger,
45                Statistics stats) {    
46            super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
47                    peakPeriod, troughPeriod, cycleType, rampType, logger,
48                    stats);
49        }
50        public void setLatency(long latency) {
51            this.latency = latency;
52        }
53        public void execute() throws Exception {
54            Thread.sleep(latency);
55        }
56        protected void cleanUp() {
57            oneStats.addValue(metricOne);
58            twoStats.addValue(metricTwo);
59            randomStats.addValue(randomData.nextUniform(0d, 1d));
60        }
61        protected void finish() {
62            stats.addStatistics(
63                    oneStats, Thread.currentThread().getName(), "one");
64            stats.addStatistics(
65                    twoStats, Thread.currentThread().getName(), "two");
66            stats.addStatistics(
67                    randomStats, Thread.currentThread().getName(), "random");   
68        }
69    }
70    
71    class TestLoadGenerator extends LoadGenerator {
72        protected ClientThread makeClientThread(long iterations, long minDelay,
73                long maxDelay, double sigma, String delayType, long rampPeriod,
74                long peakPeriod, long troughPeriod, String cycleType, 
75                String rampType, Logger logger, Statistics stats) {
76            return new TestClientThread(iterations, minDelay,
77                maxDelay, sigma, delayType, rampPeriod,
78                peakPeriod, troughPeriod, cycleType, 
79                rampType, logger, stats);
80        }
81        protected void parseConfigFile() throws Exception {
82            getDigester().parse(this.getClass().getResourceAsStream(
83                    "/org/apache/commons/performance/pool/config-pool.xml"));
84        }
85    }
86    
87  
88    public LoadGeneratorTest(String name) {
89      super(name);
90    }
91  
92  
93    public static Test suite() {
94      return new TestSuite(LoadGeneratorTest.class);
95    }
96    
97    public void setUp() throws Exception {
98       generator = new TestLoadGenerator();
99    }
100 
101   public void testStatistics() throws Exception {
102       generator.execute();
103       Statistics statistics = generator.getStatistics();
104       SummaryStatistics stats = null;
105       stats = statistics.getMeanSummary("latency");
106       assertEquals(50, stats.getMean(), 100.0);
107       stats = statistics.getMeanSummary("one");
108       assertEquals(10, stats.getMean(), 1.0);
109       stats = statistics.getMeanSummary("two");
110       assertEquals(20, stats.getMean(), 1.0);
111       stats = statistics.getMeanSummary("random");
112       assertEquals(0.5, stats.getMean(), 0.25);
113   }
114   
115 }