I'm new to Jmeter and vigorously learning.
I wanted to know how JSR223 Sampler and JSR223 PreProcessor are different, can a sampler be used the same way as the preprocessor?
General difference is that PreProcessor won't be executed unless it has a sampler in its scope which it will be triggered (per sampler)
Pre-Processor element is defined to alter the settings of Samplers in their scope. It will always execute before the actual sampler request.
Specific difference is that JSR223 PreProcessor doesn't have SampleResult
available, so for example you can't execute the following example:
SampleResult.setStopTest(true);
The
SampleResult
ResponseData is set from the return value of the script. If the script returns null, it can set the response directly, by using the methodSampleResult.setResponseData(data)
, where data is either a String or a byte array. The data type defaults to "text", but can be set to binary by using the methodSampleResult.setDataType(SampleResult.BINARY)
.The SampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods
setStopThread(boolean)
andsetStopTest(boolean)
.
JSR223 Sampler is a Sampler, so it will generate a SampleResult which will appear in the Test Results (unless you call SampleResult.setIgnore() method)
JSR223 PreProcessor cannot be executed per se, you need to connect it to one (or many) Samplers according to JMeter Scoping Rules so it will be executed before one (or many) samplers. PreProcessors execution time is not reflected in the test results (unless you use a Transaction Controller configured to include it)
Both can run an arbitrary code (it's recommended to stick to Groovy) so which one is to use mostly depends on your use case, if you need to conduct the load and measure the time - go for the Sampler, if you need to set up some data - go for the PreProcessor, etc.