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 package org.apache.commons.performance;
018
019 import java.util.logging.Logger;
020 import junit.framework.Test;
021 import junit.framework.TestCase;
022 import junit.framework.TestSuite;
023 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
024 import org.apache.commons.math.random.RandomData;
025 import org.apache.commons.math.random.RandomDataImpl;
026
027 public class LoadGeneratorTest extends TestCase {
028
029 protected TestLoadGenerator generator = null;
030 protected static Logger logger = Logger.getLogger(LoadGenerator.class.getName());
031 protected static Statistics stats = new Statistics();
032
033 class TestClientThread extends ClientThread {
034 private long latency = 50;
035 private double metricOne = 10d;
036 private double metricTwo = 20d;
037 private SummaryStatistics oneStats = new SummaryStatistics();
038 private SummaryStatistics twoStats = new SummaryStatistics();
039 private SummaryStatistics randomStats = new SummaryStatistics();
040 private RandomData randomData = new RandomDataImpl();
041 public TestClientThread(long iterations, long minDelay, long maxDelay,
042 double sigma, String delayType, long rampPeriod,
043 long peakPeriod, long troughPeriod, String cycleType,
044 String rampType, Logger logger,
045 Statistics stats) {
046 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
047 peakPeriod, troughPeriod, cycleType, rampType, logger,
048 stats);
049 }
050 public void setLatency(long latency) {
051 this.latency = latency;
052 }
053 public void execute() throws Exception {
054 Thread.sleep(latency);
055 }
056 protected void cleanUp() {
057 oneStats.addValue(metricOne);
058 twoStats.addValue(metricTwo);
059 randomStats.addValue(randomData.nextUniform(0d, 1d));
060 }
061 protected void finish() {
062 stats.addStatistics(
063 oneStats, Thread.currentThread().getName(), "one");
064 stats.addStatistics(
065 twoStats, Thread.currentThread().getName(), "two");
066 stats.addStatistics(
067 randomStats, Thread.currentThread().getName(), "random");
068 }
069 }
070
071 class TestLoadGenerator extends LoadGenerator {
072 protected ClientThread makeClientThread(long iterations, long minDelay,
073 long maxDelay, double sigma, String delayType, long rampPeriod,
074 long peakPeriod, long troughPeriod, String cycleType,
075 String rampType, Logger logger, Statistics stats) {
076 return new TestClientThread(iterations, minDelay,
077 maxDelay, sigma, delayType, rampPeriod,
078 peakPeriod, troughPeriod, cycleType,
079 rampType, logger, stats);
080 }
081 protected void parseConfigFile() throws Exception {
082 getDigester().parse(this.getClass().getResourceAsStream(
083 "/org/apache/commons/performance/pool/config-pool.xml"));
084 }
085 }
086
087
088 public LoadGeneratorTest(String name) {
089 super(name);
090 }
091
092
093 public static Test suite() {
094 return new TestSuite(LoadGeneratorTest.class);
095 }
096
097 public void setUp() throws Exception {
098 generator = new TestLoadGenerator();
099 }
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 }