Exemple #1
0
  /**
   * Perform a single sample.<br>
   * In this case, this method will simply sleep for some amount of time.
   *
   * <p>This method returns a <code>SampleResult</code> object.
   *
   * <pre>
   *
   *  The following fields are always set:
   *  - responseCode (default &quot;&quot;)
   *  - responseMessage (default &quot;&quot;)
   *  - label (set from LABEL_NAME parameter if it exists, else element name)
   *  - success (default true)
   *
   * </pre>
   *
   * The following fields are set from the user-defined parameters, if supplied:
   *
   * <pre>
   * -samplerData - responseData
   * </pre>
   *
   * @see org.apache.jmeter.samplers.SampleResult#sampleStart()
   * @see org.apache.jmeter.samplers.SampleResult#sampleEnd()
   * @see org.apache.jmeter.samplers.SampleResult#setSuccessful(boolean)
   * @see org.apache.jmeter.samplers.SampleResult#setSampleLabel(String)
   * @see org.apache.jmeter.samplers.SampleResult#setResponseCode(String)
   * @see org.apache.jmeter.samplers.SampleResult#setResponseMessage(String)
   * @see org.apache.jmeter.samplers.SampleResult#setResponseData(byte [])
   * @see org.apache.jmeter.samplers.SampleResult#setDataType(String)
   * @param context the context to run with. This provides access to initialization parameters.
   * @return a SampleResult giving the results of this sample.
   */
  @Override
  public SampleResult runTest(JavaSamplerContext context) {
    setupValues(context);

    SampleResult results = new SampleResult();

    results.setResponseCode(responseCode);
    results.setResponseMessage(responseMessage);
    results.setSampleLabel(label);

    if (samplerData != null && samplerData.length() > 0) {
      results.setSamplerData(samplerData);
    }

    if (resultData != null && resultData.length() > 0) {
      results.setResponseData(resultData, null);
      results.setDataType(SampleResult.TEXT);
    }

    // Record sample start time.
    results.sampleStart();

    long sleep = sleepTime;
    if (sleepTime > 0 && sleepMask > 0) { // / Only do the calculation if
      // it is needed
      long start = System.currentTimeMillis();
      // Generate a random-ish offset value using the current time.
      sleep = sleepTime + (start % sleepMask);
    }

    try {
      // Execute the sample. In this case sleep for the
      // specified time, if any
      if (sleep > 0) {
        TimeUnit.MILLISECONDS.sleep(sleep);
      }
      results.setSuccessful(success);
    } catch (InterruptedException e) {
      LOG.warn("JavaTest: interrupted.");
      results.setSuccessful(true);
    } catch (Exception e) {
      LOG.error("JavaTest: error during sample", e);
      results.setSuccessful(false);
    } finally {
      // Record end time and populate the results.
      results.sampleEnd();
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug(whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime());
      listParameters(context);
    }

    return results;
  }