Measure website response time under load
Preconditions (2)
✅ Expected Result: The average response time should be under 2 seconds and error rate should be 0%
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.engine.StandardJMeterEngine; import org.apache.jmeter.protocol.http.control.HeaderManager; 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.samplers.SampleSaveConfiguration; 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; public class PerformanceTest { public static void main(String[] args) throws Exception { // Set JMeter home for JMeterUtils String jmeterHome = System.getenv("JMETER_HOME"); JMeterUtils.setJMeterHome(jmeterHome); JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties"); JMeterUtils.initLocale(); StandardJMeterEngine jmeter = new StandardJMeterEngine(); // Create Test Plan TestPlan testPlan = new TestPlan("Performance Test Plan"); // Create HTTP Sampler HTTPSamplerProxy httpSampler = new HTTPSamplerProxy(); httpSampler.setDomain("example.com"); httpSampler.setPort(80); httpSampler.setPath("/"); httpSampler.setMethod("GET"); httpSampler.setName("Homepage Request"); httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName()); // Create Thread Group ThreadGroup threadGroup = new ThreadGroup(); threadGroup.setName("Load Test Users"); threadGroup.setNumThreads(50); threadGroup.setRampUp(10); threadGroup.setDuration(300); // 5 minutes threadGroup.setScheduler(true); threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName()); threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName()); // Create Test Plan tree HashTree testPlanTree = new HashTree(); HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup); threadGroupHashTree.add(httpSampler); // Add Result Collector SampleSaveConfiguration saveConfig = new SampleSaveConfiguration(); saveConfig.setTime(true); saveConfig.setLatency(true); saveConfig.setSuccess(true); saveConfig.setResponseCode(true); saveConfig.setResponseMessage(true); saveConfig.setThreadName(true); saveConfig.setDataType(true); saveConfig.setEncoding(true); saveConfig.setAssertions(true); saveConfig.setSubresults(true); saveConfig.setResponseData(false); saveConfig.setSamplerData(false); saveConfig.setAsXml(false); ResultCollector logger = new ResultCollector(saveConfig); logger.setFilename("performance_test_results.jtl"); testPlanTree.add(testPlanTree.getArray()[0], logger); // Run Test Plan jmeter.configure(testPlanTree); jmeter.run(); // After test, parse results file to assert average response time and error rate // (Parsing and assertions would be done in a separate step or tool) } }
This code sets up a JMeter test plan programmatically.
First, it configures JMeter environment and loads properties.
Then it creates a test plan with a thread group of 50 users ramping up over 10 seconds and running for 5 minutes.
An HTTP sampler sends GET requests to the homepage of example.com.
A result collector saves test results to a file.
The test plan tree is built and the test is run.
Assertions on average response time and error rate should be done by analyzing the saved results file after the test run.
Now add data-driven testing with 3 different URLs to test homepage, login page, and search page
performance testing in software development?Pass the test and this pattern moves unknown