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.samplers;
020:
021: import org.apache.jmeter.util.JMeterUtils;
022: import org.apache.log.Logger;
023: import org.apache.jorphan.logging.LoggingManager;
024:
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.rmi.RemoteException;
028: import java.io.Serializable;
029:
030: /**
031: * Implements batch reporting for remote testing.
032: *
033: * @author Michael Freeman
034: */
035: public class BatchSampleSender implements SampleSender, Serializable {
036: private static final Logger log = LoggingManager
037: .getLoggerForClass();
038:
039: private static final int DEFAULT_NUM_SAMPLE_THRESHOLD = 100;
040:
041: private static final long DEFAULT_TIME_THRESHOLD = 60000L;
042:
043: private RemoteSampleListener listener;
044:
045: private List sampleStore = new ArrayList();
046:
047: private int numSamplesThreshold;
048:
049: private long timeThreshold;
050:
051: private long batchSendTime = -1;
052:
053: public BatchSampleSender() {
054: log.warn("Constructor only intended for use in testing"); // $NON-NLS-1$
055: }
056:
057: /**
058: * Constructor
059: *
060: * @param listener
061: * that the List of sample events will be sent to.
062: */
063: BatchSampleSender(RemoteSampleListener listener) {
064: this .listener = listener;
065: init();
066: log.info("Using batching for this run." + " Thresholds: num="
067: + numSamplesThreshold + ", time=" + timeThreshold);
068: }
069:
070: /**
071: * Checks for the Jmeter properties num_sample_threshold and time_threshold,
072: * and assigns defaults if not found.
073: */
074: private void init() {
075: this .numSamplesThreshold = JMeterUtils.getPropDefault(
076: "num_sample_threshold", DEFAULT_NUM_SAMPLE_THRESHOLD);
077: this .timeThreshold = JMeterUtils.getPropDefault(
078: "time_threshold", DEFAULT_TIME_THRESHOLD);
079: }
080:
081: /**
082: * Checks if any sample events are still present in the sampleStore and
083: * sends them to the listener. Informs the listener of the testended.
084: */
085: public void testEnded() {
086: try {
087: if (sampleStore.size() != 0) {
088: listener.processBatch(sampleStore);
089: sampleStore.clear();
090: }
091: listener.testEnded();
092: } catch (RemoteException err) {
093: log.error("testEnded()", err);
094: }
095: }
096:
097: /**
098: * Checks if any sample events are still present in the sampleStore and
099: * sends them to the listener. Informs the listener of the testended.
100: *
101: * @param host
102: * the host that the test has ended on.
103: */
104: public void testEnded(String host) {
105: try {
106: if (sampleStore.size() != 0) {
107: listener.processBatch(sampleStore);
108: sampleStore.clear();
109: }
110: listener.testEnded(host);
111: } catch (RemoteException err) {
112: log.error("testEnded(host)", err);
113: }
114: }
115:
116: /**
117: * Stores sample events untill either a time or sample threshold is
118: * breached. Both thresholds are reset if one fires. If only one threshold
119: * is set it becomes the only value checked against. When a threhold is
120: * breached the list of sample events is sent to a listener where the event
121: * are fired locally.
122: *
123: * @param e
124: * a Sample Event
125: */
126: public void SampleOccurred(SampleEvent e) {
127: synchronized (sampleStore) {
128: sampleStore.add(e);
129:
130: if (numSamplesThreshold != -1) {
131: if (sampleStore.size() >= numSamplesThreshold) {
132: try {
133: log.debug("Firing sample");
134: listener.processBatch(sampleStore);
135: sampleStore.clear();
136: } catch (RemoteException err) {
137: log.error("sampleOccurred", err);
138: }
139: }
140: }
141:
142: if (timeThreshold != -1) {
143: SampleResult sr = e.getResult();
144: long timestamp = sr.getTimeStamp();
145:
146: // Checking for and creating initial timestamp to cheak against
147: if (batchSendTime == -1) {
148: this .batchSendTime = timestamp + timeThreshold;
149: }
150:
151: if (batchSendTime < timestamp) {
152: try {
153: log.debug("Firing time");
154: if (sampleStore.size() > 0) {
155: listener.processBatch(sampleStore);
156: sampleStore.clear();
157: }
158: this .batchSendTime = timestamp + timeThreshold;
159: } catch (RemoteException err) {
160: log.error("sampleOccurred", err);
161: }
162: }
163: }
164: }
165: }
166: }
|