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.io.Serializable;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: import org.apache.jmeter.samplers.Clearable;
028: import org.apache.jmeter.samplers.SampleResult;
029: import org.apache.jorphan.logging.LoggingManager;
030: import org.apache.log.Logger;
031:
032: /**
033: * The model that collects the average of the set of pages to be sampled.
034: *
035: */
036:
037: public class GraphAccumModel implements Clearable, Serializable {
038: private static final Logger log = LoggingManager
039: .getLoggerForClass();
040:
041: protected String name;
042:
043: protected List samples;
044:
045: protected List listeners;
046:
047: protected long averageSum = 0;
048:
049: protected long variationSum = 0;
050:
051: protected long counter = 0;
052:
053: protected long previous = 0;
054:
055: protected long max = 1;
056:
057: protected boolean bigChange = false;
058:
059: protected SampleResult current;
060:
061: /**
062: * Constructor.
063: */
064: public GraphAccumModel() {
065: log.debug("Start : GraphAccumModel1");
066: listeners = new LinkedList();
067: samples = Collections.synchronizedList(new LinkedList());
068: log.debug("End : GraphAccumModel1");
069: }
070:
071: /**
072: * Sets the Name attribute of the GraphModel object.
073: *
074: * @param name
075: * the new Name value
076: */
077: public void setName(String name) {
078: this .name = name;
079: }
080:
081: /**
082: * Gets the SampleCount attribute of the GraphAccumModel object.
083: *
084: * @return the SampleCount value
085: */
086: public int getSampleCount() {
087: return samples.size();
088: }
089:
090: /**
091: * Gets the List attribute of the GraphAccumModel object.
092: *
093: * @return the List value
094: */
095: public List getList() {
096: return samples;
097: }
098:
099: /**
100: * Gets the Name attribute of the GraphModel object.
101: *
102: * @return the Name value
103: */
104: public String getName() {
105: return name;
106: }
107:
108: /**
109: * Gets the Max attribute of the GraphAccumModel object.
110: *
111: * @return the Max value
112: */
113: public long getMax() {
114: log.debug("getMax1 : Returning - " + max);
115: return max;
116: }
117:
118: /**
119: * Adds a feature to the ModelListener attribute of the GraphAccumModel
120: * object.
121: *
122: * @param listener
123: * the feature to be added to the GraphAccumListener attribute.
124: */
125: public void addGraphAccumListener(GraphAccumListener listener) {
126: listeners.add(listener);
127: }
128:
129: /**
130: * Clear the results.
131: */
132: public void clearData() {
133: log.debug("Start : clear1");
134: samples.clear();
135: max = 1;
136: bigChange = true;
137: this .fireDataChanged();
138: log.debug("End : clear1");
139: }
140:
141: /**
142: * Add the new sample to the results.
143: *
144: * @param res
145: * sample containing the results
146: */
147: public void addNewSample(SampleResult res) {
148: log.debug("Start : addNewSample1");
149: // Set time to time taken to load this url without components (e.g.
150: // images etc)
151: long totalTime = res.getTime();
152:
153: if (log.isDebugEnabled()) {
154: log.debug("addNewSample1 : time - " + totalTime);
155: log.debug("addNewSample1 : max - " + max);
156: }
157: if (totalTime > max) {
158: bigChange = true;
159: max = totalTime;
160: }
161: current = res;
162: samples.add(res);
163: log.debug("End : addNewSample1");
164: fireDataChanged();
165: }
166:
167: /**
168: * Depending on whether the graph needs to be rescale call the appropriate
169: * methods.
170: */
171: protected void fireDataChanged() {
172: log.debug("Start : fireDataChanged1");
173: Iterator iter = listeners.iterator();
174:
175: if (bigChange) {
176: while (iter.hasNext()) {
177: ((GraphAccumListener) iter.next()).updateGui();
178: }
179: bigChange = false;
180: } else {
181: quickUpdate(current);
182: }
183: log.debug("End : fireDataChanged1");
184: }
185:
186: /**
187: * The sample to be added did not exceed the current set of samples so do
188: * not need to rescale graph.
189: */
190: protected void quickUpdate(SampleResult s) {
191: Iterator iter = listeners.iterator();
192: {
193: while (iter.hasNext()) {
194: ((GraphAccumListener) iter.next()).updateGui(s);
195: }
196: }
197: }
198: }
|