Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.examples.sampler » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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 occurrence of the
   31    * element in every thread. [some additional copies may be created before the
   32    * test run starts]
   33    *
   34    * Thus each sampler is guaranteed to be called by a single thread - there is no
   35    * need to synchronize access to instance variables.
   36    *
   37    * However, access to class fields must be synchronized.
   38    *
   39    * @version $Revision: 674365 $
   40    */
   41   public class ExampleSampler extends AbstractSampler {
   42   
   43       private static final Logger log = LoggingManager.getLoggerForClass();
   44   
   45       // The name of the property used to hold our data
   46       public final static String DATA = "ExampleSampler.data"; //$NON-NLS-1$
   47   
   48       private static int classCount = 0; // keep track of classes created
   49   
   50       // (for instructional purposes only!)
   51   
   52       public ExampleSampler() {
   53           classCount++;
   54           trace("ExampleSampler()");
   55       }
   56   
   57       /*
   58        * (non-Javadoc) Performs the sample, and returns the result
   59        *
   60        * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
   61        */
   62       public SampleResult sample(Entry e) {
   63           trace("sample()");
   64           SampleResult res = new SampleResult();
   65           boolean isOK = false; // Did sample succeed?
   66           String data = getData(); // Sampler data
   67           String response = null;
   68   
   69           res.setSampleLabel(getTitle());
   70           /*
   71            * Perform the sampling
   72            */
   73           res.sampleStart(); // Start timing
   74           try {
   75   
   76               // Do something here ...
   77   
   78               response = Thread.currentThread().getName();
   79   
   80               /*
   81                * Set up the sample result details
   82                */
   83               res.setSamplerData(data);
   84               res.setResponseData(response.getBytes());
   85               res.setDataType(SampleResult.TEXT);
   86   
   87               res.setResponseCodeOK();
   88               res.setResponseMessage("OK");// $NON-NLS-1$
   89               isOK = true;
   90           } catch (Exception ex) {
   91               log.debug("", ex);
   92               res.setResponseCode("500");// $NON-NLS-1$
   93               res.setResponseMessage(ex.toString());
   94           }
   95           res.sampleEnd(); // End timimg
   96   
   97           res.setSuccessful(isOK);
   98   
   99           return res;
  100       }
  101   
  102       /**
  103        * @return a string for the sampleResult Title
  104        */
  105       private String getTitle() {
  106           return this.getName();
  107       }
  108   
  109       /**
  110        * @return the data for the sample
  111        */
  112       public String getData() {
  113           return getPropertyAsString(DATA);
  114       }
  115   
  116       /*
  117        * Helper method
  118        */
  119       private void trace(String s) {
  120           String tl = getTitle();
  121           String tn = Thread.currentThread().getName();
  122           String th = this.toString();
  123           log.debug(tn + " (" + classCount + ") " + tl + " " + s + " " + th);
  124       }
  125   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.examples.sampler » [javadoc | source]