View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Copyright (c) 2002, Reuters All rights reserved.
34   *
35   * Redistribution and use in source and binary forms, with or without
36   * modification, are permitted provided that the following conditions are met:
37   *
38   * Redistributions of source code must retain the above copyright notice, this
39   * list of conditions and the following disclaimer. Redistributions in binary
40   * form must reproduce the above copyright notice, this list of conditions and
41   * the following disclaimer in the documentation and/or other materials provided
42   * with the distribution. Neither the name of the Reuters nor the names of its
43   * contributors may be used to endorse or promote products derived from this
44   * software without specific prior written permission.
45   *
46   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56   * POSSIBILITY OF SUCH DAMAGE.
57   *
58   */
59  package com.reuters.msgtest.load;
60  
61  import com.reuters.msgtest.RvRecorder;
62  import com.reuters.msgtest.RvTestCase;
63  import com.reuters.msgtest.RvTestException;
64  import com.reuters.msgtest.fixture.Response;
65  import com.reuters.msgtest.fixture.Stimuli;
66  import com.reuters.msgtest.fixture.Stimulus;
67  import com.reuters.msgtest.load.config.KeyField;
68  import com.reuters.msgtest.load.config.LoadStimulus;
69  import com.reuters.msgtest.load.config.LoadTestFixture;
70  import com.reuters.msgtest.load.output.LoadTestOutput;
71  import com.reuters.msgtest.load.value.IntRangeValueGenerator;
72  import com.reuters.msgtest.load.value.ValueGenerator;
73  import com.reuters.msgtest.load.value.ValueSet;
74  import com.tibco.tibrv.TibrvException;
75  import com.tibco.tibrv.TibrvMsg;
76  import com.tibco.tibrv.TibrvTransport;
77  import java.io.BufferedWriter;
78  import java.io.File;
79  import java.io.FileWriter;
80  import java.io.IOException;
81  import java.io.Writer;
82  import java.lang.reflect.Constructor;
83  import java.lang.reflect.InvocationTargetException;
84  import java.util.Iterator;
85  import java.util.LinkedList;
86  import java.util.List;
87  import java.util.Random;
88  
89  
90  public abstract class LoadTest extends RvTestCase {
91      public static final Random RANDOM = new Random(System.currentTimeMillis());
92      public final TibrvMsg NULL_TIBRV_MESSAGE = new TibrvMsg();
93      protected TibrvMsgFieldMutator fieldMutator = new TibrvMsgFieldMutator();
94      protected LoadTestResults loadTestResults;
95      private LoadTestFixture loadTestFixture;
96      private RvRecorder recorder;
97  
98      public LoadTest(String name, File file) {
99          super(name);
100         setLoadTestFixture(new LoadTestFixture(getRvTestConfiguration(), file));
101         setLoadTestResults(new LoadTestResults(getLoadTestFixture()
102                                                    .getLoadTestConfig().getName()));
103     }
104 
105     public void testLoad() throws Exception {
106         assertNotNull("Load Test Result Object has to be instantiated",
107             getLoadTestResults());
108         startRecording();
109         processStimuli();
110         getResponses();
111         printResults();
112     }
113 
114     abstract public void getResponses() throws RvTestException;
115 
116     abstract public void processStimuli() throws RvTestException;
117 
118     public void startRecording() throws TibrvException {
119         setRecorder(new RvRecorder(getRvTestConfiguration().getTransport()));
120         getRecorder().start();
121 
122         for (Iterator iter = getLoadTestFixture().getResponses().iterator();
123                 iter.hasNext();) {
124             Response response = (Response) iter.next();
125             String subject = response.getTibrvMsg().getSendSubject();
126             assertNotNull(subject);
127             recorder.record(subject);
128         }
129     }
130 
131     public Writer createResultFileWriter() throws RvTestException {
132         String outputFileName = getLoadTestFixture().getLoadTestConfig()
133                                     .getOutputFileName();
134 
135         if ((outputFileName == null) || (outputFileName.length() < 1)) {
136             outputFileName = "output.txt";
137         }
138 
139         try {
140             return new BufferedWriter(new FileWriter(new File(outputFileName),
141                     true));
142         } catch (IOException e) {
143             throw new RvTestException("Could not create output file", e);
144         }
145     }
146 
147     public LoadTestFixture getLoadTestFixture() {
148         return loadTestFixture;
149     }
150 
151     public void setLoadTestFixture(LoadTestFixture loadTestFixture) {
152         this.loadTestFixture = loadTestFixture;
153     }
154 
155     public RvRecorder getRecorder() {
156         return recorder;
157     }
158 
159     public void setRecorder(RvRecorder recorder) {
160         this.recorder = recorder;
161     }
162 
163     public LoadTestResults getLoadTestResults() {
164         return loadTestResults;
165     }
166 
167     public void setLoadTestResults(LoadTestResults loadTestResults) {
168         this.loadTestResults = loadTestResults;
169     }
170 
171     protected TibrvTransport getTibrvTransport() throws RvTestException {
172         try {
173             return getRvTestConfiguration().getTransport().getRvdTransport();
174         } catch (TibrvException e) {
175             throw new RvTestException("Could not aquire transport object.", e);
176         }
177     }
178 
179     protected ValueGenerator createValueGenerator(LoadStimulus loadStimulus) {
180         // TODO: we are directly referencing index, 
181         ValueSet valueSet = (ValueSet) loadStimulus.getValueSetList().get(0);
182 
183         // TODO: make sure other types (besides Integer Range) are implemented
184         IntRangeValueGenerator valueGenerator = new IntRangeValueGenerator(valueSet);
185 
186         return valueGenerator;
187     }
188 
189     protected TibrvMsg getStimuliMessage() {
190         Stimuli stimuli = getLoadTestFixture().getStimuli();
191 
192         //TODO directly referncing Stimulus using index
193         Stimulus stimulus = stimuli.getStimulus(0);
194         TibrvMsg templateMessage = stimulus.getMsg();
195 
196         return templateMessage;
197     }
198 
199     protected void modifyMessageFields(TibrvMsg msg, List keyFields,
200         ValueGenerator valueGenerator) throws RvTestException {
201         if (keyFields == null) {
202             return;
203         }
204 
205         for (Iterator iter = keyFields.iterator(); iter.hasNext();) {
206             KeyField fieldToModify = (KeyField) iter.next();
207             String value = valueGenerator.getCurrentValue(fieldToModify.getFillpattern());
208             fieldMutator.updateField(msg, fieldToModify.getName(), value);
209         }
210     }
211 
212     protected List createMessagesToSend(final TibrvMsg templateMessage,
213         LoadStimulus loadStimulus) throws RvTestException {
214         ValueGenerator valueGenerator = createValueGenerator(loadStimulus);
215         List messages = new LinkedList();
216         List keyFields = loadStimulus.getKeyFieldList();
217 
218         for (int i = 0; i < loadStimulus.getNumberOfMsgs(); i++) {
219             try {
220                 TibrvMsg msg = new TibrvMsg(templateMessage);
221                 modifyMessageFields(msg, keyFields, valueGenerator);
222                 messages.add(msg);
223             } catch (TibrvException e) {
224                 throw new RvTestException("Could prepare messages to send.", e);
225             }
226 
227             valueGenerator.changeValue();
228         }
229 
230         return messages;
231     }
232 
233     public void printResults() throws RvTestException {
234         String outputterClassName = getLoadTestFixture().getLoadTestConfig()
235                                         .getOutputPrinter();
236         LoadTestOutput outputter = createPrinterObj(outputterClassName);
237         outputter.outputResults(loadTestResults, createResultFileWriter());
238     }
239 
240     protected void setSendSubject(TibrvMsg message, String subjectToSend)
241         throws RvTestException {
242         try {
243             message.setSendSubject(subjectToSend);
244         } catch (TibrvException e) {
245             throw new RvTestException("Could not set message send subject.", e);
246         }
247     }
248 
249     protected void delay(int delayTime) {
250         if (delayTime > 0) {
251             try {
252                 Thread.sleep(delayTime);
253             } catch (InterruptedException e) {
254             }
255         }
256     }
257 
258     protected LoadTestOutput createPrinterObj(String outputterName)
259         throws RvTestException {
260         try {
261             Class clazz = CustomStimuliLoadTestCase.class.getClassLoader()
262                                                          .loadClass(outputterName);
263             Constructor constructor = clazz.getDeclaredConstructor(new Class[] {  });
264 
265             return (LoadTestOutput) constructor.newInstance(new Object[] {  });
266         } catch (ClassNotFoundException e) {
267             throw new RvTestException("Could not load a class " +
268                 outputterName, e);
269         } catch (NoSuchMethodException e) {
270             throw new RvTestException("Error printing load test results.", e);
271         } catch (IllegalArgumentException e) {
272             throw new RvTestException("Error printing load test results.", e);
273         } catch (InstantiationException e) {
274             throw new RvTestException("Error printing load test results.", e);
275         } catch (IllegalAccessException e) {
276             throw new RvTestException("Error printing load test results.", e);
277         } catch (InvocationTargetException e) {
278             throw new RvTestException("Error printing load test results.", e);
279         }
280     }
281 }