JMeter User Defined Variable for enabling Test Duration - jmeter

I am trying to parameterize the JMeter test so that I can run Load Test, Stress Test as well as Soak Load Test using the same test plan.
In order to do this, I defined the following as User Defined variables (Test Plan -> Add -> Config Element -> User Defined Variables)
numberOfThreads=${__P(numberOfThreads,1)}
rampUp=${__P(rampUp,1)}
loopCount=${__P(loopCount,1)}
schedulerEnabled=${__P(schedulerEnabled,false)}
schedulerDuration=${__P(schedulerDuration,120)}
Now, in the ThreadGroup definition, I am using these variables as
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Test123" enabled="true">
<stringProp name="TestPlan.comments">Test for the endpoint GET /test123</stringProp>
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">${loopCount}</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">${numberOfThreads}</stringProp>
<stringProp name="ThreadGroup.ramp_time">${rampUp}</stringProp>
<boolProp name="ThreadGroup.scheduler">{schedulerEnabled}</boolProp>
<stringProp name="ThreadGroup.duration">${schedulerDuration}</stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
But when I run the same and pass in values
numberOfThreads=50
rampUp=10
loopCount=-1
schedulerEnabled=true
schedulerDuration=30
(using jmeter-maven-plugin)
the first ThreadGroup keeps creating infinite number of threads instead of shutting down the thread groups after 30 seconds.
PS: 30 is an example. When executing Soak Load Test, I will be setting a higher value to the same.
PS: I noticed that LoopController.continue_forever is set to 'false' immaterial whether for Loop Count I check "Infinite" or provide the value 1. When in UI, I check "Infinite", the value of LoopController.loops is set to -1.
jmeter-maven-plugin: 2.7.0
JMeter Version: 5.1.1

I don't think this is the correct way to enable/disable the thread lifetime setting:
<boolProp name="ThreadGroup.scheduler">{schedulerEnabled}</boolProp>
Instead of trying to enable/disable the thread lifetime setting you could rather play with this schedulerDuration setting itself, for example if you plan to run fixed amount of loops - set the duration to something very big, the maximum value is 9223372036854775807
Also it should be possible to use Runtime Controller
If ability to enable/disable the scheduler is something you really need, you will need to amend it in the .jmx script beforehand somehow, i.e. using sed editor. Also Taurus framework has possibility to amend all the properties of all the test elements using simple YAML syntax

Related

How to modify the image in the JMeter script?

I'm uploading the image with the following JMeter script. But we have the requirement to modify the image for every request so that virus scanner scans it every time. Can anyone please suggest how to modify the image? Thanks.
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<elementProp name="HTTPsampler.Files" elementType="HTTPFileArgs">
<collectionProp name="HTTPFileArgs.files">
<elementProp name="Sample.JPG" elementType="HTTPFileArg">
<stringProp name="File.path">Sample.JPG</stringProp>
<stringProp name="File.paramname">File</stringProp>
<stringProp name="File.mimetype">image/jpeg</stringProp>
</elementProp>
</collectionProp>
</elementProp>
You can use ImageIO class in order to add something to the original image
Add JSR223 PreProcessor as a child of the HTTP Request sampler which performs the image upload
Put the following code into "Script" area:
def image = javax.imageio.ImageIO.read(new File('Sample.jpg'))
def graphics = image.getGraphics()
graphics.setFont(graphics.getFont().deriveFont(16f))
graphics.drawString('User ' + ctx.getThreadNum() + '; iteration: ' + ctx.getVariables().getIteration(), 50, 50)
graphics.dispose()
javax.imageio.ImageIO.write(image, "jpg", new File("Sample.jpg"))
That's it, when you run your test the above code will add current user and iteration text to the upper-left corner of the image (the font color is white so if your image is white you won't be able to see it using your eyes)
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It
If any concurrency is assumed - consider making the copy of the image and updating the filename in the HTTP Request sampler otherwise you will run into race condition when multiple threads will be simultaneously writing into the same file so the image will get corrupt.

how can we know, by what user the sql is executed in jmeter

I'm executing the sqls with different jdbc-connection strings. Each sql will run on respective database-user. Sqls are executing fine. But, I need confirmation like, the sql is executed with the user mentioned in connection-string. Is there any option which will emit the user involved to execute the sql to jtl ?
We can make it possible by . Parse the username using regex and read the variable through BeanShellPostprocessor and put same to the jtl.
<hashTree /><RegexExtractor enabled="true" guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="Regular Expression Extractor">
<stringProp name="RegexExtractor.useHeaders">true</stringProp>
<stringProp name="RegexExtractor.refname">UserName</stringProp>
<stringProp name="RegexExtractor.regex">UserName=(\w+), </stringProp>
<stringProp name="RegexExtractor.template">$1$</stringProp>
<stringProp name="RegexExtractor.default" />
<stringProp name="RegexExtractor.match_number" />
</RegexExtractor><hashTree />
In Beanshell postprocessor read it:
String db_user = vars.get("UserName");
vars.put("db_user", db_user);
Note: Also, need to mention the variable name used here at jmeter/bin/jmeter.properties file.
sample_variables=db_user
By above way, we can achieve this.

