Perform load testing on a web application homepage
Preconditions (2)
✅ Expected Result: The web application should handle 50 users simultaneously with average response time under 2 seconds and error rate below 1%
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.engine.StandardJMeterEngine; import org.apache.jmeter.reporters.ResultCollector; import org.apache.jmeter.samplers.SampleSaveConfiguration; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.threads.ThreadGroup; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jorphan.collections.HashTree; import java.io.FileOutputStream; public class LoadTestExample { public static void main(String[] args) throws Exception { // Initialize JMeter Engine StandardJMeterEngine jmeter = new StandardJMeterEngine(); // Create Test Plan TestPlan testPlan = new TestPlan("Load 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"); // Create Thread Group ThreadGroup threadGroup = new ThreadGroup(); threadGroup.setName("Load Test Users"); threadGroup.setNumThreads(50); // 50 virtual users threadGroup.setRampUp(10); // ramp up in 10 seconds threadGroup.setDuration(300); // 5 minutes // Build Test Plan tree HashTree testPlanTree = new HashTree(); HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup); threadGroupHashTree.add(httpSampler); // Add Listener to collect results SampleSaveConfiguration saveConfig = new SampleSaveConfiguration(); saveConfig.setTime(true); saveConfig.setLatency(true); saveConfig.setSuccess(true); saveConfig.setResponseCode(true); saveConfig.setResponseMessage(true); ResultCollector logger = new ResultCollector(saveConfig); logger.setFilename("load_test_results.jtl"); testPlanTree.add(testPlanTree.getArray()[0], logger); // Configure JMeter jmeter.configure(testPlanTree); // Run Test jmeter.run(); // After test, user can analyze load_test_results.jtl for response times and errors } }
This code sets up a load test using Apache JMeter's Java API.
We create a test plan and add a thread group with 50 virtual users that ramp up over 10 seconds and run for 5 minutes.
The HTTP sampler sends GET requests to the homepage of example.com.
A result collector saves test results to a file for later analysis.
This setup follows best practices by using thread groups for users, configuring duration, and collecting detailed results.
Now add data-driven testing with 3 different URLs to test homepage, login page, and search page
load testing in software testing?load_test.sh in a terminal?bash script.sh runs a shell script.run, start, or execute are not standard shell commands to run scripts.Users simulated: 100 Average response time: 2.5 seconds Error rate: 0%
Pass the test and this pattern moves unknown