0
0
Testing Fundamentalstesting~15 mins

Performance testing basics in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Measure website response time under load
Preconditions (2)
Step 1: Open the performance testing tool
Step 2: Create a test plan with 50 virtual users
Step 3: Set the test to send requests to the website homepage URL
Step 4: Run the test for 5 minutes
Step 5: Monitor the response times and error rates during the test
✅ Expected Result: The average response time should be under 2 seconds and error rate should be 0%
Automation Requirements - Apache JMeter
Assertions Needed:
Verify average response time is less than 2000 milliseconds
Verify error rate is zero
Best Practices:
Use realistic number of virtual users to simulate load
Add assertions to check response time and error rate
Use timers to simulate real user think time
Run tests for sufficient duration to get stable results
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 4 Pitfalls
Using too few virtual users that do not simulate real load
Not adding assertions to check response time and errors
Running tests for too short a time
Hardcoding URLs without using variables
Bonus Challenge

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

Show Hint