Get the SSIS Event Name in Script Task

I am wanting to run a standard script task in package level events (OnError, OnPreExecute, OnPostExecute) for logging.
Ideally I would like to be able to just copy and paste the Script task, but it needs to know the Event that it is running inside of. Currently I pass that in as a use variable, but this means that I need to manually set the variable to store the name of the event. That's not too arduous, but it is easily forgotten. Thus my question is, is there a performant way to detect which event the script task is running within.
My suspicion is that this is not easy as essentially this is the same as finding out the name of the parent container, which turns out to be next to impossible.
Another option is to be able to sniff the scope of standard event variables, such as 'EventHandlerStartTime', but I can't find a way to determine the scope of a variable from within the script task.
Any help is much appreciated.
Since #Mark mentioned Biml, I thought I'd take a moment to explode out the concept of using some very basic concepts to make this happen.
Assuming I had a pathetic logging table like
CREATE TABLE
dbo.DoWhat
(
PackageName nvarchar(150) NULL
, ParentContainerGUID varchar(38) NULL
, SourceDescription nvarchar(1000) NULL
, SourceName nvarchar(150) NULL
, SourceParentGUID varchar(38) NULL
, EventName varchar(20) NULL
);
then I could define a "function" in a file called inc_events.biml
What that says, this file expects a parameter of type string that will populate the eventName variable. I then use a classic ASP style syntax for subbing in that value <#=eventName#>
So, this biml will create an Event. That event has a Variable named WhereAmI that is scoped to the Event. I then have an Execute SQL Task in there that does an insert into the logging table passing along System variables and my local variable name.
You're looking to use a Script Task so you'd replace the Execute SQL Task but the concept is the same. There's a something in there that's going to use our local variable that had the event's name assigned to it.
<## property name="eventName" type="String" #>
<Event EventType="<#=eventName#>" ConstraintMode="Linear" Name="<#=eventName#>">
<Variables>
<Variable DataType="String" Name="WhereAmI"><#=eventName#></Variable>
</Variables>
<Tasks>
<ExecuteSQL ConnectionName="tempdb" Name="SQL Log notes">
<VariableInput VariableName="User.QueryLog"></VariableInput>
<Parameters>
<Parameter DataType="String" VariableName="System.PackageName" Name="0" />
<Parameter DataType="AnsiString" VariableName="System.ParentContainerGUID" Name="1" DataTypeCodeOverride="129" />
<Parameter DataType="String" VariableName="System.SourceDescription" Name="2" />
<Parameter DataType="String" VariableName="System.SourceName" Name="3" />
<Parameter DataType="AnsiString" VariableName="System.SourceParentGUID" Name="4" DataTypeCodeOverride="129" />
<Parameter DataType="AnsiString" VariableName="User.WhereAmI" Name="5" DataTypeCodeOverride="129" />
</Parameters>
</ExecuteSQL>
</Tasks>
</Event>
Using the include file
I add a second Biml file, so_27378254_inc.biml to my project. This one is going to demonstrate how we use the function/include file.
I define an OLE DB database connection in Connections collection called tempdb
In my Packages collection, I define a new package called so_27378254_inc. It has a Variable, QueryLog which just contains the format for an insert statement into my log table.
In the package's Events collection, I then invoke CallBimlScript 3 times, one for each Event Handler I want to add at the package level scope.
Into the Tasks collection, I add a Sequence Container just so I can get some variety in my log.
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Connections>
<OleDbConnection Name="tempdb" ConnectionString="Data Source=localhost\dev2014;Initial Catalog=tempdb;Provider=SQLNCLI10.1;Integrated Security=SSPI;" />
</Connections>
<Packages>
<Package ConstraintMode="Linear" Name="so_27378254_inc">
<Variables>
<Variable DataType="String" Name="QueryLog">
<![CDATA[INSERT INTO
dbo.DoWhat
(
PackageName
, ParentContainerGUID
, SourceDescription
, SourceName
, SourceParentGUID
, EventName
)
SELECT
? /* AS PackageName */
, ? /* AS ParentContainerGUID */
, ? /* AS SourceDescription */
, ? /* AS SourceName */
, ? /* AS SourceParentGUID */
, ? /* AS EventName */
;]]>
</Variable>
</Variables>
<Events>
<#=CallBimlScript("inc_events.biml", "OnError")#>
<#=CallBimlScript("inc_events.biml", "OnPreExecute")#>
<#=CallBimlScript("inc_events.biml", "OnPostExecute")#>
</Events>
<Tasks>
<Container Name="SEQC Container on Control Flow" ConstraintMode="Linear">
</Container>
</Tasks>
</Package>
</Packages>
</Biml>
So, what do you do with it? You right click on the so_27378254_inc.biml file and whoosh, a new package is generated.
How the heck do I sell this?
You mention in your comments, and rightfully so, concerns over anything that would "add friction to adoption".
My counter to that is
BIDS Helper is free and if you're not using it, you're not being as effective in your delivery as you could be. Especially pre-2012, there's just too many sharp edges in the product.
Using an approach like this will get everyone to a consistent starting point, without some of the hassles that come with template packages (IDs are not automatically updated until 2012)
You don't have to go all in on the Biml approach. Assuming you're the architect/lead developer, use it to define best practices. Everyone has logging turned on, apply configurations, get the standard connections defined, etc. All people have to do is instead of creating new SSIS, they right click and generate the template package and then rename it to something appropriate and they're off the races worrying about data flows and file acquisition, etc.

