View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    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.TestRunContext;
    29  import com.eviware.soapui.model.testsuite.TestRunner;
    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 )) :
    75  			samples.get( rowIndex ).get( columnIndex-1 );
    76  	}
    77
    78  	public Class<?> getColumnClass(int columnIndex)
    79  	{
    80  		return columnIndex == 0 ? Date.class : LoadTestStepSample[].class;
    81  	}
    82
    83  	public String getColumnName(int columnIndex)
    84  	{
    85  		return columnIndex == 0 ? "Timestamp" : loadTest.getTestCase().getTestStepAt( columnIndex-1 ).getName();
    86  	}
    87
    88  	public synchronized void clear()
    89  	{
    90  		int size = samples.size();
    91  		samples.clear();
    92  		timestamps.clear();
    93  		fireTableRowsDeleted( 0, size-1 );
    94  	}
    95
    96  	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
    97  	{
    98  		public void loadTestRemoved(LoadTest loadTest)
    99  		{
    100 			if( loadTest.equals( LoadTestSamples.this.loadTest ))
    101 			{
    102 				loadTest.removeLoadTestRunListener( loadTestRunListener );
    103 			}
    104 		}
    105
    106 		public void testStepAdded(TestStep testStep, int index)
    107 		{
    108 			if( testStep.getTestCase() == loadTest.getTestCase() )
    109 			{
    110 				for( List<LoadTestStepSample[]> values : samples )
    111 				{
    112 					values.add( index, new LoadTestStepSample[0] );
    113 				}
    114 			}
    115 		}
    116
    117 		public void testStepMoved(TestStep testStep, int fromIndex, int offset)
    118 		{
    119 			if( testStep.getTestCase() == loadTest.getTestCase() )
    120 			{
    121 				for( List<LoadTestStepSample[]> values : samples )
    122 				{
    123 					LoadTestStepSample[] s = values.remove( fromIndex );
    124 					values.add( offset, s );
    125 				}
    126 			}
    127 		}
    128
    129 		public void testStepRemoved(TestStep testStep, int index)
    130 		{
    131 			if( testStep.getTestCase() == loadTest.getTestCase() )
    132 			{
    133 				for( List<LoadTestStepSample[]> values : samples )
    134 				{
    135 					values.remove( index );
    136 				}
    137 			}
    138 		}
    139 	}
    140
    141 	private class InternalLoadTestRunListener extends LoadTestRunListenerAdapter
    142 	{
    143 		public void afterTestCase(LoadTestRunner loadTestRunner, LoadTestRunContext context, TestRunner testRunner, TestRunContext 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 }