KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > examples > sampler > ExampleSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/examples/org/apache/jmeter/examples/sampler/ExampleSampler.java,v 1.1.2.1 2004/05/24 23:20:39 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.examples.sampler;
20
21 import org.apache.jmeter.samplers.AbstractSampler;
22 import org.apache.jmeter.samplers.Entry;
23 import org.apache.jmeter.samplers.SampleResult;
24 import org.apache.jorphan.logging.LoggingManager;
25 import org.apache.log.Logger;
26
27 /**
28  * Example Sampler (non-Bean version)
29  *
30  * JMeter creates an instance of a sampler class for every
31  * occurrence of the element in every thread.
32  * [some additional copies may be created before the test run starts]
33  *
34  * Thus each sampler is guaranteed to be called by a single thread -
35  * there is no need to synchronize access to instance variables.
36  *
37  * However, access to class fields must be synchronized.
38  *
39  * @version $Revision: 1.1.2.1 $ $Date: 2004/05/24 23:20:39 $
40  */

41 public class ExampleSampler extends AbstractSampler
42 {
43
44     protected static Logger log = LoggingManager.getLoggerForClass();
45
46     // The name of the property used to hold our data
47
public final static String JavaDoc DATA = "ExampleSampler.data"; //$NON-NLS-1$
48

49     private transient static int classCount=0; // keep track of classes created
50
// (for instructional purposes only!)
51

52     public ExampleSampler()
53     {
54         classCount++;
55         trace("ExampleSampler()");
56     }
57
58     /* (non-Javadoc)
59      * Performs the sample, and returns the result
60      *
61      * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
62      */

63     public SampleResult sample(Entry e)
64     {
65         trace("sample()");
66         SampleResult res = new SampleResult();
67         boolean isOK = false; // Did sample succeed?
68
String JavaDoc data=getData(); // Sampler data
69
String JavaDoc response=null;
70         
71         res.setSampleLabel(getTitle());
72         /*
73          * Perform the sampling
74          */

75         res.sampleStart(); //Start timing
76
try {
77             
78             // Do something here ...
79

80             response=Thread.currentThread().getName();
81             
82             /*
83              * Set up the sample result details
84              */

85             res.setSamplerData(data);
86             res.setResponseData(response.getBytes());
87             res.setDataType(SampleResult.TEXT);
88             
89             res.setResponseCode("200");
90             res.setResponseMessage("OK");
91             isOK = true;
92         }
93         catch (Exception JavaDoc ex){
94             log.debug("",ex);
95             res.setResponseCode("500");
96             res.setResponseMessage(ex.toString());
97         }
98         res.sampleEnd(); //End timimg
99

100         res.setSuccessful(isOK);
101
102         return res;
103     }
104
105     /**
106      * @return a string for the sampleResult Title
107      */

108     private String JavaDoc getTitle()
109     {
110         return this.getName();
111     }
112     
113     /**
114      * @return the data for the sample
115      */

116     public String JavaDoc getData()
117     {
118         return getPropertyAsString(DATA);
119     }
120     
121     /*
122      * Helper method
123      */

124     private void trace(String JavaDoc s)
125     {
126         String JavaDoc tl = getTitle();
127         String JavaDoc tn = Thread.currentThread().getName();
128         String JavaDoc th = this.toString();
129         log.debug(tn+" ("+classCount+") "+tl+" "+s+" "+th);
130     }
131 }
Popular Tags