1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.data;
14
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.List;
18
19 import javax.swing.table.AbstractTableModel;
20
21 import org.apache.log4j.Logger;
22
23 import com.eviware.soapui.model.support.LoadTestRunListenerAdapter;
24 import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
25 import com.eviware.soapui.model.testsuite.LoadTest;
26 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
27 import com.eviware.soapui.model.testsuite.LoadTestRunner;
28 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
29 import com.eviware.soapui.model.testsuite.TestCaseRunner;
30 import com.eviware.soapui.model.testsuite.TestStep;
31 import com.eviware.soapui.model.testsuite.TestStepResult;
32
33 /***
34 * Collector of samples from a loadtest, exposed as TableModel
35 *
36 * @author Ole.Matzura
37 */
38
39 public class LoadTestSamples extends AbstractTableModel
40 {
41 private final LoadTest loadTest;
42 private List<List<LoadTestStepSample[]>> samples = new ArrayList<List<LoadTestStepSample[]>>();
43 private List<Long> timestamps = new ArrayList<Long>();
44 private InternalLoadTestRunListener loadTestRunListener = new InternalLoadTestRunListener();
45 private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
46 private final static Logger log = Logger.getLogger( LoadTestSamples.class );
47
48 public LoadTestSamples( LoadTest loadTest )
49 {
50 this.loadTest = loadTest;
51
52 loadTest.addLoadTestRunListener( loadTestRunListener );
53 loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener );
54 }
55
56 public int getRowCount()
57 {
58 return samples.size();
59 }
60
61 public void release()
62 {
63 loadTest.removeLoadTestRunListener( loadTestRunListener );
64 loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
65 }
66
67 public int getColumnCount()
68 {
69 return loadTest.getTestCase().getTestStepCount() + 1;
70 }
71
72 public Object getValueAt( int rowIndex, int columnIndex )
73 {
74 return columnIndex == 0 ? new Date( timestamps.get( rowIndex ) ) : samples.get( rowIndex ).get( columnIndex - 1 );
75 }
76
77 public Class<?> getColumnClass( int columnIndex )
78 {
79 return columnIndex == 0 ? Date.class : LoadTestStepSample[].class;
80 }
81
82 public String getColumnName( int columnIndex )
83 {
84 return columnIndex == 0 ? "Timestamp" : loadTest.getTestCase().getTestStepAt( columnIndex - 1 ).getName();
85 }
86
87 public synchronized void clear()
88 {
89 int size = samples.size();
90 samples.clear();
91 timestamps.clear();
92 fireTableRowsDeleted( 0, size - 1 );
93 }
94
95 private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
96 {
97 public void loadTestRemoved( LoadTest loadTest )
98 {
99 if( loadTest.equals( LoadTestSamples.this.loadTest ) )
100 {
101 loadTest.removeLoadTestRunListener( loadTestRunListener );
102 }
103 }
104
105 public void testStepAdded( TestStep testStep, int index )
106 {
107 if( testStep.getTestCase() == loadTest.getTestCase() )
108 {
109 for( List<LoadTestStepSample[]> values : samples )
110 {
111 values.add( index, new LoadTestStepSample[0] );
112 }
113 }
114 }
115
116 public void testStepMoved( TestStep testStep, int fromIndex, int offset )
117 {
118 if( testStep.getTestCase() == loadTest.getTestCase() )
119 {
120 for( List<LoadTestStepSample[]> values : samples )
121 {
122 LoadTestStepSample[] s = values.remove( fromIndex );
123 values.add( offset, s );
124 }
125 }
126 }
127
128 public void testStepRemoved( TestStep testStep, int index )
129 {
130 if( testStep.getTestCase() == loadTest.getTestCase() )
131 {
132 for( List<LoadTestStepSample[]> values : samples )
133 {
134 values.remove( index );
135 }
136 }
137 }
138 }
139
140 private class InternalLoadTestRunListener extends LoadTestRunListenerAdapter
141 {
142 public void afterTestCase( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
143 TestCaseRunContext runContext )
144 {
145 long timestamp = System.currentTimeMillis();
146 List<LoadTestStepSample[]> s = new ArrayList<LoadTestStepSample[]>();
147 List<TestStepResult> testResults = testRunner.getResults();
148
149 for( int c = 0; c < loadTest.getTestCase().getTestStepCount(); c++ )
150 {
151 TestStep testStep = loadTest.getTestCase().getTestStepAt( c );
152 List<LoadTestStepSample> results = new ArrayList<LoadTestStepSample>();
153
154 for( int i = 0; i < testResults.size(); i++ )
155 {
156 TestStepResult stepResult = testResults.get( i );
157 if( stepResult == null )
158 {
159 log.warn( "Result [" + c + "] is null in TestCase [" + testRunner.getTestCase().getName() + "]" );
160 continue;
161 }
162
163 if( stepResult.getTestStep().equals( testStep ) )
164 results.add( new LoadTestStepSample( stepResult ) );
165 }
166
167 s.add( results.toArray( new LoadTestStepSample[results.size()] ) );
168 }
169
170 synchronized( this )
171 {
172 samples.add( s );
173 timestamps.add( timestamp );
174 fireTableRowsInserted( samples.size() - 1, samples.size() - 1 );
175 }
176 }
177 }
178 }