Obfuscate or Secure Jmeter Script - performance

I am working on Jmeter Scripts from sometime now, there is a need to secure the Jmeter script and majorly make it unreadable for external stakeholders. My expectation is to obfuscate or deliver the script as some kind of JAR or executable. I need some ideas or workaround to start with.
Thanks
Senz79

It is possible to run existing JMeter script from Java code or create a JMeter test purely in Java using JMeter API so it is not a problem to create an executable binary which will run your test and obfuscate it.
Example Java code to run a JMeter test:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
public class JMeterFromCode {
public static void main(String[] argv) throws Exception {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties("/tmp/jmeter/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/tmp/jmeter");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
HashTree testPlanTree = SaveService.loadTree(new File("/tmp/jmeter/test.jmx"));
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
ResultCollector logger = new ResultCollector(summer);
logger.setFilename("/tmp/jmeter/test.jtl");
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
See the following reference material to get started:
Five Ways To Launch a JMeter Test without Using the JMeter GUI
Bytecode obfuscation

Related

Localhost connection refused when integrating WireMock and JMeter

I'm trying to integrate Wiremock into a Jmeter test plan so that every time I execute the test plan it will start up an instance of WireMock in the beginning and then run tests I have outlined. I followed this answer (https://stackoverflow.com/a/49130518/12912945) but the problem I am having is that I always get the error:
Response message:Non HTTP response message: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
From what I can see, the Wiremock server never starts even though I have the following code in a JSR223 Sampler at the beginning of the test plan:
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
public class WireMockTest {
public static void main(String[] args) {
WireMockServer wireMockServer = new WireMockServer();
configureFor("127.0.0.1", 8080);
wireMockServer.start();
StubMapping foo = stubFor(get(urlEqualTo("/some/thing"))
.willReturn(aResponse()
.withStatus(200)
.withBody("Hello World")));
wireMockServer.addStubMapping(foo);
}
}
Could anyone point me in the right direction of how to correctly integrate the two, I have tried adding to the classpath but I feel like I have not done this correctly or I'm missing something
Thank you!
You're defining the main function but I failed to see where you're executing it. In other words your Wiremock initialization code isn't getting executed at all.
You need to explicitly call this main function in order to get your code executed, to get this done add the next line to the end of your script:
WireMockTest.main()
Once done the JSR223 Sampler will invoke the code inside the main function and the Wiremock server will be started.
Another option is to remove these class and function declarations and just use
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
WireMockServer wireMockServer = new WireMockServer();
configureFor("127.0.0.1", 8080);
wireMockServer.start();
StubMapping foo = stubFor(get(urlEqualTo("/some/thing"))
.willReturn(aResponse()
.withStatus(200)
.withBody("Hello World")));
wireMockServer.addStubMapping(foo);
as the script you define in the JSR223 Test elements don't require an entry point
Check out Apache Groovy - Why and How You Should Use It article for more information about Groovy scripting in JMeter tests.

How to programmatically run a jMeter benchmark from code?

I want to benchmark some remote API calls via code. I've been using JMH for it so far, but it doesnt quite fit my need as a stress test tool (JMH works well for micro benchmarking, where the snippet being benchmarks rusn really really fast). My remote APIs respond in tens of seconds, so I really need to run the tests with:
Multiple parameterized inputs
Multiple client threads (controlling the load)
I'm able achieve a lot via manual tests in JMeter UI, but I would like to write some java tests/benchmarks that use JMeter and does the same. Is there a way to do that?
You can either kick off an existing JMeter test or create a new one using JMeter API
Run existing test example code:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
public class RunExistingJMeterTest {
public static void main(String[] args) throws Exception {
// Initialize properties
JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize logging, locale, etc.
JMeterUtils.setJMeterHome("/path/to/your/jmeter");
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
HashTree testPlanTree = SaveService.loadTree(new File("/path/to/your/jmeter/extras/Test.jmx"));
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
String logFile = "/path/to/results/file.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
Create JMeter script programmatically:
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.FileOutputStream;
public class CreateNewJMeterTest {
public static void main(String[] args) throws Exception {
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome("/path/to/your/jmeter/installation");
JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
JMeterUtils.initLocale();
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// First HTTP Sampler - open example.com
HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
examplecomSampler.setDomain("example.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("Open example.com");
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(examplecomSampler);
// save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream("/path/to/test.jmx"));
//add Summarizer output to get test progress in stdout like:
// summary = 2 in 1.3s = 1.5/s Avg: 631 Min: 290 Max: 973 Err: 0 (0.00%)
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
String logFile = "/path/to/test/results.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
System.exit(0);
}
}
Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information.

Concurrent AJAX calls in Jmeter

How can we trigger concurrent requests in JMeter...
I am not talking about the non -html elements (where we have a provision in jmeter to download them concurrently).
I have few AJAX calls to be downloaded concurrently...
As per JMeter 3.0 there is no suitable Test Elements, you will need to:
go for JSR223 Sampler + Groovy language and Apache HTTP Components (which are parts of JMeter anyway)
or develop your custom sampler to overcome JMeter thread group limitation and kick off extra threads to execute AJAX calls.
Example JSR223 Sampler Code:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; // necessary imports
List<String> urls = new ArrayList<String>(); // initialize array of URLs
Collections.addAll(urls,args); // read URLs from "Parameters" input and add them to array
ExecutorService pool = Executors.newFixedThreadPool(urls.size()); // initialize pool of Future Tasks with number of threads equal to size of URLs provided
for (String url : urls) { // for each URL from list
final String currentURL = url;
pool.submit(new Runnable() { // Sumbit a new thread which will execute GET request
#Override
public void run() {
try {
HttpClient client = new DefaultHttpClient(); // Use Apache Commons HTTPClient to perform GET request
HttpGet get = new HttpGet(currentURL);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
pool.shutdown(); // shut down thread pool
More details: How to Load Test AJAX/XHR Enabled Sites With JMeter

Run jmx file using java

I created a jmx file using java code. But when i tried to execute the jmx file using java, it throws the exception. Pls help me.. I have added all the jars.
(Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'/home/ksahu/MyScreenshots/k.jmx', conversion error com.thoughtworks.xstream.converters.ConversionException: null : null)
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.FileInputStream;
public class RunJMXfile {
public static void main(String[] argv) throws Exception {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties("/home/ksahu/apache-jmeter-2.13/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/home/ksahu/apache-jmeter-2.13");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
FileInputStream in = new FileInputStream("/home/ksahu/MyScreenshots/k.jmx");
HashTree testPlanTree = SaveService.loadTree(in);
in.close();
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
This is the code that i have used to generate the jmx file
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class jmeterTesting {
public static void main(String[] args) throws FileNotFoundException, IOException{
// Engine
StandardJMeterEngine jm = new StandardJMeterEngine();
JMeterUtils.setJMeterHome("/home/ksahu/apache-jmeter-2.13");
// jmeter.properties
JMeterUtils.loadJMeterProperties("/home/ksahu/apache-jmeter-2.13/bin/jmeter.properties");
HashTree hashTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("www.google.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
// Loop Controller
TestElement loopCtrl = new LoopController();
((LoopController)loopCtrl).setLoops(1);
((LoopController)loopCtrl).addTestElement(httpSampler);
((LoopController)loopCtrl).setFirst(true);
// Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController((LoopController)loopCtrl);
// Test plan
TestPlan testPlan = new TestPlan("MY TEST PLAN");
hashTree.add("testPlan", testPlan);
hashTree.add("loopCtrl", loopCtrl);
hashTree.add("threadGroup", threadGroup);
hashTree.add("httpSampler", httpSampler);
jm.configure(hashTree);
jm.run();
System.out.println(hashTree);
SaveService.saveTree(hashTree,new FileOutputStream("/home/ksahu/MyScreenshots/k.jmx"));
}
}
Try to open your /home/ksahu/MyScreenshots/k.jmx in JMeter GUI. If it does not open - there is a problem with the code, you generated the JMX file with. In that case update your question with the code, you used to create the k.jmx file.
See Chapter 4. RUN A JMETER TEST THROUGH A PROGRAM (FROM JAVA CODE) of the Five Ways To Launch a JMeter Test without Using the JMeter GUI for details.
Also there is a sample project which you can use as a reference: https://bitbucket.org/blazemeter/jmeter-from-code/
You need to change the text "org.apache.jorphan.collections.HashTree" in the JMX generated using java to "hashTree". Open the JMX in any text editor and do the replace as mentioned. If that doesn't suffice, include the below step.
You need to set TestElement.ENABLED, TestElement.TEST_CLASS and TestElement.GUI_CLASS explicitly for each element. For example a sampler can be defined as below.
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setDomain(DOMAIN);
httpSampler.setPort(PORT);
httpSampler.setPath(PATH);
httpSampler.setMethod(METHOD);
httpSampler.addArgument("", "${domain}");
httpSampler.setProperty(TestElement.ENABLED, true);
httpSampler.setResponseTimeout("20000");
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler .setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

JMeter not showing Sampler JAR

Moderators, please read through once before marking it as a duplicate.
This is my first time creating a custom Sampler in JMeter. I am trying a simple query.
public class CustomJavaSampler extends AbstractJavaSamplerClient implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5838315675279087366L;
#Override
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult result = new SampleResult();
result.sampleStart(); // start stopwatch
JMeterVariables vars = JMeterContextService.getContext().getVariables();
vars.putObject("search", "DoASearch");
JMeterContextService.getContext().setVariables(vars);
result.sampleEnd(); // stop stopwatch
result.setSuccessful(true);
result.setResponseMessage("Successfull");
result.setResponseCodeOK(); // 200 code
return result;
}
}
I have created a jar and I've added it to the lib/ext folder in jmeter. But I am not able to see the class in Java Request sampler.
I have enabled debug logs.
log_level.jmeter=DEBUG
log_level.jmeter.junit=DEBUG
log_level.jmeter.control=DEBUG
log_level.jmeter.testbeans=DEBUG
log_level.jmeter.engine=DEBUG
log_level.jmeter.threads=DEBUG
log_level.jmeter.gui=WARN
log_level.jmeter.testelement=DEBUG
log_level.jmeter.util=DEBUG
log_level.jmeter.util.classfinder=DEBUG
log_level.jmeter.test=DEBUG
log_level.jmeter.protocol.http=DEBUG
# For CookieManager, AuthManager etc:
log_level.jmeter.protocol.http.control=DEBUG
log_level.jmeter.protocol.ftp=WARN
log_level.jmeter.protocol.jdbc=DEBUG
log_level.jmeter.protocol.java=WARN
log_level.jmeter.testelements.property=DEBUG
log_level.jorphan=DEBUG
log_file=jmeter-debug.log
The complete jmeter log is http://pastebin.com/T39iUhFW
And also why is the log showing a "did not find" message for the /lib jars?
Nothing is wrong with your code, CustomJavaSampler is displayed in Java Request classnames.
So perhaps it's something with your .jar file. Can you upload it somewhere so we could take a look? I guess it's samp.jar.
By the way, setting a variable is a very "light" operation which can be done via Beanshell Sampler which is almost Java compatible.
If you need to perform some load - next option is JSR223 Sampler and "groovy" which is a "balance" between simplicity and productivity so if you will need to make some changes in your code you won't have to recompile, replace jars, restart JMeter, etc.

Resources