1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
181 ValueSet valueSet = (ValueSet) loadStimulus.getValueSetList().get(0);
182
183
184 IntRangeValueGenerator valueGenerator = new IntRangeValueGenerator(valueSet);
185
186 return valueGenerator;
187 }
188
189 protected TibrvMsg getStimuliMessage() {
190 Stimuli stimuli = getLoadTestFixture().getStimuli();
191
192
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 }