0
0
Testing Fundamentalstesting~15 mins

Load testing concepts in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Perform load testing on a web application homepage
Preconditions (2)
Step 1: Open the load testing tool
Step 2: Create a test plan with 50 virtual users
Step 3: Set the test duration to 5 minutes
Step 4: Configure the test to send HTTP GET requests to http://example.com
Step 5: Start the load test
Step 6: Monitor the response times and error rates during the test
Step 7: Stop the test after 5 minutes
✅ Expected Result: The web application should handle 50 users simultaneously with average response time under 2 seconds and error rate below 1%
Automation Requirements - Apache JMeter
Assertions Needed:
Average response time is less than 2000 milliseconds
Error rate is less than 1%
Best Practices:
Use thread groups to simulate virtual users
Add listeners to collect response time and error data
Use assertions to validate performance criteria
Avoid hardcoding URLs; use variables for flexibility
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 4 Pitfalls
Hardcoding URLs directly inside the test script
Not setting ramp-up time for virtual users
Ignoring error rate and only checking response times
Using too few virtual users for load testing
Bonus Challenge

Now add data-driven testing with 3 different URLs to test homepage, login page, and search page

Show Hint