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:
019: package org.apache.jmeter.samplers;
020:
021: import java.io.StringWriter;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.jmeter.util.Calculator;
026: import org.apache.log.LogTarget;
027: import org.apache.log.format.Formatter;
028: import org.apache.log.format.RawFormatter;
029: import org.apache.log.output.io.WriterTarget;
030:
031: // TODO need more tests - particularly for the new functions
032:
033: public class TestSampleResult extends TestCase {
034: public TestSampleResult(String name) {
035: super (name);
036: }
037:
038: public void testElapsed() throws Exception {
039: SampleResult res = new SampleResult();
040:
041: // Check sample increments OK
042: res.sampleStart();
043: Thread.sleep(110); // Needs to be greater than the minimum to allow for boundary errors
044: res.sampleEnd();
045: long time = res.getTime();
046: if (time < 100) {
047: fail("Sample time should be >=100, actual " + time);
048: }
049: }
050:
051: public void testPause() throws Exception {
052: SampleResult res = new SampleResult();
053: // Check sample increments OK
054: res.sampleStart();
055: Thread.sleep(100);
056: res.samplePause();
057:
058: Thread.sleep(200);
059:
060: // Re-increment
061: res.sampleResume();
062: Thread.sleep(100);
063: res.sampleEnd();
064: long sampleTime = res.getTime();
065: if ((sampleTime < 200) || (sampleTime > 290)) {
066: fail("Accumulated time (" + sampleTime
067: + ") was not between 200 and 290 ms");
068: }
069: }
070:
071: private static Formatter fmt = new RawFormatter();
072:
073: private StringWriter wr = null;
074:
075: private void divertLog() {// N.B. This needs to divert the log for SampleResult
076: wr = new StringWriter(1000);
077: LogTarget[] lt = { new WriterTarget(wr, fmt) };
078: SampleResult.log.setLogTargets(lt);
079: }
080:
081: public void testPause2() throws Exception {
082: divertLog();
083: SampleResult res = new SampleResult();
084: res.sampleStart();
085: res.samplePause();
086: assertTrue(wr.toString().length() == 0);
087: res.samplePause();
088: assertFalse(wr.toString().length() == 0);
089: }
090:
091: public void testByteCount() throws Exception {
092: SampleResult res = new SampleResult();
093:
094: res.sampleStart();
095: res.setBytes(100);
096: res.setSampleLabel("sample of size 100 bytes");
097: res.sampleEnd();
098: assertEquals(100, res.getBytes());
099: assertEquals("sample of size 100 bytes", res.getSampleLabel());
100: }
101:
102: public void testSubResults() throws Exception {
103: // This test tries to emulate a http sample, with two
104: // subsamples, representing images that are downloaded for the
105: // page representing the first sample.
106:
107: // Sample that will get two sub results, simulates a web page load
108: SampleResult resWithSubResults = new SampleResult();
109:
110: long beginTest = System.currentTimeMillis();
111:
112: resWithSubResults.sampleStart();
113: Thread.sleep(100);
114: resWithSubResults.setBytes(300);
115: resWithSubResults.setSampleLabel("sample with two subresults");
116: resWithSubResults.setSuccessful(true);
117: resWithSubResults.sampleEnd();
118: long sampleWithSubResultsTime = resWithSubResults.getTime();
119:
120: // Sample with no sub results, simulates an image download
121: SampleResult resNoSubResults1 = new SampleResult();
122: resNoSubResults1.sampleStart();
123: Thread.sleep(100);
124: resNoSubResults1.setBytes(100);
125: resNoSubResults1.setSampleLabel("sample with no subresults");
126: resNoSubResults1.setSuccessful(true);
127: resNoSubResults1.sampleEnd();
128: long sample1Time = resNoSubResults1.getTime();
129:
130: assertTrue(resNoSubResults1.isSuccessful());
131: assertEquals(100, resNoSubResults1.getBytes());
132: assertEquals("sample with no subresults", resNoSubResults1
133: .getSampleLabel());
134: assertEquals(1, resNoSubResults1.getSampleCount());
135: assertEquals(0, resNoSubResults1.getSubResults().length);
136:
137: // Sample with no sub results, simulates an image download
138: SampleResult resNoSubResults2 = new SampleResult();
139: resNoSubResults2.sampleStart();
140: Thread.sleep(100);
141: resNoSubResults2.setBytes(200);
142: resNoSubResults2.setSampleLabel("sample with no subresults");
143: resNoSubResults2.setSuccessful(true);
144: resNoSubResults2.sampleEnd();
145: long sample2Time = resNoSubResults2.getTime();
146:
147: long overallTime = System.currentTimeMillis() - beginTest;
148:
149: assertTrue(resNoSubResults2.isSuccessful());
150: assertEquals(200, resNoSubResults2.getBytes());
151: assertEquals("sample with no subresults", resNoSubResults2
152: .getSampleLabel());
153: assertEquals(1, resNoSubResults2.getSampleCount());
154: assertEquals(0, resNoSubResults2.getSubResults().length);
155:
156: // Now add the subsamples to the sample
157: resWithSubResults.addSubResult(resNoSubResults1);
158: resWithSubResults.addSubResult(resNoSubResults2);
159: assertTrue(resWithSubResults.isSuccessful());
160: assertEquals(600, resWithSubResults.getBytes());
161: assertEquals("sample with two subresults", resWithSubResults
162: .getSampleLabel());
163: assertEquals(1, resWithSubResults.getSampleCount());
164: assertEquals(2, resWithSubResults.getSubResults().length);
165: long totalTime = resWithSubResults.getTime();
166:
167: // Check the sample times
168: long allsamplesTime = sampleWithSubResultsTime + sample1Time
169: + sample2Time;
170: if (totalTime < allsamplesTime) {
171: fail("Total: " + totalTime + " < sum(samples): "
172: + allsamplesTime);
173: }
174: if (totalTime > overallTime) {
175: fail("Total: " + totalTime + " > overall time: "
176: + overallTime);
177: }
178:
179: // Check that calculator gets the correct statistics from the sample
180: Calculator calculator = new Calculator();
181: calculator.addSample(resWithSubResults);
182: assertEquals(600, calculator.getTotalBytes());
183: assertEquals(1, calculator.getCount());
184: assertEquals(1d / (totalTime / 1000d), calculator.getRate(),
185: 0.0001d); // Allow for some margin of error
186: // Check that the throughput uses the time elapsed for the sub results
187: assertFalse(1d / (sampleWithSubResultsTime / 1000d) <= calculator
188: .getRate());
189: }
190:
191: // TODO some more invalid sequence tests needed
192:
193: public void testEncodingAndType() throws Exception {
194: // check default
195: SampleResult res = new SampleResult();
196: assertEquals(SampleResult.DEFAULT_ENCODING, res
197: .getDataEncoding());
198: assertEquals(SampleResult.DEFAULT_ENCODING, res
199: .getDataEncodingWithDefault());
200: assertEquals("DataType should be blank", "", res.getDataType());
201: assertNull(res.getDataEncodingNoDefault());
202:
203: // check null changes nothing
204: res.setEncodingAndType(null);
205: assertEquals(SampleResult.DEFAULT_ENCODING, res
206: .getDataEncoding());
207: assertEquals(SampleResult.DEFAULT_ENCODING, res
208: .getDataEncodingWithDefault());
209: assertEquals("DataType should be blank", "", res.getDataType());
210: assertNull(res.getDataEncodingNoDefault());
211:
212: // check no charset
213: res.setEncodingAndType("text/html");
214: assertEquals(SampleResult.DEFAULT_ENCODING, res
215: .getDataEncoding());
216: assertEquals(SampleResult.DEFAULT_ENCODING, res
217: .getDataEncodingWithDefault());
218: assertEquals("text", res.getDataType());
219: assertNull(res.getDataEncodingNoDefault());
220:
221: // Check unquoted charset
222: res.setEncodingAndType("text/html; charset=aBcd");
223: assertEquals("aBcd", res.getDataEncodingWithDefault());
224: assertEquals("aBcd", res.getDataEncodingNoDefault());
225: assertEquals("aBcd", res.getDataEncoding());
226: assertEquals("text", res.getDataType());
227:
228: // Check quoted charset
229: res.setEncodingAndType("text/html; charset=\"aBCd\"");
230: assertEquals("aBCd", res.getDataEncodingWithDefault());
231: assertEquals("aBCd", res.getDataEncodingNoDefault());
232: assertEquals("aBCd", res.getDataEncoding());
233: assertEquals("text", res.getDataType());
234: }
235: }
|