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.examples.sampler;
020:
021: import org.apache.jmeter.samplers.AbstractSampler;
022: import org.apache.jmeter.samplers.Entry;
023: import org.apache.jmeter.samplers.SampleResult;
024: import org.apache.jorphan.logging.LoggingManager;
025: import org.apache.log.Logger;
026:
027: /**
028: * Example Sampler (non-Bean version)
029: *
030: * JMeter creates an instance of a sampler class for every occurrence of the
031: * element in every thread. [some additional copies may be created before the
032: * test run starts]
033: *
034: * Thus each sampler is guaranteed to be called by a single thread - there is no
035: * need to synchronize access to instance variables.
036: *
037: * However, access to class fields must be synchronized.
038: *
039: * @version $Revision: 493780 $ $Date: 2007-01-07 17:49:30 +0000 (Sun, 07 Jan 2007) $
040: */
041: public class ExampleSampler extends AbstractSampler {
042:
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: // The name of the property used to hold our data
047: public final static String DATA = "ExampleSampler.data"; //$NON-NLS-1$
048:
049: private static int classCount = 0; // keep track of classes created
050:
051: // (for instructional purposes only!)
052:
053: public ExampleSampler() {
054: classCount++;
055: trace("ExampleSampler()");
056: }
057:
058: /*
059: * (non-Javadoc) Performs the sample, and returns the result
060: *
061: * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
062: */
063: public SampleResult sample(Entry e) {
064: trace("sample()");
065: SampleResult res = new SampleResult();
066: boolean isOK = false; // Did sample succeed?
067: String data = getData(); // Sampler data
068: String response = null;
069:
070: res.setSampleLabel(getTitle());
071: /*
072: * Perform the sampling
073: */
074: res.sampleStart(); // Start timing
075: try {
076:
077: // Do something here ...
078:
079: response = Thread.currentThread().getName();
080:
081: /*
082: * Set up the sample result details
083: */
084: res.setSamplerData(data);
085: res.setResponseData(response.getBytes());
086: res.setDataType(SampleResult.TEXT);
087:
088: res.setResponseCodeOK();
089: res.setResponseMessage("OK");// $NON-NLS-1$
090: isOK = true;
091: } catch (Exception ex) {
092: log.debug("", ex);
093: res.setResponseCode("500");// $NON-NLS-1$
094: res.setResponseMessage(ex.toString());
095: }
096: res.sampleEnd(); // End timimg
097:
098: res.setSuccessful(isOK);
099:
100: return res;
101: }
102:
103: /**
104: * @return a string for the sampleResult Title
105: */
106: private String getTitle() {
107: return this .getName();
108: }
109:
110: /**
111: * @return the data for the sample
112: */
113: public String getData() {
114: return getPropertyAsString(DATA);
115: }
116:
117: /*
118: * Helper method
119: */
120: private void trace(String s) {
121: String tl = getTitle();
122: String tn = Thread.currentThread().getName();
123: String th = this .toString();
124: log.debug(tn + " (" + classCount + ") " + tl + " " + s + " "
125: + th);
126: }
127: }
|