| 1 | |
package kg.apc.jmeter.timers; |
| 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.charting.AbstractGraphRow; |
| 18 | |
import kg.apc.charting.DateTimeRenderer; |
| 19 | |
import kg.apc.charting.GraphPanelChart; |
| 20 | |
import kg.apc.charting.rows.GraphRowExactValues; |
| 21 | |
import kg.apc.jmeter.JMeterPluginsUtils; |
| 22 | |
import kg.apc.jmeter.gui.ButtonPanelAddCopyRemove; |
| 23 | |
import kg.apc.jmeter.gui.GuiBuilderHelper; |
| 24 | |
import kg.apc.jmeter.threads.UltimateThreadGroupGui; |
| 25 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 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.timers.gui.AbstractTimerGui; |
| 33 | |
import org.apache.jorphan.logging.LoggingManager; |
| 34 | |
import org.apache.log.Logger; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class VariableThroughputTimerGui |
| 40 | |
extends AbstractTimerGui |
| 41 | |
implements TableModelListener, |
| 42 | |
CellEditorListener { |
| 43 | |
|
| 44 | |
public static final String WIKIPAGE = "ThroughputShapingTimer"; |
| 45 | 1 | private static final Logger log = LoggingManager.getLoggerForClass(); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
protected ConcurrentHashMap<String, AbstractGraphRow> model; |
| 50 | |
protected GraphPanelChart chart; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 2 | private static Integer[] defaultValues = new Integer[]{ |
| 55 | 1 | 1, 1000, 60 |
| 56 | |
}; |
| 57 | |
protected PowerTableModel tableModel; |
| 58 | |
protected JTable grid; |
| 59 | |
protected ButtonPanelAddCopyRemove buttons; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public VariableThroughputTimerGui() { |
| 65 | 11 | super(); |
| 66 | 11 | init(); |
| 67 | 11 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
protected final void init() { |
| 73 | 12 | setBorder(makeBorder()); |
| 74 | 12 | setLayout(new BorderLayout()); |
| 75 | 12 | add(JMeterPluginsUtils.addHelpLinkToPanel(makeTitlePanel(), WIKIPAGE), BorderLayout.NORTH); |
| 76 | 12 | JPanel containerPanel = new VerticalPanel(); |
| 77 | |
|
| 78 | 12 | containerPanel.add(createParamsPanel(), BorderLayout.NORTH); |
| 79 | 12 | containerPanel.add(GuiBuilderHelper.getComponentWithMargin(createChart(), 2, 2, 0, 2), BorderLayout.CENTER); |
| 80 | 12 | add(containerPanel, BorderLayout.CENTER); |
| 81 | 12 | } |
| 82 | |
|
| 83 | |
private JPanel createParamsPanel() { |
| 84 | 12 | JPanel panel = new JPanel(new BorderLayout(5, 5)); |
| 85 | 12 | panel.setBorder(BorderFactory.createTitledBorder("Requests Per Second (RPS) Schedule")); |
| 86 | 12 | panel.setPreferredSize(new Dimension(200, 200)); |
| 87 | |
|
| 88 | 12 | JScrollPane scroll = new JScrollPane(createGrid()); |
| 89 | 12 | scroll.setPreferredSize(scroll.getMinimumSize()); |
| 90 | 12 | panel.add(scroll, BorderLayout.CENTER); |
| 91 | 12 | buttons = new ButtonPanelAddCopyRemove(grid, tableModel, defaultValues); |
| 92 | 12 | panel.add(buttons, BorderLayout.SOUTH); |
| 93 | |
|
| 94 | 12 | return panel; |
| 95 | |
} |
| 96 | |
|
| 97 | |
private JTable createGrid() { |
| 98 | 12 | grid = new JTable(); |
| 99 | 12 | grid.getDefaultEditor(String.class).addCellEditorListener(this); |
| 100 | 12 | createTableModel(); |
| 101 | 12 | grid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 102 | 12 | grid.setMinimumSize(new Dimension(200, 100)); |
| 103 | 12 | return grid; |
| 104 | |
} |
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public String getLabelResource() { |
| 108 | 1 | return this.getClass().getSimpleName(); |
| 109 | |
} |
| 110 | |
|
| 111 | |
@Override |
| 112 | |
public String getStaticLabel() { |
| 113 | 25 | return JMeterPluginsUtils.prefixLabel("Throughput Shaping Timer"); |
| 114 | |
} |
| 115 | |
|
| 116 | |
@Override |
| 117 | |
public TestElement createTestElement() { |
| 118 | |
|
| 119 | 1 | VariableThroughputTimer tg = new VariableThroughputTimer(); |
| 120 | 1 | modifyTestElement(tg); |
| 121 | 1 | tg.setComment(JMeterPluginsUtils.getWikiLinkText(WIKIPAGE)); |
| 122 | 1 | return tg; |
| 123 | |
} |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public void modifyTestElement(TestElement tg) { |
| 127 | |
|
| 128 | 2 | super.configureTestElement(tg); |
| 129 | |
|
| 130 | 2 | if (grid.isEditing()) { |
| 131 | 0 | grid.getCellEditor().stopCellEditing(); |
| 132 | |
} |
| 133 | |
|
| 134 | 2 | if (tg instanceof VariableThroughputTimer) { |
| 135 | 2 | VariableThroughputTimer utg = (VariableThroughputTimer) tg; |
| 136 | 2 | CollectionProperty rows = JMeterPluginsUtils.tableModelRowsToCollectionProperty(tableModel, VariableThroughputTimer.DATA_PROPERTY); |
| 137 | 2 | utg.setData(rows); |
| 138 | |
} |
| 139 | 2 | } |
| 140 | |
|
| 141 | |
@Override |
| 142 | |
public void configure(TestElement tg) { |
| 143 | |
|
| 144 | 1 | super.configure(tg); |
| 145 | 1 | VariableThroughputTimer utg = (VariableThroughputTimer) tg; |
| 146 | 1 | JMeterProperty threadValues = utg.getData(); |
| 147 | 1 | if (threadValues instanceof NullProperty) { |
| 148 | 0 | log.warn("Received null property instead of collection"); |
| 149 | 0 | return; |
| 150 | |
} |
| 151 | |
|
| 152 | 1 | CollectionProperty columns = (CollectionProperty) threadValues; |
| 153 | |
|
| 154 | 1 | tableModel.removeTableModelListener(this); |
| 155 | 1 | JMeterPluginsUtils.collectionPropertyToTableModelRows(columns, tableModel); |
| 156 | 1 | tableModel.addTableModelListener(this); |
| 157 | 1 | buttons.checkDeleteButtonStatus(); |
| 158 | 1 | updateUI(); |
| 159 | 1 | } |
| 160 | |
|
| 161 | |
@Override |
| 162 | |
public void updateUI() { |
| 163 | 17 | super.updateUI(); |
| 164 | |
|
| 165 | 17 | if (tableModel != null) { |
| 166 | 6 | VariableThroughputTimer utgForPreview = new VariableThroughputTimer(); |
| 167 | 6 | utgForPreview.setData(JMeterPluginsUtils.tableModelRowsToCollectionPropertyEval(tableModel, VariableThroughputTimer.DATA_PROPERTY)); |
| 168 | 6 | updateChart(utgForPreview); |
| 169 | |
} |
| 170 | 17 | } |
| 171 | |
|
| 172 | |
private int getIntFromRow(int row, int col) { |
| 173 | |
int ret; |
| 174 | |
try { |
| 175 | 4 | ret = Integer.valueOf(new CompoundVariable(tableModel.getValueAt(row, col).toString()).execute()); |
| 176 | 0 | } catch (NumberFormatException ex) { |
| 177 | 0 | ret = -1; |
| 178 | 4 | } |
| 179 | 4 | return ret; |
| 180 | |
} |
| 181 | |
|
| 182 | |
private void updateChart(VariableThroughputTimer tg) { |
| 183 | 6 | model.clear(); |
| 184 | 6 | chart.clearErrorMessage(); |
| 185 | 6 | AbstractGraphRow row = new GraphRowExactValues(); |
| 186 | 6 | row.setColor(Color.BLUE); |
| 187 | 6 | row.setDrawLine(true); |
| 188 | 6 | row.setMarkerSize(AbstractGraphRow.MARKER_SIZE_NONE); |
| 189 | 6 | row.setDrawThickLines(true); |
| 190 | |
|
| 191 | 6 | long now = System.currentTimeMillis(); |
| 192 | |
|
| 193 | 6 | int rowsCount = tableModel.getRowCount(); |
| 194 | 6 | row.add(now, 0); |
| 195 | 6 | row.add(now, tg.getRPSForSecond(0)); |
| 196 | |
|
| 197 | 6 | int duration = 0; |
| 198 | 10 | for (int i = 0; i < rowsCount; i++) { |
| 199 | 4 | row.add(now + (duration + 1) * 1000, tg.getRPSForSecond(duration + 1)); |
| 200 | 4 | int rowVal = getIntFromRow(i, 2); |
| 201 | 4 | if (rowVal < 0) { |
| 202 | 0 | chart.setErrorMessage("The values entered cannot be rendered in preview..."); |
| 203 | 0 | break; |
| 204 | |
} |
| 205 | 4 | duration = duration + rowVal; |
| 206 | 4 | row.add(now + duration * 1000, tg.getRPSForSecond(duration)); |
| 207 | |
} |
| 208 | |
|
| 209 | 6 | chart.setxAxisLabelRenderer(new DateTimeRenderer(DateTimeRenderer.HHMMSS, now - 1)); |
| 210 | 6 | chart.setForcedMinX(now); |
| 211 | |
|
| 212 | 6 | model.put("Expected RPS", row); |
| 213 | 6 | chart.invalidateCache(); |
| 214 | 6 | chart.repaint(); |
| 215 | 6 | } |
| 216 | |
|
| 217 | |
private Component createChart() { |
| 218 | 12 | chart = new GraphPanelChart(false, true); |
| 219 | 12 | model = new ConcurrentHashMap<String, AbstractGraphRow>(); |
| 220 | 12 | chart.setRows(model); |
| 221 | 12 | chart.getChartSettings().setDrawFinalZeroingLines(true); |
| 222 | 12 | chart.setxAxisLabel("Elapsed Time"); |
| 223 | 12 | chart.setYAxisLabel("Number of requests /sec"); |
| 224 | 12 | chart.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
| 225 | 12 | return chart; |
| 226 | |
} |
| 227 | |
|
| 228 | |
private void createTableModel() { |
| 229 | 12 | tableModel = new PowerTableModel(VariableThroughputTimer.columnIdentifiers, VariableThroughputTimer.columnClasses); |
| 230 | 12 | tableModel.addTableModelListener(this); |
| 231 | 12 | grid.setModel(tableModel); |
| 232 | 12 | } |
| 233 | |
|
| 234 | |
@Override |
| 235 | |
public void editingStopped(ChangeEvent e) { |
| 236 | 1 | updateUI(); |
| 237 | 1 | } |
| 238 | |
|
| 239 | |
@Override |
| 240 | |
public void editingCanceled(ChangeEvent e) { |
| 241 | |
|
| 242 | 1 | } |
| 243 | |
|
| 244 | |
@Override |
| 245 | |
public void clearGui() { |
| 246 | 1 | super.clearGui(); |
| 247 | 1 | tableModel.clearData(); |
| 248 | 1 | tableModel.fireTableDataChanged(); |
| 249 | 1 | } |
| 250 | |
|
| 251 | |
@Override |
| 252 | |
public void tableChanged(TableModelEvent e) { |
| 253 | 3 | updateUI(); |
| 254 | 3 | } |
| 255 | |
} |