Coverage Report - kg.apc.jmeter.threads.SteppingThreadGroupGui
 
Classes in this File Line Coverage Branch Coverage Complexity
SteppingThreadGroupGui
97%
174/178
66%
8/12
1.474
SteppingThreadGroupGui$FieldChangesListener
50%
7/14
33%
2/6
1.474
 
 1  
 package kg.apc.jmeter.threads;
 2  
 
 3  
 import java.awt.BorderLayout;
 4  
 import java.awt.Color;
 5  
 import java.awt.GridLayout;
 6  
 import java.util.concurrent.ConcurrentHashMap;
 7  
 import javax.swing.BorderFactory;
 8  
 import javax.swing.JLabel;
 9  
 import javax.swing.JPanel;
 10  
 import javax.swing.JTextField;
 11  
 import javax.swing.event.DocumentEvent;
 12  
 import javax.swing.event.DocumentListener;
 13  
 import kg.apc.jmeter.JMeterPluginsUtils;
 14  
 
 15  
 import kg.apc.charting.AbstractGraphRow;
 16  
 import kg.apc.charting.GraphPanelChart;
 17  
 import kg.apc.charting.rows.GraphRowSumValues;
 18  
 import kg.apc.charting.DateTimeRenderer;
 19  
 import kg.apc.jmeter.gui.GuiBuilderHelper;
 20  
 
 21  
 import org.apache.jmeter.control.LoopController;
 22  
 import org.apache.jmeter.control.gui.LoopControlPanel;
 23  
 import org.apache.jmeter.engine.util.CompoundVariable;
 24  
 import org.apache.jmeter.testelement.TestElement;
 25  
 import org.apache.jmeter.threads.AbstractThreadGroup;
 26  
 import org.apache.jmeter.threads.JMeterThread;
 27  
 import org.apache.jmeter.threads.gui.AbstractThreadGroupGui;
 28  
 import org.apache.jorphan.collections.HashTree;
 29  
 import org.apache.jorphan.logging.LoggingManager;
 30  
 import org.apache.log.Logger;
 31  
 
 32  0
 public class SteppingThreadGroupGui
 33  
         extends AbstractThreadGroupGui {
 34  
 
 35  
     private class FieldChangesListener implements DocumentListener {
 36  
 
 37  
         private final JTextField tf;
 38  
 
 39  72
         public FieldChangesListener(JTextField field) {
 40  72
             tf = field;
 41  72
         }
 42  
 
 43  
         private void update() {
 44  0
             refreshPreview();
 45  0
         }
 46  
 
 47  
         @Override
 48  
         public void insertUpdate(DocumentEvent e) {
 49  72
             if (tf.hasFocus()) {
 50  0
                 update();
 51  
             }
 52  72
         }
 53  
 
 54  
         @Override
 55  
         public void removeUpdate(DocumentEvent e) {
 56  18
             if (tf.hasFocus()) {
 57  0
                 update();
 58  
             }
 59  18
         }
 60  
 
 61  
         @Override
 62  
         public void changedUpdate(DocumentEvent e) {
 63  0
             if (tf.hasFocus()) {
 64  0
                 update();
 65  
             }
 66  0
         }
 67  
     }
 68  
     public static final String WIKIPAGE = "SteppingThreadGroup";
 69  
     /**
 70  
      *
 71  
      */
 72  1
     private static final Logger log = LoggingManager.getLoggerForClass();
 73  
     protected ConcurrentHashMap<String, AbstractGraphRow> model;
 74  
     private GraphPanelChart chart;
 75  
     private JTextField initialDelay;
 76  
     private JTextField incUserCount;
 77  
     private JTextField incUserCountBurst;
 78  
     private JTextField incUserPeriod;
 79  
     private JTextField flightTime;
 80  
     private JTextField decUserCount;
 81  
     private JTextField decUserPeriod;
 82  
     private JTextField totalThreads;
 83  
     private LoopControlPanel loopPanel;
 84  
     private JTextField rampUp;
 85  
 
 86  
     /**
 87  
      *
 88  
      */
 89  
     public SteppingThreadGroupGui() {
 90  7
         super();
 91  7
         init();
 92  7
         initGui();
 93  7
     }
 94  
 
 95  
     protected final void init() {
 96  8
         JMeterPluginsUtils.addHelpLinkToPanel(this, WIKIPAGE);
 97  8
         JPanel containerPanel = new JPanel(new BorderLayout());
 98  
 
 99  8
         containerPanel.add(createParamsPanel(), BorderLayout.NORTH);
 100  
 
 101  8
         chart = new GraphPanelChart(false, true);
 102  8
         model = new ConcurrentHashMap<String, AbstractGraphRow>();
 103  8
         chart.setRows(model);
 104  8
         chart.getChartSettings().setDrawFinalZeroingLines(true);
 105  
 
 106  8
         chart.setxAxisLabel("Elapsed time");
 107  8
         chart.setYAxisLabel("Number of active threads");
 108  
 
 109  8
         chart.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
 110  
 
 111  8
         containerPanel.add(GuiBuilderHelper.getComponentWithMargin(chart, 2, 2, 0, 2), BorderLayout.CENTER);
 112  
 
 113  8
         add(containerPanel, BorderLayout.CENTER);
 114  
 
 115  
         // this magic LoopPanel provides functionality for thread loops
 116  8
         createControllerPanel();
 117  8
     }
 118  
 
 119  
     @Override
 120  
     public void clearGui() {
 121  1
         super.clearGui();
 122  1
         initGui();
 123  1
     }
 124  
 
 125  
     // Initialise the gui field values
 126  
     private void initGui() {
 127  8
         totalThreads.setText("100");
 128  8
         initialDelay.setText("0");
 129  8
         incUserCount.setText("10");
 130  8
         incUserCountBurst.setText("0");
 131  8
         incUserPeriod.setText("30");
 132  8
         flightTime.setText("60");
 133  8
         decUserCount.setText("5");
 134  8
         decUserPeriod.setText("1");
 135  8
         rampUp.setText("5");
 136  8
     }
 137  
 
 138  
     private JPanel createParamsPanel() {
 139  8
         JPanel panel = new JPanel(new GridLayout(0, 5, 5, 5));
 140  8
         panel.setBorder(BorderFactory.createTitledBorder("Threads Scheduling Parameters"));
 141  
 
 142  8
         panel.add(new JLabel("This group will start", JLabel.RIGHT));
 143  8
         totalThreads = new JTextField(5);
 144  8
         panel.add(totalThreads);
 145  8
         panel.add(new JLabel("threads:", JLabel.LEFT));
 146  8
         panel.add(new JLabel());
 147  8
         panel.add(new JLabel());
 148  
 
 149  8
         panel.add(new JLabel("First, wait for", JLabel.RIGHT));
 150  8
         initialDelay = new JTextField(5);
 151  8
         panel.add(initialDelay);
 152  8
         panel.add(new JLabel("seconds;", JLabel.LEFT));
 153  8
         panel.add(new JLabel());
 154  8
         panel.add(new JLabel());
 155  
 
 156  8
         panel.add(new JLabel("Then start", JLabel.RIGHT));
 157  8
         incUserCountBurst = new JTextField(5);
 158  8
         panel.add(incUserCountBurst);
 159  8
         panel.add(new JLabel("threads; ", JLabel.LEFT));
 160  8
         panel.add(new JLabel(""));
 161  8
         panel.add(new JLabel());
 162  
 
 163  8
         panel.add(new JLabel("Next, add", JLabel.RIGHT));
 164  8
         incUserCount = new JTextField(5);
 165  8
         panel.add(incUserCount);
 166  8
         panel.add(new JLabel("threads every", JLabel.CENTER));
 167  8
         incUserPeriod = new JTextField(5);
 168  8
         panel.add(incUserPeriod);
 169  8
         panel.add(new JLabel("seconds, ", JLabel.LEFT));
 170  
 
 171  8
         panel.add(new JLabel());
 172  8
         panel.add(new JLabel());
 173  8
         panel.add(new JLabel("using ramp-up", JLabel.RIGHT));
 174  8
         rampUp = new JTextField(5);
 175  8
         panel.add(rampUp);
 176  8
         panel.add(new JLabel("seconds.", JLabel.LEFT));
 177  
 
 178  8
         panel.add(new JLabel("Then hold load for", JLabel.RIGHT));
 179  8
         flightTime = new JTextField(5);
 180  8
         panel.add(flightTime);
 181  8
         panel.add(new JLabel("seconds.", JLabel.LEFT));
 182  8
         panel.add(new JLabel());
 183  8
         panel.add(new JLabel());
 184  
 
 185  8
         panel.add(new JLabel("Finally, stop", JLabel.RIGHT));
 186  8
         decUserCount = new JTextField(5);
 187  8
         panel.add(decUserCount);
 188  8
         panel.add(new JLabel("threads every", JLabel.CENTER));
 189  8
         decUserPeriod = new JTextField(5);
 190  8
         panel.add(decUserPeriod);
 191  8
         panel.add(new JLabel("seconds.", JLabel.LEFT));
 192  
 
 193  8
         registerJTextfieldForGraphRefresh(totalThreads);
 194  8
         registerJTextfieldForGraphRefresh(initialDelay);
 195  8
         registerJTextfieldForGraphRefresh(incUserCount);
 196  8
         registerJTextfieldForGraphRefresh(incUserCountBurst);
 197  8
         registerJTextfieldForGraphRefresh(incUserPeriod);
 198  8
         registerJTextfieldForGraphRefresh(flightTime);
 199  8
         registerJTextfieldForGraphRefresh(decUserCount);
 200  8
         registerJTextfieldForGraphRefresh(decUserPeriod);
 201  8
         registerJTextfieldForGraphRefresh(rampUp);
 202  
 
 203  8
         return panel;
 204  
     }
 205  
 
 206  
     @Override
 207  
     public String getLabelResource() {
 208  1
         return this.getClass().getSimpleName();
 209  
     }
 210  
 
 211  
     @Override
 212  
     public String getStaticLabel() {
 213  16
         return JMeterPluginsUtils.prefixLabel("Stepping Thread Group");
 214  
     }
 215  
 
 216  
     @Override
 217  
     public TestElement createTestElement() {
 218  2
         SteppingThreadGroup tg = new SteppingThreadGroup();
 219  2
         modifyTestElement(tg);
 220  2
         tg.setComment(JMeterPluginsUtils.getWikiLinkText(WIKIPAGE));
 221  2
         return tg;
 222  
     }
 223  
 
 224  
     private void refreshPreview() {
 225  3
         SteppingThreadGroup tgForPreview = new SteppingThreadGroup();
 226  3
         tgForPreview.setNumThreads(new CompoundVariable(totalThreads.getText()).execute());
 227  3
         tgForPreview.setThreadGroupDelay(new CompoundVariable(initialDelay.getText()).execute());
 228  3
         tgForPreview.setInUserCount(new CompoundVariable(incUserCount.getText()).execute());
 229  3
         tgForPreview.setInUserCountBurst(new CompoundVariable(incUserCountBurst.getText()).execute());
 230  3
         tgForPreview.setInUserPeriod(new CompoundVariable(incUserPeriod.getText()).execute());
 231  3
         tgForPreview.setOutUserCount(new CompoundVariable(decUserCount.getText()).execute());
 232  3
         tgForPreview.setOutUserPeriod(new CompoundVariable(decUserPeriod.getText()).execute());
 233  3
         tgForPreview.setFlightTime(new CompoundVariable(flightTime.getText()).execute());
 234  3
         tgForPreview.setRampUp(new CompoundVariable(rampUp.getText()).execute());
 235  
 
 236  3
         if (tgForPreview.getInUserCountAsInt() == 0) {
 237  0
             tgForPreview.setInUserCount(new CompoundVariable(totalThreads.getText()).execute());
 238  
         }
 239  3
         if (tgForPreview.getOutUserCountAsInt() == 0) {
 240  0
             tgForPreview.setOutUserCount(new CompoundVariable(totalThreads.getText()).execute());
 241  
         }
 242  
 
 243  3
         updateChart(tgForPreview);
 244  3
     }
 245  
 
 246  
     @Override
 247  
     public void modifyTestElement(TestElement te) {
 248  3
         super.configureTestElement(te);
 249  
 
 250  3
         if (te instanceof SteppingThreadGroup) {
 251  3
             SteppingThreadGroup tg = (SteppingThreadGroup) te;
 252  3
             tg.setProperty(SteppingThreadGroup.NUM_THREADS, totalThreads.getText());
 253  3
             tg.setThreadGroupDelay(initialDelay.getText());
 254  3
             tg.setInUserCount(incUserCount.getText());
 255  3
             tg.setInUserCountBurst(incUserCountBurst.getText());
 256  3
             tg.setInUserPeriod(incUserPeriod.getText());
 257  3
             tg.setOutUserCount(decUserCount.getText());
 258  3
             tg.setOutUserPeriod(decUserPeriod.getText());
 259  3
             tg.setFlightTime(flightTime.getText());
 260  3
             tg.setRampUp(rampUp.getText());
 261  3
             ((AbstractThreadGroup) tg).setSamplerController((LoopController) loopPanel.createTestElement());
 262  
 
 263  3
             refreshPreview();
 264  
         }
 265  3
     }
 266  
 
 267  
     @Override
 268  
     public void configure(TestElement te) {
 269  1
         super.configure(te);
 270  1
         SteppingThreadGroup tg = (SteppingThreadGroup) te;
 271  1
         totalThreads.setText(tg.getNumThreadsAsString());
 272  1
         initialDelay.setText(tg.getThreadGroupDelay());
 273  1
         incUserCount.setText(tg.getInUserCount());
 274  1
         incUserCountBurst.setText(tg.getInUserCountBurst());
 275  1
         incUserPeriod.setText(tg.getInUserPeriod());
 276  1
         decUserCount.setText(tg.getOutUserCount());
 277  1
         decUserPeriod.setText(tg.getOutUserPeriod());
 278  1
         flightTime.setText(tg.getFlightTime());
 279  1
         rampUp.setText(tg.getRampUp());
 280  
 
 281  1
         TestElement controller = (TestElement) tg.getProperty(AbstractThreadGroup.MAIN_CONTROLLER).getObjectValue();
 282  1
         if (controller != null) {
 283  0
             loopPanel.configure(controller);
 284  
         }
 285  1
     }
 286  
 
 287  
     private void updateChart(SteppingThreadGroup tg) {
 288  3
         model.clear();
 289  
 
 290  3
         GraphRowSumValues row = new GraphRowSumValues();
 291  3
         row.setColor(Color.RED);
 292  3
         row.setDrawLine(true);
 293  3
         row.setMarkerSize(AbstractGraphRow.MARKER_SIZE_NONE);
 294  3
         row.setDrawThickLines(true);
 295  
 
 296  3
         final HashTree hashTree = new HashTree();
 297  3
         hashTree.add(new LoopController());
 298  3
         JMeterThread thread = new JMeterThread(hashTree, null, null);
 299  
 
 300  3
         long now = System.currentTimeMillis();
 301  
 
 302  
         // test start
 303  3
         chart.setxAxisLabelRenderer(new DateTimeRenderer(DateTimeRenderer.HHMMSS, now - 1)); //-1 because row.add(thread.getStartTime() - 1, 0)
 304  3
         row.add(now, 0);
 305  3
         row.add(now + tg.getThreadGroupDelayAsInt(), 0);
 306  
 
 307  3
         int numThreads = tg.getNumThreads();
 308  
 
 309  
         // users in
 310  303
         for (int n = 0; n < numThreads; n++) {
 311  300
             thread.setThreadNum(n);
 312  300
             tg.scheduleThread(thread, now);
 313  300
             row.add(thread.getStartTime() - 1, 0);
 314  300
             row.add(thread.getStartTime(), 1);
 315  
         }
 316  
 
 317  
         // users out
 318  303
         for (int n = 0; n < numThreads; n++) {
 319  300
             thread.setThreadNum(n);
 320  300
             tg.scheduleThread(thread, now);
 321  300
             row.add(thread.getEndTime() - 1, 0);
 322  300
             row.add(thread.getEndTime(), -1);
 323  
         }
 324  
 
 325  3
         model.put("Expected Active Users Count", row);
 326  3
         chart.invalidateCache();
 327  3
         chart.repaint();
 328  3
     }
 329  
 
 330  
     private JPanel createControllerPanel() {
 331  8
         loopPanel = new LoopControlPanel(false);
 332  8
         LoopController looper = (LoopController) loopPanel.createTestElement();
 333  8
         looper.setLoops(-1);
 334  8
         looper.setContinueForever(true);
 335  8
         loopPanel.configure(looper);
 336  8
         return loopPanel;
 337  
     }
 338  
 
 339  
     private void registerJTextfieldForGraphRefresh(final JTextField tf) {
 340  72
         tf.addActionListener(loopPanel);
 341  72
         tf.getDocument().addDocumentListener(new FieldChangesListener(tf));
 342  72
     }
 343  
 }