# Jmeter

### Conditions <a href="#conditions" id="conditions"></a>

```
${JMeterThread.last_sample_ok}
```

whether or not the last sample was OK - true/false. Note: this is updated after PostProcessors and Assertions have been run. [1](https://www.null-pointer.co.uk/jmeter.html#fn_1)

```
${__javaScript("${remaining}"!="0")}
```

### Regular Expression Extractor <a href="#regular-expression-extractor" id="regular-expression-extractor"></a>

#### Json <a href="#json" id="json"></a>

```css
"field" : "(.+?)"
```

#### XML <a href="#xml" id="xml"></a>

```markup
<MyElement myAttribute="(.+?)"/>
```

### dynamic filenames for results <a href="#dynamic-filenames-for-results" id="dynamic-filenames-for-results"></a>

```
results/${__time(yyyy-MM-dd)}/${myProp}/run_${__time(HHmm)}.jtl
```

### Stopping at any point <a href="#stopping-at-any-point" id="stopping-at-any-point"></a>

Use **Test Action** Target: All Threads Action: Stop Now

### Debugging <a href="#debugging" id="debugging"></a>

Use **Http sampler** Name: ${var\_to\_output} server name: [www.example.com](http://www.example.com)

### BeanShell <a href="#beanshell" id="beanshell"></a>

#### Passing details from one thread group to another <a href="#passing-details-from-one-thread-group-to-another" id="passing-details-from-one-thread-group-to-another"></a>

BeanShell Assertion

```
${__setProperty( propertyInNewThreadGroup, ${varInThisThreadGroup )};
```

Using this property

```
${__property(propertyInNewThreadGroup)}
```

#### General beanShell tips <a href="#general-beanshell-tips" id="general-beanshell-tips"></a>

```
var myVar = vars.get("myJmeterVar")
vars.put("booleanVar", "true")
vars.put("intVar", Integer.toString( 3 ));
```

### Creating your own plug-in <a href="#creating-your-own-plug-in" id="creating-your-own-plug-in"></a>

This show an example Jmeter sampler plugin

Add the jar to you jmeter /lib/ext folder

```
package paulkane.jmeter.myplugin;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testbeans.TestBean;

import java.io.UnsupportedEncodingException;
import java.time.Clock;

public class MyPlugin extends AbstractSampler implements TestBean {
    private static final String UTF_8 = "UTF-8";

    private String fieldOne;
    private String fieldTwo;

    @Override
    public SampleResult sample(Entry entry) {
        SampleResult sampleResult = new SampleResult();
        sampleResult.setSampleLabel(getName());
        //This displays as the Request
        sampleResult.setSamplerData(asJson(getFieldOne(), getFieldTwo());
        sampleResult.sampleStart();

        try {
            String result = performActionOn(getFieldOne(), getFieldTwo());

            sampleResult.setSuccessful(true);
            //This displays as the Response data
            sampleResult.setResponseData(asJson(result), UTF_8);
        } catch (UnsupportedEncodingException e) {
            sampleResult.setResponseData(e.getMessage(), UTF_8);
            sampleResult.setSuccessful(false);
        }

        sampleResult.setDataType(SampleResult.TEXT);
        sampleResult.sampleEnd();

        return sampleResult;
    }

    private String asJson(String fieldOne, String fieldTwo) {
        return String.format("{\n" +
            "  \"fieldOne\" : \"%s\",\n" +
            "  \"fieldTwo\" : \"%s\"\n" +
            "}", fieldOne, fieldTwo);
    }

    private String asJson(String result) {
        //outputting as json allows you to use Regular expression extractor on the result
        return String.format("{\n" +
            "  \"result\" : \"%s\"\n" +
            "}", result);
    }

    public String getFieldOne() {
        return fieldOne;
    }

    public void setFieldOne(String fieldOne) {
        this.fieldOne = fieldOne;
    }

    public String getFieldTwo() {
        return fieldTwo;
    }

    public void setFieldTwo(String fieldTwo) {
        this.fieldTwo = fieldTwo;
    }

}
```

> 1\. [20.6 Pre-defined Variables](http://jmeter.apache.org/usermanual/functions.html#predefinedvars)[ ↩](https://www.null-pointer.co.uk/jmeter.html#reffn_1)
