Coverage Report - kg.apc.jmeter.PluginsCMDWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginsCMDWorker
66%
142/215
34%
34/98
2.818
 
 1  
 package kg.apc.jmeter;
 2  
 
 3  
 import kg.apc.charting.ChartSettings;
 4  
 import kg.apc.charting.GraphPanelChart;
 5  
 import kg.apc.cmd.UniversalRunner;
 6  
 import kg.apc.jmeter.graphs.AbstractGraphPanelVisualizer;
 7  
 import kg.apc.jmeter.vizualizers.CorrectedResultCollector;
 8  
 import org.apache.jorphan.logging.LoggingManager;
 9  
 import org.apache.log.Logger;
 10  
 
 11  
 import java.io.File;
 12  
 import java.io.FileInputStream;
 13  
 import java.io.IOException;
 14  
 import java.io.InputStream;
 15  
 import java.util.ListIterator;
 16  
 import java.util.Properties;
 17  
 
 18  
 public class PluginsCMDWorker {
 19  
 
 20  35
     private int graphWidth = 800;
 21  35
     private int graphHeight = 600;
 22  
     public static final int EXPORT_PNG = 1;
 23  
     public static final int EXPORT_CSV = 2;
 24  35
     private int exportMode = 0;
 25  
     private String inputFile;
 26  
     private String outputCSV;
 27  
     private String outputPNG;
 28  
     private AbstractGraphPanelVisualizer pluginType;
 29  1
     private static final Logger log = LoggingManager.getLoggerForClass();
 30  35
     private int aggregate = -1;
 31  35
     private int zeroing = -1;
 32  35
     private int preventOutliers = -1;
 33  35
     private int rowsLimit = -1;
 34  35
     private int forceY = -1;
 35  35
     private int lowCounts = -1;
 36  35
     private int granulation = -1;
 37  35
     private int relativeTimes = -1;
 38  35
     private int gradient = -1;
 39  35
     private int autoScaleRows = -1;
 40  35
     private float lineWeight = -1;
 41  35
     private String includeLabels = "";
 42  35
     private String excludeLabels = "";
 43  35
     private String startOffset = "";
 44  35
     private String endOffset = "";
 45  35
     private int includeSamplesWithRegex = -1;
 46  35
     private int excludeSamplesWithRegex = -1;
 47  35
     private int successFilter = -1;
 48  35
     private int markers = -1;
 49  
 
 50  35
     public PluginsCMDWorker() {
 51  35
         log.info("Using JMeterPluginsCMD v. " + JMeterPluginsUtils.getVersion());
 52  35
         JMeterPluginsUtils.prepareJMeterEnv(UniversalRunner.getJARLocation());
 53  35
     }
 54  
 
 55  
 
 56  
     public void addExportMode(int mode) {
 57  9
         exportMode |= mode;
 58  9
     }
 59  
 
 60  
     public void setInputFile(String string) {
 61  8
         inputFile = string;
 62  8
     }
 63  
 
 64  
     public void setOutputCSVFile(String string) {
 65  6
         outputCSV = string;
 66  6
     }
 67  
 
 68  
     public void setOutputPNGFile(String string) {
 69  4
         outputPNG = string;
 70  4
     }
 71  
 
 72  
     public void setPluginType(String string) {
 73  8
         pluginType = getGUIObject(string);
 74  8
     }
 75  
 
 76  
     private void checkParams() {
 77  7
         if (pluginType == null) {
 78  0
             throw new IllegalArgumentException("Missing plugin type specification");
 79  
         }
 80  
 
 81  7
         if (exportMode == 0) {
 82  0
             throw new IllegalArgumentException("Missing any export specification");
 83  
         }
 84  
 
 85  7
         if (inputFile == null) {
 86  0
             throw new IllegalArgumentException("Missing input JTL file specification");
 87  
         }
 88  
 
 89  7
         if (!(new File(inputFile).exists())) {
 90  0
             throw new IllegalArgumentException("Cannot find specified JTL file: " + inputFile);
 91  
         }
 92  
 
 93  7
     }
 94  
 
 95  
     public void setGraphWidth(int i) {
 96  2
         graphWidth = i;
 97  2
     }
 98  
 
 99  
     public void setGraphHeight(int i) {
 100  2
         graphHeight = i;
 101  2
     }
 102  
 
 103  
     public int doJob() {
 104  7
         checkParams();
 105  
 
 106  7
         AbstractGraphPanelVisualizer pluginInstance = pluginType;
 107  7
         pluginType.setIgnoreCurrentTestStartTime();
 108  7
         setOptions(pluginInstance);
 109  
 
 110  
         CorrectedResultCollector rc;
 111  7
         rc = (CorrectedResultCollector) pluginInstance.createTestElement();
 112  7
         rc.setExcludeLabels(excludeLabels);
 113  7
         rc.setIncludeLabels(includeLabels);
 114  7
         rc.setStartOffset(startOffset);
 115  7
         rc.setEndOffset(endOffset);
 116  
 
 117  7
         if (includeSamplesWithRegex >= 0) {
 118  0
             rc.setEnabledIncludeRegex(includeSamplesWithRegex != 0);
 119  
         }
 120  7
         if (excludeSamplesWithRegex >= 0) {
 121  0
             rc.setEnabledExcludeRegex(excludeSamplesWithRegex != 0);
 122  
         }
 123  
 
 124  7
         if (successFilter >= 0) {
 125  0
             rc.setErrorLogging(successFilter == 0);
 126  0
             rc.setSuccessOnlyLogging(successFilter != 0);
 127  
         }
 128  
 
 129  14
         if (pluginType.getStaticLabel().equals(
 130  7
                 JMeterPluginsUtils.prefixLabel("Merge Results"))) {
 131  0
             log.debug("Using properties file with MergeResults plugin: "
 132  
                     + inputFile);
 133  0
             Properties prop = new Properties();
 134  0
             InputStream input = null;
 135  
 
 136  
             try {
 137  0
                 input = new FileInputStream(inputFile);
 138  
 
 139  
                 // load a properties file
 140  0
                 prop.load(input);
 141  
 
 142  0
                 for (int i = 1; i < 5; i++) {
 143  0
                     rc.setFilename(null == prop.getProperty("inputJtl" + i) ? ""
 144  0
                             : prop.getProperty("inputJtl" + i));
 145  0
                     if (rc.getFilename().isEmpty()) {
 146  0
                         break;
 147  
                     }
 148  0
                     rc.setPrefixLabel(null == prop.getProperty("prefixLabel"
 149  0
                             + i) ? "" : prop.getProperty("prefixLabel" + i));
 150  0
                     rc.setIncludeLabels(null == prop
 151  0
                             .getProperty("includeLabels" + i) ? "" : prop
 152  0
                             .getProperty("includeLabels" + i));
 153  0
                     rc.setExcludeLabels(null == prop
 154  0
                             .getProperty("excludeLabels" + i) ? "" : prop
 155  0
                             .getProperty("excludeLabels" + i));
 156  0
                     rc.setEnabledIncludeRegex(Boolean.valueOf(prop
 157  0
                             .getProperty("includeLabelRegex" + i)));
 158  0
                     rc.setEnabledExcludeRegex(Boolean.valueOf(prop
 159  0
                             .getProperty("excludeLabelRegex" + i)));
 160  0
                     rc.setStartOffset(null == prop.getProperty("startOffset"
 161  0
                             + i) ? "" : prop.getProperty("startOffset" + i));
 162  0
                     rc.setEndOffset(null == prop.getProperty("endOffset" + i) ? ""
 163  0
                             : prop.getProperty("endOffset" + i));
 164  0
                     rc.setListener(pluginInstance);
 165  0
                     pluginInstance.configure(rc);
 166  
 
 167  
                     // rc.testStarted();
 168  0
                     rc.loadExistingFile();
 169  
                     // rc.testEnded();
 170  
                 }
 171  
 
 172  0
             } catch (IOException ex) {
 173  0
                 ex.printStackTrace();
 174  
             } finally {
 175  0
                 if (input != null) {
 176  
                     try {
 177  0
                         input.close();
 178  0
                     } catch (IOException e) {
 179  0
                         e.printStackTrace();
 180  0
                     }
 181  
                 }
 182  
             }
 183  
 
 184  0
         } else {
 185  7
             log.debug("Using JTL file: " + inputFile);
 186  7
             rc.setFilename(inputFile);
 187  7
             rc.setListener(pluginInstance);
 188  7
             pluginInstance.configure(rc);
 189  
 
 190  
             // rc.testStarted();
 191  7
             rc.loadExistingFile();
 192  
             // rc.testEnded();
 193  
         }
 194  
 
 195  
         // to handle issue 64 and since it must be cheap - set options again
 196  7
         setOptions(pluginInstance);
 197  
 
 198  6
         if ((exportMode & EXPORT_PNG) == EXPORT_PNG) {
 199  2
             File pngFile = new File(outputPNG);
 200  2
             forceDir(pngFile);
 201  
 
 202  
             try {
 203  2
                 pluginInstance.getGraphPanelChart().saveGraphToPNG(pngFile, graphWidth, graphHeight);
 204  0
             } catch (IOException ex) {
 205  0
                 throw new RuntimeException(ex);
 206  2
             }
 207  
         }
 208  
 
 209  6
         if ((exportMode & EXPORT_CSV) == EXPORT_CSV) {
 210  5
             File csvFile = new File(outputCSV);
 211  5
             forceDir(csvFile);
 212  
 
 213  
             try {
 214  5
                 pluginInstance.getGraphPanelChart().saveGraphToCSV(csvFile);
 215  0
             } catch (IOException ex) {
 216  0
                 throw new RuntimeException(ex);
 217  5
             }
 218  
         }
 219  
 
 220  6
         return 0;
 221  
     }
 222  
 
 223  
     private AbstractGraphPanelVisualizer getGUIObject(String pluginType) {
 224  
         Class a;
 225  
         try {
 226  24
             a = Class.forName(pluginType);
 227  16
         } catch (ClassNotFoundException ex) {
 228  16
             if (!pluginType.endsWith("Gui")) {
 229  8
                 return getGUIObject(pluginType + "Gui");
 230  
             }
 231  
 
 232  8
             if (!pluginType.startsWith("kg.apc.jmeter.vizualizers.")) {
 233  8
                 return getGUIObject("kg.apc.jmeter.vizualizers." + pluginType);
 234  
             }
 235  
 
 236  0
             throw new RuntimeException(ex);
 237  8
         }
 238  
 
 239  
         try {
 240  8
             return (AbstractGraphPanelVisualizer) a.newInstance();
 241  0
         } catch (InstantiationException ex) {
 242  0
             throw new RuntimeException(ex);
 243  0
         } catch (IllegalAccessException ex) {
 244  0
             throw new RuntimeException(ex);
 245  
         }
 246  
     }
 247  
 
 248  
     private void setOptions(AbstractGraphPanelVisualizer gui) {
 249  14
         GraphPanelChart graph = gui.getGraphPanelChart();
 250  
 
 251  14
         if (aggregate >= 0) {
 252  4
             gui.switchModel(aggregate > 0);
 253  
         }
 254  13
         if (granulation >= 0) {
 255  0
             gui.setGranulation(granulation);
 256  
         }
 257  13
         if (relativeTimes >= 0) {
 258  0
             graph.setUseRelativeTime(relativeTimes > 0);
 259  
         }
 260  13
         if (lineWeight >= 0) {
 261  0
             graph.getChartSettings().setLineWidth(lineWeight);
 262  
         }
 263  
 
 264  
 
 265  13
         if (gradient >= 0) {
 266  0
             graph.getChartSettings().setDrawGradient(gradient > 0);
 267  
         }
 268  13
         if (zeroing >= 0) {
 269  0
             graph.getChartSettings().setDrawFinalZeroingLines(zeroing > 0);
 270  
         }
 271  13
         if (rowsLimit >= 0) {
 272  0
             graph.getChartSettings().setMaxPointPerRow(rowsLimit);
 273  
         }
 274  13
         if (preventOutliers >= 0) {
 275  0
             graph.getChartSettings().setPreventXAxisOverScaling(preventOutliers > 0);
 276  
         }
 277  13
         if (lowCounts >= 0) {
 278  0
             graph.getChartSettings().setHideNonRepValLimit(lowCounts);
 279  
         }
 280  13
         if (forceY >= 0) {
 281  0
             graph.getChartSettings().setForcedMaxY(forceY);
 282  
         }
 283  13
         if (autoScaleRows >= 0) {
 284  0
             graph.getChartSettings().setExpendRows(autoScaleRows > 0);
 285  
         }
 286  13
         if (markers >= 0) {
 287  0
             graph.getChartSettings().setChartMarkers(markers > 0 ? ChartSettings.CHART_MARKERS_YES : ChartSettings.CHART_MARKERS_NO);
 288  
         }
 289  
 
 290  13
     }
 291  
 
 292  
     public void setAggregate(int logicValue) {
 293  3
         aggregate = logicValue;
 294  3
     }
 295  
 
 296  
     public void setZeroing(int logicValue) {
 297  1
         zeroing = logicValue;
 298  1
     }
 299  
 
 300  
     public void setPreventOutliers(int logicValue) {
 301  1
         preventOutliers = logicValue;
 302  1
     }
 303  
 
 304  
     public void setRowsLimit(int parseInt) {
 305  1
         rowsLimit = parseInt;
 306  1
     }
 307  
 
 308  
     public void setForceY(int parseInt) {
 309  1
         forceY = parseInt;
 310  1
     }
 311  
 
 312  
     public void setHideLowCounts(int parseInt) {
 313  1
         lowCounts = parseInt;
 314  1
     }
 315  
 
 316  
     public void setGranulation(int parseInt) {
 317  1
         granulation = parseInt;
 318  1
     }
 319  
 
 320  
     public void setRelativeTimes(int logicValue) {
 321  1
         relativeTimes = logicValue;
 322  1
     }
 323  
 
 324  
     public void setGradient(int logicValue) {
 325  1
         gradient = logicValue;
 326  1
     }
 327  
 
 328  
     public void setMarkers(int logicValue) {
 329  0
         markers = logicValue;
 330  0
     }
 331  
 
 332  
     public void setIncludeLabels(String string) {
 333  2
         includeLabels = string;
 334  2
     }
 335  
 
 336  
     public void setExcludeLabels(String string) {
 337  1
         excludeLabels = string;
 338  1
     }
 339  
 
 340  
     public void setIncludeSamplesWithRegex(int logicValue) {
 341  1
         includeSamplesWithRegex = logicValue;
 342  1
     }
 343  
 
 344  
     public void setExcludeSamplesWithRegex(int logicValue) {
 345  1
         excludeSamplesWithRegex = logicValue;
 346  1
     }
 347  
 
 348  
     public void setStartOffset(String string) {
 349  1
         startOffset = string;
 350  1
     }
 351  
 
 352  
     public void setEndOffset(String string) {
 353  1
         endOffset = string;
 354  1
     }
 355  
 
 356  
     public void setAutoScaleRows(int logicValue) {
 357  1
         autoScaleRows = logicValue;
 358  1
     }
 359  
 
 360  
     public void setLineWeight(float parseInt) {
 361  1
         lineWeight = parseInt;
 362  1
     }
 363  
 
 364  
     public void setSuccessFilter(int logicValue) {
 365  1
         successFilter = logicValue;
 366  1
     }
 367  
 
 368  
     private void forceDir(File resultFile) {
 369  7
         File parent = resultFile.getParentFile();
 370  7
         if (parent != null) {
 371  7
             if (!parent.mkdirs() && !parent.exists()) {
 372  0
                 throw new RuntimeException("Failed to create directory for " + resultFile.getAbsolutePath());
 373  
             }
 374  
         }
 375  7
     }
 376  
 
 377  
     public void processUnknownOption(String nextArg, ListIterator args) {
 378  1
         if (pluginType != null && pluginType instanceof CMDLineArgumentsProcessor) {
 379  0
             log.debug("Trying to process unknown option using CMDLineArgumentsProcessor: " + nextArg);
 380  0
             CMDLineArgumentsProcessor obj = (CMDLineArgumentsProcessor) pluginType;
 381  0
             obj.processCMDOption(nextArg, args);
 382  0
         } else {
 383  1
             throw new UnsupportedOperationException("Unrecognized option: " + nextArg);
 384  
         }
 385  0
     }
 386  
 }