001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.visualizers;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.GridLayout;
026: import java.awt.Image;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JComponent;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.border.Border;
033: import javax.swing.border.EmptyBorder;
034:
035: import org.apache.jmeter.samplers.Clearable;
036: import org.apache.jmeter.samplers.SampleResult;
037: import org.apache.jmeter.util.JMeterUtils;
038: import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
039: import org.apache.jorphan.gui.layout.VerticalLayout;
040:
041: /**
042: * This class implements a statistical analyser that takes samples to process a
043: * Spline interpolated curve. Currently, it tries to look mostly like the
044: * GraphVisualizer.
045: *
046: */
047: public class SplineVisualizer extends AbstractVisualizer implements
048: ImageVisualizer, GraphListener, Clearable {
049:
050: private static final String SUFFIX_MS = " ms"; //$NON-NLS-1$
051:
052: protected final Color BACKGROUND_COLOR = getBackground();
053:
054: protected final Color MINIMUM_COLOR = new Color(0F, 0.5F, 0F);
055:
056: protected final Color MAXIMUM_COLOR = new Color(0.9F, 0F, 0F);
057:
058: protected final Color AVERAGE_COLOR = new Color(0F, 0F, 0.75F);
059:
060: protected final Color INCOMING_COLOR = Color.black;
061:
062: protected final int NUMBERS_TO_DISPLAY = 4;
063:
064: protected final boolean FILL_UP_WITH_ZEROS = false;
065:
066: transient private SplineGraph graph = null;
067:
068: private JLabel minimumLabel = null;
069:
070: private JLabel maximumLabel = null;
071:
072: private JLabel averageLabel = null;
073:
074: private JLabel incomingLabel = null;
075:
076: private JLabel minimumNumberLabel = null;
077:
078: private JLabel maximumNumberLabel = null;
079:
080: private JLabel averageNumberLabel = null;
081:
082: private JLabel incomingNumberLabel = null;
083:
084: transient private SplineModel model;
085:
086: public SplineVisualizer() {
087: super ();
088: model = new SplineModel();
089: graph = new SplineGraph();
090: this .model.setListener(this );
091: setGUI();
092: }
093:
094: public void add(SampleResult res) {
095: model.add(res);
096: }
097:
098: public String getLabelResource() {
099: return "spline_visualizer_title"; //$NON-NLS-1$
100: }
101:
102: public void updateGui(Sample s) {
103: updateGui();
104: }
105:
106: public void clearData() {
107: model.clearData();
108: }
109:
110: private void setGUI() {
111: Color backColor = BACKGROUND_COLOR;
112:
113: this .setBackground(backColor);
114:
115: this .setLayout(new BorderLayout());
116:
117: // MAIN PANEL
118: JPanel mainPanel = new JPanel();
119: Border margin = new EmptyBorder(10, 10, 5, 10);
120:
121: mainPanel.setBorder(margin);
122: mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.BOTH));
123:
124: // NAME
125: mainPanel.add(makeTitlePanel());
126:
127: maximumLabel = new JLabel(JMeterUtils
128: .getResString("spline_visualizer_maximum")); //$NON-NLS-1$
129: maximumLabel.setForeground(MAXIMUM_COLOR);
130: maximumLabel.setBackground(backColor);
131:
132: averageLabel = new JLabel(JMeterUtils
133: .getResString("spline_visualizer_average")); //$NON-NLS-1$
134: averageLabel.setForeground(AVERAGE_COLOR);
135: averageLabel.setBackground(backColor);
136:
137: incomingLabel = new JLabel(JMeterUtils
138: .getResString("spline_visualizer_incoming")); //$NON-NLS-1$
139: incomingLabel.setForeground(INCOMING_COLOR);
140: incomingLabel.setBackground(backColor);
141:
142: minimumLabel = new JLabel(JMeterUtils
143: .getResString("spline_visualizer_minimum")); //$NON-NLS-1$
144: minimumLabel.setForeground(MINIMUM_COLOR);
145: minimumLabel.setBackground(backColor);
146:
147: maximumNumberLabel = new JLabel("0 ms"); //$NON-NLS-1$
148: maximumNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
149: maximumNumberLabel.setForeground(MAXIMUM_COLOR);
150: maximumNumberLabel.setBackground(backColor);
151:
152: averageNumberLabel = new JLabel("0 ms"); //$NON-NLS-1$
153: averageNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
154: averageNumberLabel.setForeground(AVERAGE_COLOR);
155: averageNumberLabel.setBackground(backColor);
156:
157: incomingNumberLabel = new JLabel("0 ms"); //$NON-NLS-1$
158: incomingNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
159: incomingNumberLabel.setForeground(INCOMING_COLOR);
160: incomingNumberLabel.setBackground(backColor);
161:
162: minimumNumberLabel = new JLabel("0 ms"); //$NON-NLS-1$
163: minimumNumberLabel.setHorizontalAlignment(JLabel.RIGHT);
164: minimumNumberLabel.setForeground(MINIMUM_COLOR);
165: minimumNumberLabel.setBackground(backColor);
166:
167: // description Panel
168: JPanel labelPanel = new JPanel();
169:
170: labelPanel.setLayout(new GridLayout(0, 1));
171: labelPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
172: 20));
173: labelPanel.setBackground(backColor);
174: labelPanel.add(maximumLabel);
175: labelPanel.add(averageLabel);
176: if (model.SHOW_INCOMING_SAMPLES) {
177: labelPanel.add(incomingLabel);
178: }
179: labelPanel.add(minimumLabel);
180: // number Panel
181: JPanel numberPanel = new JPanel();
182:
183: numberPanel.setLayout(new GridLayout(0, 1));
184: numberPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
185: 20));
186: numberPanel.setBackground(backColor);
187: numberPanel.add(maximumNumberLabel);
188: numberPanel.add(averageNumberLabel);
189: if (model.SHOW_INCOMING_SAMPLES) {
190: numberPanel.add(incomingNumberLabel);
191: }
192: numberPanel.add(minimumNumberLabel);
193: // information display Panel
194: JPanel infoPanel = new JPanel();
195:
196: infoPanel.setLayout(new BorderLayout());
197: infoPanel.add(labelPanel, BorderLayout.CENTER);
198: infoPanel.add(numberPanel, BorderLayout.EAST);
199:
200: this .add(mainPanel, BorderLayout.NORTH);
201: this .add(infoPanel, BorderLayout.WEST);
202: this .add(graph, BorderLayout.CENTER);
203: // everyone is free to swing on its side :)
204: // add(infoPanel, BorderLayout.EAST);
205: }
206:
207: public void updateGui() {
208: repaint();
209: synchronized (this ) {
210: setMinimum(model.getMinimum());
211: setMaximum(model.getMaximum());
212: setAverage(model.getAverage());
213: setIncoming(model.getCurrent());
214: }
215: }
216:
217: public String toString() {
218: return "Show the samples analysis as a Spline curve";
219: }
220:
221: private String formatMeasureToDisplay(long measure) {
222: String numberString = String.valueOf(measure);
223:
224: if (FILL_UP_WITH_ZEROS) {
225: for (int i = numberString.length(); i < NUMBERS_TO_DISPLAY; i++) {
226: numberString = "0" + numberString; //$NON-NLS-1$
227: }
228: }
229: return numberString;
230: }
231:
232: private void setMinimum(long n) {
233: String text = this .formatMeasureToDisplay(n) + SUFFIX_MS;
234:
235: this .minimumNumberLabel.setText(text);
236: }
237:
238: private void setMaximum(long n) {
239: String text = this .formatMeasureToDisplay(n) + SUFFIX_MS;
240:
241: this .maximumNumberLabel.setText(text);
242: }
243:
244: private void setAverage(long n) {
245: String text = this .formatMeasureToDisplay(n) + SUFFIX_MS;
246:
247: this .averageNumberLabel.setText(text);
248: }
249:
250: private void setIncoming(long n) {
251: String text = this .formatMeasureToDisplay(n) + SUFFIX_MS;
252:
253: this .incomingNumberLabel.setText(text);
254: }
255:
256: public JPanel getControlPanel() {// TODO - is this needed?
257: return this ;
258: }
259:
260: public Image getImage() {
261: Image result = graph.createImage(graph.getWidth(), graph
262: .getHeight());
263:
264: graph.paintComponent(result.getGraphics());
265:
266: return result;
267: }
268:
269: /**
270: * Component showing a Spline curve.
271: *
272: */
273: public class SplineGraph extends JComponent {
274: public boolean reinterpolated = false;
275:
276: protected final Color WAITING_COLOR = Color.darkGray;
277:
278: protected int lastWidth = -1;
279:
280: protected int lastHeight = -1;
281:
282: protected int[] plot = null;
283:
284: public SplineGraph() {
285: }
286:
287: /**
288: * Clear the Spline graph and get ready for the next wave.
289: */
290: public void clear() {
291: lastWidth = -1;
292: lastHeight = -1;
293: plot = null;
294: this .repaint();
295: }
296:
297: public void paintComponent(Graphics g) {
298: super .paintComponent(g);
299:
300: Dimension dimension = this .getSize();
301: int width = dimension.width;
302: int height = dimension.height;
303:
304: if (model.getDataCurve() == null) {
305: g.setColor(this .getBackground());
306: g.fillRect(0, 0, width, height);
307: g.setColor(WAITING_COLOR);
308: g
309: .drawString(
310: JMeterUtils
311: .getResString("spline_visualizer_waitingmessage"), //$NON-NLS-1$
312: (width - 120) / 2, height
313: - (height - 12) / 2);
314: return;
315: }
316:
317: // boolean resized = true;
318:
319: if (width == lastWidth && height == lastHeight) {
320: // dimension of the SplineGraph is the same
321: // resized = false;
322: } else {
323: // dimension changed
324: // resized = true;
325: lastWidth = width;
326: lastHeight = height;
327: }
328:
329: this .plot = model.getDataCurve().getPlots(width, height); // rounds!
330:
331: int n = plot.length;
332: int curY = plot[0];
333:
334: for (int i = 1; i < n; i++) {
335: g.setColor(Color.black);
336: g.drawLine(i - 1, height - curY - 1, i, height
337: - plot[i] - 1);
338: curY = plot[i];
339: }
340: }
341: }
342: }
|