|
| 1 | +package kg.apc.jmeter.vizualizers; |
| 2 | + |
| 3 | +import kg.apc.charting.AbstractGraphRow; |
| 4 | +import kg.apc.charting.rows.GraphRowOverTimePercentile; |
| 5 | +import kg.apc.jmeter.JMeterPluginsUtils; |
| 6 | +import kg.apc.jmeter.graphs.AbstractOverTimeVisualizer; |
| 7 | +import org.apache.jmeter.samplers.SampleResult; |
| 8 | +import org.apache.jmeter.util.JMeterUtils; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.awt.*; |
| 13 | +import java.text.DecimalFormat; |
| 14 | +import java.util.*; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class ResponseTimesPercentilesOverTimeGui extends AbstractOverTimeVisualizer { |
| 19 | + |
| 20 | + private static final Logger log = LoggerFactory.getLogger(ResponseTimesPercentilesOverTimeGui.class); |
| 21 | + protected List<Double> percentiles; |
| 22 | + protected Map<Double, Double> shadingFactors; // render each percentile with a lighter alpha value |
| 23 | + protected Map<String, Color> baseColors; |
| 24 | + |
| 25 | + public ResponseTimesPercentilesOverTimeGui() { |
| 26 | + super(); |
| 27 | + setGranulation(30000); |
| 28 | + getGraphPanelChart().getChartSettings().setLineWidth(3); |
| 29 | + getGraphPanelChart().setYAxisLabel("Response times in ms"); |
| 30 | + |
| 31 | + String percentilesConfig = JMeterUtils.getPropDefault("jmeterPlugin.percentilesOverTime", "50,90,95,99"); |
| 32 | + try { |
| 33 | + percentiles = Arrays.stream(percentilesConfig.split(",")) |
| 34 | + .map(Double::valueOf) |
| 35 | + .collect(Collectors.toList()); |
| 36 | + } catch (NumberFormatException e) { |
| 37 | + log.error("Invalid percentiles configuration", e); |
| 38 | + percentiles = Collections.singletonList(50d); |
| 39 | + } |
| 40 | + shadingFactors = new HashMap<>(); |
| 41 | + for (int i = 0; i < percentiles.size(); i++) { |
| 42 | + // color shades should not get lighter than an alpha value of 32 out of 255 |
| 43 | + shadingFactors.put(percentiles.get(i), percentiles.size()>1 ? Math.pow(32d/256d, (double)i/(percentiles.size()-1)) : 1); |
| 44 | + } |
| 45 | + baseColors = new HashMap<>(); |
| 46 | + } |
| 47 | + |
| 48 | + public String getLabelResource() { |
| 49 | + return this.getClass().getSimpleName(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getStaticLabel() { |
| 54 | + return JMeterPluginsUtils.prefixLabel("Response Times Percentiles Over Time"); |
| 55 | + } |
| 56 | + |
| 57 | + protected synchronized AbstractGraphRow getNewRow(String label, boolean isAggregate, double percentile) { |
| 58 | + final AbstractGraphRow newRow = new GraphRowOverTimePercentile(percentile); |
| 59 | + final String suffix = new DecimalFormat(" (p#.####)").format(percentile); |
| 60 | + newRow.setLabel(label+suffix); |
| 61 | + newRow.setMarkerSize(AbstractGraphRow.MARKER_SIZE_NONE); |
| 62 | + newRow.setDrawBar(false); |
| 63 | + newRow.setDrawLine(true); |
| 64 | + newRow.setDrawValueLabel(false); |
| 65 | + newRow.setDrawThickLines(false); |
| 66 | + newRow.setShowInLegend(true); |
| 67 | + |
| 68 | + // Avoid cycling of colors for subsequent percentiles: Let base class assign color only once, then store it. |
| 69 | + Color color = baseColors.get(label); |
| 70 | + if (color != null) { |
| 71 | + color = new Color(color.getRed(), color.getGreen(), color.getBlue(), |
| 72 | + (int)(color.getAlpha() * shadingFactors.get(percentile))); |
| 73 | + } |
| 74 | + AbstractGraphRow addedRow = getNewRow(newRow, color, isAggregate, true); |
| 75 | + if (color == null) { |
| 76 | + baseColors.put(label, addedRow.getColor()); |
| 77 | + } |
| 78 | + return addedRow; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void add(SampleResult res) { |
| 83 | + if (!isSampleIncluded(res)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + super.add(res); |
| 87 | + |
| 88 | + final String labelAgg = "Overall Response Times"; |
| 89 | + String label = res.getSampleLabel(); |
| 90 | + long time = normalizeTime(res.getEndTime()); |
| 91 | + long elapsed = res.getTime(); |
| 92 | + |
| 93 | + for (double p : percentiles) { |
| 94 | + getNewRow(label, false, p).add(time, elapsed); |
| 95 | + getNewRow(labelAgg, true, p).add(time, elapsed); |
| 96 | + } |
| 97 | + |
| 98 | + updateGui(null); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected JSettingsPanel createSettingsPanel() { |
| 103 | + return new JSettingsPanel(this, |
| 104 | + JSettingsPanel.TIMELINE_OPTION |
| 105 | + | JSettingsPanel.GRADIENT_OPTION |
| 106 | + | JSettingsPanel.LIMIT_POINT_OPTION |
| 107 | + | JSettingsPanel.AGGREGATE_OPTION |
| 108 | + | JSettingsPanel.MAXY_OPTION |
| 109 | + | JSettingsPanel.RELATIVE_TIME_OPTION |
| 110 | + | JSettingsPanel.MARKERS_OPTION); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getWikiPage() { |
| 115 | + return "ResponseTimesPercentilesOverTime"; |
| 116 | + } |
| 117 | +} |
0 commit comments