⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphvisualizer.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.visualizers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import org.apache.jmeter.gui.util.JMeterColor;
import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.gui.AbstractVisualizer;

/**
 * This class implements a statistical analyser that calculates both the average
 * and the standard deviation of the sampling process and outputs them as
 * autoscaling plots.
 * 
 * Created February 8, 2001
 * 
 */
public class GraphVisualizer extends AbstractVisualizer implements ImageVisualizer, ItemListener, Clearable {
    
	private static final String ZERO = "0";  //$NON-NLS-1$
	
	private NumberFormat nf = NumberFormat.getInstance(); // OK, because used in synchronised method

	private SamplingStatCalculator model;

	private JTextField maxYField = null;

	private JTextField minYField = null;

	private JTextField noSamplesField = null;

	private String minute = JMeterUtils.getResString("minute"); // $NON-NLS-1$

	private Graph graph;

	private JCheckBox data;

	private JCheckBox average;

	private JCheckBox deviation;

	private JCheckBox throughput;

	private JCheckBox median;

	private JTextField dataField;

	private JTextField averageField;

	private JTextField deviationField;

	private JTextField throughputField;

	private JTextField medianField;

	/**
	 * Constructor for the GraphVisualizer object.
	 */
	public GraphVisualizer() {
		model = new SamplingStatCalculator("Graph");
		graph = new Graph(model);
		init();
	}

	/**
	 * Gets the Image attribute of the GraphVisualizer object.
	 * 
	 * @return the Image value
	 */
	public Image getImage() {
		Image result = graph.createImage(graph.getWidth(), graph.getHeight());

		graph.paintComponent(result.getGraphics());

		return result;
	}

	public synchronized void updateGui(Sample s) {
		// We have received one more sample
		graph.updateGui(s);
		noSamplesField.setText(Long.toString(s.getCount()));
		dataField.setText(Long.toString(s.getData()));
		averageField.setText(Long.toString(s.getAverage()));
		deviationField.setText(Long.toString(s.getDeviation()));
		throughputField.setText(nf.format(60 * s.getThroughput()) + "/" + minute); // $NON-NLS-1$
		medianField.setText(Long.toString(s.getMedian()));
		updateYAxis();
	}

	public void add(SampleResult res) {
		updateGui(model.addSample(res));
	}

	public String getLabelResource() {
		return "graph_results_title"; // $NON-NLS-1$
	}

	public void itemStateChanged(ItemEvent e) {
		if (e.getItem() == data) {
			this.graph.enableData(e.getStateChange() == ItemEvent.SELECTED);
		} else if (e.getItem() == average) {
			this.graph.enableAverage(e.getStateChange() == ItemEvent.SELECTED);
		} else if (e.getItem() == deviation) {
			this.graph.enableDeviation(e.getStateChange() == ItemEvent.SELECTED);
		} else if (e.getItem() == throughput) {
			this.graph.enableThroughput(e.getStateChange() == ItemEvent.SELECTED);
		} else if (e.getItem() == median) {
			this.graph.enableMedian(e.getStateChange() == ItemEvent.SELECTED);
		}
		this.graph.repaint();
	}

	public void clearData() {
		graph.clearData();		
		model.clear();
		dataField.setText(ZERO);
		averageField.setText(ZERO);
		deviationField.setText(ZERO);
		throughputField.setText("0/" + minute); //$NON-NLS-1$
		medianField.setText(ZERO);
		noSamplesField.setText(ZERO);
		updateYAxis();
		repaint();
	}

	public String toString() {
		return "Show the samples analysis as dot plots";
	}

	/**
	 * Update the max and min value of the Y axis.
	 */
	private void updateYAxis() {
		maxYField.setText(Long.toString(graph.getGraphMax()));
		minYField.setText(ZERO);
	}

	/**
	 * Initialize the GUI.
	 */
	private void init() {
		this.setLayout(new BorderLayout());

		// MAIN PANEL
		Border margin = new EmptyBorder(10, 10, 5, 10);

		this.setBorder(margin);

		// Set up the graph with header, footer, Y axis and graph display
		JPanel graphPanel = new JPanel(new BorderLayout());
		graphPanel.add(createYAxis(), BorderLayout.WEST);
		graphPanel.add(createChoosePanel(), BorderLayout.NORTH);
		graphPanel.add(createGraphPanel(), BorderLayout.CENTER);
		graphPanel.add(createGraphInfoPanel(), BorderLayout.SOUTH);

		// Add the main panel and the graph
		this.add(makeTitlePanel(), BorderLayout.NORTH);
		this.add(graphPanel, BorderLayout.CENTER);
	}

	// Methods used in creating the GUI

	/**
	 * Creates the panel containing the graph's Y axis labels.
	 * 
	 * @return the Y axis panel
	 */
	private JPanel createYAxis() {
		JPanel graphYAxisPanel = new JPanel();

		graphYAxisPanel.setLayout(new BorderLayout());

		maxYField = createYAxisField(5);
		minYField = createYAxisField(3);

		graphYAxisPanel.add(createYAxisPanel("graph_results_ms", maxYField), BorderLayout.NORTH); // $NON-NLS-1$
		graphYAxisPanel.add(createYAxisPanel("graph_results_ms", minYField), BorderLayout.SOUTH); // $NON-NLS-1$

		return graphYAxisPanel;
	}

	/**
	 * Creates a text field to be used for the value of a Y axis label. These
	 * fields hold the minimum and maximum values for the graph. The units are
	 * kept in a separate label outside of this field.
	 * 
	 * @param length
	 *            the number of characters which the field will use to calculate

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -