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: package org.apache.jmeter.visualizers;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Graphics;
021: import java.awt.event.ItemEvent;
022: import java.awt.event.ItemListener;
023: import java.awt.Image;
024:
025: import javax.swing.border.Border;
026: import javax.swing.border.EmptyBorder;
027:
028: import org.apache.jmeter.samplers.Clearable;
029: import org.apache.jmeter.samplers.SampleResult;
030: import org.apache.jmeter.util.JMeterUtils;
031: import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
032:
033: import org.apache.jorphan.logging.LoggingManager;
034: import org.apache.log.Logger;
035:
036: /**
037: * For performance reasons, I am using tabs for the visualizers. Since a
038: * visualizer is heavy weight, I don not want to have two separate result
039: * collectors rather the same information. Instead, I would rather have the
040: * visualizer be the container for the data and simply pass the data to child
041: * JComponents. In the future, we may want to add email alerts as a third tab.
042: */
043: public class MonitorHealthVisualizer extends AbstractVisualizer
044: implements ImageVisualizer, ItemListener, GraphListener,
045: Clearable {
046: private MonitorTabPane TABPANE;
047:
048: private MonitorHealthPanel HEALTHPANE;
049:
050: private MonitorPerformancePanel PERFPANE;
051:
052: private MonitorAccumModel MODEL;
053:
054: private MonitorGraph GRAPH;
055:
056: public static final String BUFFER = "monitor.buffer.size";
057:
058: private static transient Logger log = LoggingManager
059: .getLoggerForClass();
060:
061: /**
062: * Constructor for the GraphVisualizer object.
063: */
064: public MonitorHealthVisualizer() {
065: this .isStats = true;
066: initModel();
067: init();
068: }
069:
070: private void initModel() {
071: MODEL = new MonitorAccumModel();
072: GRAPH = new MonitorGraph(MODEL);
073: MODEL.setBufferSize(JMeterUtils.getPropDefault(BUFFER, 800));
074: }
075:
076: public String getLabelResource() {
077: return "monitor_health_title"; // $NON-NLS-1$
078: }
079:
080: /**
081: * Because of the unique requirements of a monitor We have to handle the
082: * results differently than normal GUI components. A monitor should be able
083: * to run for a very long time without eating up all the memory.
084: */
085: public void add(SampleResult res) {
086: MODEL.addSample(res);
087: try {
088: collector.recordStats(this .MODEL.getLastSample()
089: .cloneMonitorStats());
090: } catch (Exception e) {
091: // for now just swallow the exception
092: log.debug("StatsModel was null", e);
093: }
094: }
095:
096: public Image getImage() {
097: Image result = GRAPH.createImage(this .getWidth(), this
098: .getHeight());
099: Graphics image = result.getGraphics();
100: GRAPH.paintComponent(image);
101: return result;
102: }
103:
104: public void itemStateChanged(ItemEvent e) {
105: }
106:
107: public synchronized void updateGui() {
108: this .repaint();
109: }
110:
111: public synchronized void updateGui(Sample s) {
112: this .repaint();
113: }
114:
115: /**
116: * Initialize the GUI.
117: */
118: private void init() {
119: this .setLayout(new BorderLayout());
120:
121: // MAIN PANEL
122: Border margin = new EmptyBorder(10, 10, 5, 10);
123: this .setBorder(margin);
124:
125: // Add the main panel and the graph
126: this .add(this .makeTitlePanel(), BorderLayout.NORTH);
127: this .createTabs();
128: }
129:
130: private void createTabs() {
131: TABPANE = new MonitorTabPane();
132: createHealthPane(TABPANE);
133: createPerformancePane(TABPANE);
134: this .add(TABPANE, BorderLayout.CENTER);
135: }
136:
137: /**
138: * Create the JPanel
139: *
140: * @param pane
141: */
142: private void createHealthPane(MonitorTabPane pane) {
143: HEALTHPANE = new MonitorHealthPanel(MODEL);
144: pane.addTab(JMeterUtils
145: .getResString("monitor_health_tab_title"), HEALTHPANE); // $NON-NLS-1$
146: }
147:
148: /**
149: * Create the JSplitPane for the performance history
150: *
151: * @param pane
152: */
153: private void createPerformancePane(MonitorTabPane pane) {
154: PERFPANE = new MonitorPerformancePanel(MODEL, GRAPH);
155: pane.addTab(JMeterUtils
156: .getResString("monitor_performance_tab_title"),
157: PERFPANE); // $NON-NLS-1$
158: }
159:
160: /**
161: * Clears the MonitorAccumModel.
162: */
163: public void clearData() {
164: this.MODEL.clearData();
165: this.HEALTHPANE.clearData();
166: this.PERFPANE.clearData();
167: }
168:
169: }
|