Trying to generate JMeter Test Plan (jmx) With JMeter API : Not able to save CSVDataSet element

I am creating a JMeter jmx file dynamically by using JMeter APIs. I am able to add a ThreadGroup within a TestPlan and a JavaSampler within the ThreadGroup. But when I add a CSVDataSet element within the Java Sampler, it does not get saved properly.
The following code is used to create a new CSVDataSet element
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setName("CSV Data Set");
csvDataSet.setComment("Sample CSV Data Set");
csvDataSet.setDelimiter(",");
csvDataSet.setFileEncoding("");
csvDataSet.setFilename("d:\\jmeter\\data.csv"); // variable
csvDataSet.setQuotedData(true);
csvDataSet.setRecycle(true);
csvDataSet.setShareMode(shareMode.all);
csvDataSet.setStopThread(false);
csvDataSet.setVariableNames("firstname, lastname, email"); // variable
csvDataSet.setEnabled(true);
When this is saved using SaveService.saveTree, the final jmx does not contain all the values which were set.
<org.apache.jorphan.collections.HashTree>
<CSVDataSet testname="CSV Data Set Config" enabled="true">
<stringProp name="TestPlan.comments">Sample CSV Data Set Config</stringProp>
</CSVDataSet>
<org.apache.jorphan.collections.HashTree/>
As seen above, only the test name, enabled, and comments are added. The rest of the variables are completely ignored.
Is there something that needs to be set in order to get all the values as expected?
or is this a bug in JMeter? I am using version 2.11
The basic code is as per section 4.3 from following link
http://blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui
To that I add the code shown above. The way it is added is,
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
testPlanTree.add("csvDataSet", csvDataSet);
SaveService
.saveTree(testPlanTree, new FileOutputStream("d:\\test.jmx"));
output of CSVDataSet block is as shown above.
After looking into the JMeter source code, it seems all the properties are set using the setProperty function rather than the individual setter functions. So putting the following code does the job of creating the CSVDataSet element properly.
csvDataSet.setProperty("delimiter", ",");
csvDataSet.setProperty("fileEncoding", "");
csvDataSet.setProperty("filename", "d:\\data.csv");
csvDataSet.setProperty("quotedData", true);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("shareMode", "shareMode.all");
csvDataSet.setProperty("stopThread", false);
csvDataSet.setProperty("variableNames", "var1, var2, var3");
Not sure why setters are not used in the code, but this seems to be the way to go for now
It is clearly not a bug in JMeter otherwise CSV Data Set could not be saved.
It is probably an issue in the way you build the HashTree, but unless you show the full code, you cannot get help.
By the way, as I said in a previous answer, what you are trying to do to build different tests based on input parameter is not good idea IMHO, the approach will be very fragile towards upcoming versions of JMeter.
JMeter provides ways to do it that you should follow.

XACML policy containing XPath expression does not work when published in WSO2 Identity Server 5.0

