| 1 | |
package kg.apc.jmeter.threads; |
| 2 | |
|
| 3 | |
import java.awt.BorderLayout; |
| 4 | |
import java.awt.Color; |
| 5 | |
import java.awt.Component; |
| 6 | |
import java.awt.Dimension; |
| 7 | |
import java.util.concurrent.ConcurrentHashMap; |
| 8 | |
import javax.swing.BorderFactory; |
| 9 | |
import javax.swing.JPanel; |
| 10 | |
import javax.swing.JScrollPane; |
| 11 | |
import javax.swing.JTable; |
| 12 | |
import javax.swing.ListSelectionModel; |
| 13 | |
import javax.swing.event.CellEditorListener; |
| 14 | |
import javax.swing.event.ChangeEvent; |
| 15 | |
import javax.swing.event.TableModelEvent; |
| 16 | |
import javax.swing.event.TableModelListener; |
| 17 | |
import kg.apc.jmeter.JMeterPluginsUtils; |
| 18 | |
import kg.apc.charting.AbstractGraphRow; |
| 19 | |
import kg.apc.charting.DateTimeRenderer; |
| 20 | |
import kg.apc.charting.GraphPanelChart; |
| 21 | |
import kg.apc.charting.rows.GraphRowSumValues; |
| 22 | |
import kg.apc.jmeter.gui.ButtonPanelAddCopyRemove; |
| 23 | |
import kg.apc.jmeter.gui.GuiBuilderHelper; |
| 24 | |
import org.apache.jmeter.control.LoopController; |
| 25 | |
import org.apache.jmeter.control.gui.LoopControlPanel; |
| 26 | |
import org.apache.jmeter.gui.util.PowerTableModel; |
| 27 | |
import org.apache.jmeter.gui.util.VerticalPanel; |
| 28 | |
import org.apache.jmeter.testelement.TestElement; |
| 29 | |
import org.apache.jmeter.testelement.property.CollectionProperty; |
| 30 | |
import org.apache.jmeter.testelement.property.JMeterProperty; |
| 31 | |
import org.apache.jmeter.testelement.property.NullProperty; |
| 32 | |
import org.apache.jmeter.threads.AbstractThreadGroup; |
| 33 | |
import org.apache.jmeter.threads.JMeterThread; |
| 34 | |
import org.apache.jmeter.threads.gui.AbstractThreadGroupGui; |
| 35 | |
import org.apache.jorphan.collections.HashTree; |
| 36 | |
import org.apache.jorphan.logging.LoggingManager; |
| 37 | |
import org.apache.log.Logger; |
| 38 | |
|
| 39 | |
public class UltimateThreadGroupGui |
| 40 | |
extends AbstractThreadGroupGui |
| 41 | |
implements TableModelListener, |
| 42 | |
CellEditorListener { |
| 43 | |
|
| 44 | |
public static final String WIKIPAGE = "UltimateThreadGroup"; |
| 45 | 1 | private static final Logger log = LoggingManager.getLoggerForClass(); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
protected ConcurrentHashMap<String, AbstractGraphRow> model; |
| 50 | |
private GraphPanelChart chart; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 1 | public static final String[] columnIdentifiers = new String[]{ |
| 55 | |
"Start Threads Count", "Initial Delay, sec", "Startup Time, sec", "Hold Load For, sec", "Shutdown Time" |
| 56 | |
}; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 1 | public static final Class[] columnClasses = new Class[]{ |
| 61 | |
String.class, String.class, String.class, String.class, String.class |
| 62 | |
}; |
| 63 | 2 | public static final Integer[] defaultValues = new Integer[]{ |
| 64 | 1 | 100, 0, 30, 60, 10 |
| 65 | |
}; |
| 66 | |
private LoopControlPanel loopPanel; |
| 67 | |
protected PowerTableModel tableModel; |
| 68 | |
protected JTable grid; |
| 69 | |
protected ButtonPanelAddCopyRemove buttons; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public UltimateThreadGroupGui() { |
| 75 | 11 | super(); |
| 76 | 11 | init(); |
| 77 | 11 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
protected final void init() { |
| 83 | 12 | JMeterPluginsUtils.addHelpLinkToPanel(this, WIKIPAGE); |
| 84 | 12 | JPanel containerPanel = new VerticalPanel(); |
| 85 | |
|
| 86 | 12 | containerPanel.add(createParamsPanel(), BorderLayout.NORTH); |
| 87 | 12 | containerPanel.add(GuiBuilderHelper.getComponentWithMargin(createChart(), 2, 2, 0, 2), BorderLayout.CENTER); |
| 88 | 12 | add(containerPanel, BorderLayout.CENTER); |
| 89 | |
|
| 90 | |
|
| 91 | 12 | createControllerPanel(); |
| 92 | 12 | } |
| 93 | |
|
| 94 | |
private JPanel createParamsPanel() { |
| 95 | 12 | JPanel panel = new JPanel(new BorderLayout(5, 5)); |
| 96 | 12 | panel.setBorder(BorderFactory.createTitledBorder("Threads Schedule")); |
| 97 | 12 | panel.setPreferredSize(new Dimension(200, 200)); |
| 98 | |
|
| 99 | 12 | JScrollPane scroll = new JScrollPane(createGrid()); |
| 100 | 12 | scroll.setPreferredSize(scroll.getMinimumSize()); |
| 101 | 12 | panel.add(scroll, BorderLayout.CENTER); |
| 102 | 12 | buttons = new ButtonPanelAddCopyRemove(grid, tableModel, defaultValues); |
| 103 | 12 | panel.add(buttons, BorderLayout.SOUTH); |
| 104 | |
|
| 105 | 12 | return panel; |
| 106 | |
} |
| 107 | |
|
| 108 | |
private JTable createGrid() { |
| 109 | 12 | grid = new JTable(); |
| 110 | 12 | grid.getDefaultEditor(String.class).addCellEditorListener(this); |
| 111 | 12 | createTableModel(); |
| 112 | 12 | grid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 113 | 12 | grid.setMinimumSize(new Dimension(200, 100)); |
| 114 | |
|
| 115 | 12 | return grid; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public String getLabelResource() { |
| 120 | 1 | return this.getClass().getSimpleName(); |
| 121 | |
} |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public String getStaticLabel() { |
| 125 | 24 | return JMeterPluginsUtils.prefixLabel("Ultimate Thread Group"); |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public TestElement createTestElement() { |
| 130 | |
|
| 131 | 1 | UltimateThreadGroup tg = new UltimateThreadGroup(); |
| 132 | 1 | modifyTestElement(tg); |
| 133 | 1 | tg.setComment(JMeterPluginsUtils.getWikiLinkText(WIKIPAGE)); |
| 134 | |
|
| 135 | 1 | return tg; |
| 136 | |
} |
| 137 | |
|
| 138 | |
@Override |
| 139 | |
public void modifyTestElement(TestElement tg) { |
| 140 | |
|
| 141 | 2 | if (grid.isEditing()) { |
| 142 | 0 | grid.getCellEditor().stopCellEditing(); |
| 143 | |
} |
| 144 | |
|
| 145 | 2 | if (tg instanceof UltimateThreadGroup) { |
| 146 | 2 | UltimateThreadGroup utg = (UltimateThreadGroup) tg; |
| 147 | 2 | CollectionProperty rows = JMeterPluginsUtils.tableModelRowsToCollectionProperty(tableModel, UltimateThreadGroup.DATA_PROPERTY); |
| 148 | 2 | utg.setData(rows); |
| 149 | 2 | utg.setSamplerController((LoopController) loopPanel.createTestElement()); |
| 150 | |
} |
| 151 | 2 | super.configureTestElement(tg); |
| 152 | 2 | } |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public void configure(TestElement tg) { |
| 156 | |
|
| 157 | 1 | super.configure(tg); |
| 158 | 1 | UltimateThreadGroup utg = (UltimateThreadGroup) tg; |
| 159 | |
|
| 160 | 1 | JMeterProperty threadValues = utg.getData(); |
| 161 | 1 | if (!(threadValues instanceof NullProperty)) { |
| 162 | 1 | CollectionProperty columns = (CollectionProperty) threadValues; |
| 163 | |
|
| 164 | 1 | tableModel.removeTableModelListener(this); |
| 165 | 1 | JMeterPluginsUtils.collectionPropertyToTableModelRows(columns, tableModel); |
| 166 | 1 | tableModel.addTableModelListener(this); |
| 167 | 1 | updateUI(); |
| 168 | 1 | } else { |
| 169 | 0 | log.warn("Received null property instead of collection"); |
| 170 | |
} |
| 171 | |
|
| 172 | 1 | TestElement te = (TestElement) tg.getProperty(AbstractThreadGroup.MAIN_CONTROLLER).getObjectValue(); |
| 173 | 1 | if (te != null) { |
| 174 | 0 | loopPanel.configure(te); |
| 175 | |
} |
| 176 | 1 | buttons.checkDeleteButtonStatus(); |
| 177 | 1 | } |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public void updateUI() { |
| 181 | 16 | super.updateUI(); |
| 182 | |
|
| 183 | 16 | if (tableModel != null) { |
| 184 | 5 | UltimateThreadGroup utgForPreview = new UltimateThreadGroup(); |
| 185 | 5 | utgForPreview.setData(JMeterPluginsUtils.tableModelRowsToCollectionPropertyEval(tableModel, UltimateThreadGroup.DATA_PROPERTY)); |
| 186 | 5 | updateChart(utgForPreview); |
| 187 | |
} |
| 188 | 16 | } |
| 189 | |
|
| 190 | |
private void updateChart(UltimateThreadGroup tg) { |
| 191 | 5 | tg.testStarted(); |
| 192 | 5 | model.clear(); |
| 193 | 5 | GraphRowSumValues row = new GraphRowSumValues(); |
| 194 | 5 | row.setColor(Color.RED); |
| 195 | 5 | row.setDrawLine(true); |
| 196 | 5 | row.setMarkerSize(AbstractGraphRow.MARKER_SIZE_NONE); |
| 197 | 5 | row.setDrawThickLines(true); |
| 198 | |
|
| 199 | 5 | final HashTree hashTree = new HashTree(); |
| 200 | 5 | hashTree.add(new LoopController()); |
| 201 | 5 | JMeterThread thread = new JMeterThread(hashTree, null, null); |
| 202 | |
|
| 203 | 5 | long now = System.currentTimeMillis(); |
| 204 | |
|
| 205 | 5 | chart.setxAxisLabelRenderer(new DateTimeRenderer(DateTimeRenderer.HHMMSS, now - 1)); |
| 206 | 5 | chart.setForcedMinX(now); |
| 207 | |
|
| 208 | 5 | row.add(now, 0); |
| 209 | |
|
| 210 | |
|
| 211 | 5 | int numThreads = tg.getNumThreads(); |
| 212 | 5 | log.debug("Num Threads: " + numThreads); |
| 213 | 20 | for (int n = 0; n < numThreads; n++) { |
| 214 | 15 | thread.setThreadNum(n); |
| 215 | 15 | thread.setThreadName(Integer.toString(n)); |
| 216 | 15 | tg.scheduleThread(thread, now); |
| 217 | 15 | row.add(thread.getStartTime() - 1, 0); |
| 218 | 15 | row.add(thread.getStartTime(), 1); |
| 219 | |
} |
| 220 | |
|
| 221 | 5 | tg.testStarted(); |
| 222 | |
|
| 223 | 20 | for (int n = 0; n < tg.getNumThreads(); n++) { |
| 224 | 15 | thread.setThreadNum(n); |
| 225 | 15 | thread.setThreadName(Integer.toString(n)); |
| 226 | 15 | tg.scheduleThread(thread, now); |
| 227 | 15 | row.add(thread.getEndTime() - 1, 0); |
| 228 | 15 | row.add(thread.getEndTime(), -1); |
| 229 | |
} |
| 230 | |
|
| 231 | 5 | model.put("Expected parallel users count", row); |
| 232 | 5 | chart.invalidateCache(); |
| 233 | 5 | chart.repaint(); |
| 234 | 5 | } |
| 235 | |
|
| 236 | |
private JPanel createControllerPanel() { |
| 237 | 12 | loopPanel = new LoopControlPanel(false); |
| 238 | 12 | LoopController looper = (LoopController) loopPanel.createTestElement(); |
| 239 | 12 | looper.setLoops(-1); |
| 240 | 12 | looper.setContinueForever(true); |
| 241 | 12 | loopPanel.configure(looper); |
| 242 | 12 | return loopPanel; |
| 243 | |
} |
| 244 | |
|
| 245 | |
private Component createChart() { |
| 246 | 12 | chart = new GraphPanelChart(false, true); |
| 247 | 12 | model = new ConcurrentHashMap<String, AbstractGraphRow>(); |
| 248 | 12 | chart.setRows(model); |
| 249 | 12 | chart.getChartSettings().setDrawFinalZeroingLines(true); |
| 250 | 12 | chart.setxAxisLabel("Elapsed time"); |
| 251 | 12 | chart.setYAxisLabel("Number of active threads"); |
| 252 | 12 | chart.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
| 253 | 12 | return chart; |
| 254 | |
} |
| 255 | |
|
| 256 | |
public void tableChanged(TableModelEvent e) { |
| 257 | |
|
| 258 | 2 | updateUI(); |
| 259 | 2 | } |
| 260 | |
|
| 261 | |
private void createTableModel() { |
| 262 | 12 | tableModel = new PowerTableModel(columnIdentifiers, columnClasses); |
| 263 | 12 | tableModel.addTableModelListener(this); |
| 264 | 12 | grid.setModel(tableModel); |
| 265 | 12 | } |
| 266 | |
|
| 267 | |
public void editingStopped(ChangeEvent e) { |
| 268 | |
|
| 269 | 1 | updateUI(); |
| 270 | 1 | } |
| 271 | |
|
| 272 | |
public void editingCanceled(ChangeEvent e) { |
| 273 | |
|
| 274 | 1 | } |
| 275 | |
|
| 276 | |
@Override |
| 277 | |
public void clearGui() { |
| 278 | 1 | super.clearGui(); |
| 279 | 1 | tableModel.clearData(); |
| 280 | 1 | } |
| 281 | |
} |