I've a test plan with TCP sampler with HOST and PORT defined using TCP sampler config.
I have defined the 'port' value for the TCP connection defined in TCP sampler config and I need this value (TCPSampler.port=3001) to be part of request to the server.
I am trying to use beanshell pre-processor to capture and store it on a user defined variable.
Any idea about how to achieve this.
Advanced thanks for the help.
Put the following code into "Text to send" input
TCPSampler.port=${__BeanShell(ctx.getCurrentSampler().getPort();,port)}
Broken down it consists of:
__Beanshell function which allows to execute arbitrary beanshell code in any place of the script
ctx - is the shortcut to the instance of JMeterContext class. See JavaDoc for all available methods
getCurrentSampler() - is aforementioned JMeterContext class method which provides access to the instance of the current sampler
getPort is the method of TCPSampler class as in your case it will be TCP sampler
So __Beanshell function executes the script and saves result into port variable which can be later accessed anywhere in current thread group.
For Beanshell PreProcessor (if you still want to use it) relevant code will look like:
int port = sampler.getPort();
vars.put("port", String.valueOf(port));
See How to use BeanShell: JMeter's favorite built-in component guide for comprehensive information on Beanshell scripting in Apache JMeter.
`
Related
Is there a way to extract/analyse the target URL in a random JMeter test plan provided through a random source?
Also, a stretch goal is to identify the target service platform through this like Azure, AWS etc., is it possible?
Yes, it is, you can use i.e. JSR223 PostProcessor like prev.getUrlAsString()
Theoretically it should be possible to extract the IP address from the URL and check whether it belongs to Azure IP Range or AWS IP range
InetAddress.getByName(prev.getURL().getHost()).getHostAddress()
Demo:
In be above examples prev stands for SampleResult class instance holding the result of the previous sampler execution, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.
I am trying to build a JMeter script that executes one HTTP Request to get a key, it then stores that key.
It then moves into another Threadgroup which loads tests an API but using that stored key, the reason for this is not to request the key hundreds of times as this is not what we are testing.
Any help is hugely appreciated
Use setproperty() in first thread group and save the key as a property and then fetch the key using get property in the second thread group.
${key}- Variable from regular expression holding secret key.
Setproperty Ex:-${__setProperty(sec_key,${key})}...Used JSR2223 Post processor to set property
Getproperty Ex:-${__property(sec_key)}...Use wherever it is required to pass the key
Also, check "Run thread group consecutively" option in Test Plan.
Hope this helps.
You can use one of the following approaches:
In setUp Thread Group you can execute the HTTP request once and save the key into a JMeter Property using __setProperty() function and then access the value via __P() function where required
There is Inter-Thread Communication Plugin which can be used for passing data between threads even if they reside in different Thread Groups. See SynchronizationPluginsExample.jmx test plan for details. You can install Inter-Thread Communication Plugin using JMeter Plugins Manager.
Is there any JMeter plugin available natively to send JMeter results to Splunk tcp forwarder? I looked up in jmeter-plugin's site but cannot find one. One other answer in SO suggested to use beanshell postprocessor to send the result. I am fairly new to Beanshell any help to get this done is much helpful.
Instead of sending data to Splunk from JMeter you could do the opposite thing - configure Splunk's input.conf to read JMeter's .jtl results file. See Monitor files and directories article for more details.
Second viable choice would be sticking to JMeter TCP Sampler, I believe it is easier than using any form of scripting
If you are looking for a ready solution you can consider SendToSplunk program which can be invoked via OS Process Sampler
I need to sign my request xml and add a token to it before hitting the application. For that I am using a JSR223 preprocessor which gets the data from current sampler, makes the necessary changes and puts the data back to sampler. This approach works fine with a single thread group. When multiple thread groups are used data between the samplers(in different thread groups) gets interchanged and it results in incorrect requests for the thread group. Below is the code in my preprocessor:
import jmeter_plugin.*;
String body = sampler.getXmlData();
log.info(body);
Utils utils=new Utils();
String request=utils.getResponse( body,"url/to/fetch/token");
log.info(request);
sampler.setXmlData(request);
Tried by having a single preprocessors for the entire test plan and also separate pre processors for each thread group. Both approaches did not work.
Your approach should work fine no matter how many Thread Groups do you have. Pre-Processors are executed before each sampler in its scope and should modify only the current sampler.
Quick checklist:
Make sure you're using Groovy as JSR223 PreProcessor language
Make sure you have Cache compiled script if available box ticked
Remove these log.info lines as they create unnecessary Disk IO overhead
Make sure your Utils class don't use methods which are not Thread Safe
Looking into sampler.getXmlData() it appears you're using JMeter 3.1 or below as SOAP/XML-RPC Request has been removed in JMeter 3.2 and users are encouraged to use HTTP Request sampler instead.
If your "sign" algorithms is not very exotic if might be easier to use WS Security for SOAP JMeter Plugin which can be installed using JMeter Plugins Manager
I'm writing a BeanShell script in JMeter to process the results of the HTTP Requests. I'm able to use the Post-processor to capture these results and process individually however it means adding a Post-processor to every Request.
Is it possible in the BeanShell listener to access all results rather than just the last one which is accessible via the sampleResult, prev variables?
The BeanShell listener fires per request response so by writing the script for it i was able to run it for each http request. Had tried using the Listener before and it didn't work so I tried the post-processor. However it appears it was an issue in my script causing the problem.