I tried the first sample from http://www.webfarmr.eu/2011/08/xacml-102-xpath-and-xacml/, section "XPath used in a single XACML request". Just in case here is the XACML policy:
<?xml version="1.0" encoding="UTF-8"?><xacml3:Policy xmlns:xacml3="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" PolicyId="xpath-target-single-req" RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides" Version="1">
<xacml3:Description/>
<xacml3:PolicyDefaults><xacml3:XPathVersion>http://www.w3.org/TR/1999/REC-xpath-19991116</xacml3:XPathVersion></xacml3:PolicyDefaults>
<xacml3:Target>
<xacml3:AnyOf>
<xacml3:AllOf>
<xacml3:Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-equal">
<xacml3:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Gulliver's travels</xacml3:AttributeValue>
<xacml3:AttributeSelector Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" Path="/book/title/text()"/>
</xacml3:Match>
<xacml3:Match MatchId="urn:oasis:names:tc:xacml:1.0:function:integer-greater-than">
<xacml3:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">18</xacml3:AttributeValue>
<xacml3:AttributeDesignator AttributeId="age" Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" DataType="http://www.w3.org/2001/XMLSchema#integer" MustBePresent="false"/>
</xacml3:Match>
</xacml3:AllOf>
</xacml3:AnyOf>
</xacml3:Target>
<xacml3:Rule Effect="Permit" RuleId="allow-read">
<xacml3:Description/>
<xacml3:Target/>
</xacml3:Rule>
</xacml3:Policy>
When I use the "Try" option from the Policy Administration page and evaluate my XACML request - the response returns the Permit decision. Notice that the policy has not been published yet.
After that I publish the policy, enable it and use the "Try" option from the Tools->XACML menu. Result is "NotApplicable".
I have no any other policies neither in the Policy Administration nor in Policy View.
Do XACML policies that contain XPath expressions need some additional configuration? What is wrong with the provided scenario?
EDIT:
Found this message in logs:
[2014-07-03 11:13:25,021] INFO {org.wso2.balana.finder.AttributeFinder} - Failed to resolve any values for /book/title/text()
Yes. This is a bug in the Identity Server. Please refer to the public jira. However, This is not because that XPAth is not implemented in Balana and not an issue with Balana. It is related to Identity Server and Identity Server is not properly initializing the Balana engine.
You can even find the fix that is attached to public jira and you can try with that fix. You can find more details about XPath example with Identity Server from here
When I wrote the example, I overlooked adding namespaces. You need to add namespaces and then it should work fine. For instance, the XACML policy becomes:
<xacml3:Policy xmlns:xacml3="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" PolicyId="xpath-target-single-req" Version="1" RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides">
<xacml3:PolicyDefaults><xacml3:XPathVersion>http://www.w3.org/TR/1999/REC-xpath-19991116</xacml3:XPathVersion></xacml3:PolicyDefaults>
<xacml3:Target>
<xacml3:AnyOf>
<xacml3:AllOf>
<xacml3:Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-equal">
<xacml3:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Gulliver&apos;s travels</xacml3:AttributeValue>
<xacml3:AttributeSelector Path="/ns1:book/ns1:title/text()" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" xmlns:ns1="http://example.com"/>
</xacml3:Match>
<xacml3:Match MatchId="urn:oasis:names:tc:xacml:1.0:function:integer-greater-than">
<xacml3:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">18</xacml3:AttributeValue>
<xacml3:AttributeDesignator Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" AttributeId="age" DataType="http://www.w3.org/2001/XMLSchema#integer" MustBePresent="false"/>
</xacml3:Match>
</xacml3:AllOf>
</xacml3:AnyOf>
</xacml3:Target>
<xacml3:Rule RuleId="allow-read" Effect="Permit">
<xacml3:Target/>
</xacml3:Rule>
</xacml3:Policy>
And the XACML request becomes
<xacml-ctx:Request ReturnPolicyIdList="true" CombinedDecision="false" xmlns:xacml-ctx="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
<xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" >
<xacml-ctx:Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id" IncludeInResult="true">
<xacml-ctx:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Joe</xacml-ctx:AttributeValue>
</xacml-ctx:Attribute>
<xacml-ctx:Attribute AttributeId="age" IncludeInResult="true">
<xacml-ctx:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">14</xacml-ctx:AttributeValue>
</xacml-ctx:Attribute>
</xacml-ctx:Attributes>
<xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" >
<xacml-ctx:Content><book xmlns="http://example.com">
<title>Gulliver's travels</title>
<isbn>xx-yy-zz</isbn>
<publisher>Axiomatics</publisher>
</book> </xacml-ctx:Content>
</xacml-ctx:Attributes>
<xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" >
</xacml-ctx:Attributes>
<xacml-ctx:Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" >
</xacml-ctx:Attributes>
</xacml-ctx:Request>
Note that I added a namespace definition (ns1, http://example.com). It is important to fully qualify your XPath expression.
If it still doesn't work for you, it may be Balana (WSO2IS) doesn't fully implement attribute selectors. I tested it in the Axiomatics Policy Server and it works fine.
HTH,
David.

Resources