0
0
Testing Fundamentalstesting~15 mins

Stress testing concepts in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Stress Test Login Functionality Under High Load
Preconditions (3)
Step 1: Open the login page URL
Step 2: Simulate 1000 users attempting to login simultaneously using valid credentials
Step 3: Monitor the response time and server behavior during the login attempts
Step 4: Record any errors or failures during the login process
✅ Expected Result: The system should handle 1000 simultaneous login requests without crashing, with acceptable response times and no data loss or errors.
Automation Requirements - JMeter
Assertions Needed:
Verify that all login requests return HTTP status 200
Check that average response time is below 5 seconds
Ensure no error responses are returned during the test
Best Practices:
Use thread groups to simulate concurrent users
Add timers to mimic real user think time
Use assertions to validate server responses
Monitor server resource usage during the test
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.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class StressTestLogin {
    public static void main(String[] args) throws Exception {
        // Set JMeter home for JMeter utils
        String jmeterHome = System.getenv("JMETER_HOME");
        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
        JMeterUtils.initLocale();

        StandardJMeterEngine jmeter = new StandardJMeterEngine();

        // Test Plan
        TestPlan testPlan = new TestPlan("Stress Test Login Functionality");

        // HTTP Sampler for login
        HTTPSamplerProxy loginSampler = new HTTPSamplerProxy();
        loginSampler.setDomain("example.com");
        loginSampler.setPort(80);
        loginSampler.setPath("/login");
        loginSampler.setMethod("POST");
        loginSampler.addArgument("username", "testuser");
        loginSampler.addArgument("password", "password123");
        loginSampler.setName("Login Request");
        loginSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        loginSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

        // Thread Group to simulate 1000 users
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("User Threads");
        threadGroup.setNumThreads(1000);
        threadGroup.setRampUp(10); // 10 seconds ramp-up
        threadGroup.setSamplerController(new LoopController() {{
            setLoops(1);
            setFirst(true);
        }});

        // Test Plan tree
        HashTree testPlanTree = new HashTree();
        HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(loginSampler);

        // Result collector
        ResultCollector logger = new ResultCollector();
        SampleSaveConfiguration saveConfig = new SampleSaveConfiguration();
        saveConfig.setTime(true);
        saveConfig.setLatency(true);
        saveConfig.setSuccess(true);
        saveConfig.setResponseCode(true);
        saveConfig.setResponseMessage(true);
        saveConfig.setThreadName(true);
        logger.setSaveConfig(saveConfig);
        testPlanTree.add(testPlanTree.getArray()[0], logger);

        // Run Test Plan
        jmeter.configure(testPlanTree);
        jmeter.run();
    }
}

This script uses Apache JMeter's Java API to create a stress test for the login functionality.

First, it sets up JMeter environment and properties.

Then, it creates a Test Plan and an HTTP Sampler that sends a POST request to the login URL with valid credentials.

A Thread Group simulates 1000 users logging in simultaneously with a 10-second ramp-up to avoid all hitting at once.

The test plan tree organizes these elements, and a Result Collector is added to capture results.

Finally, the test plan is run, which will simulate the load and allow monitoring of response times and errors.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using Thread.sleep() to simulate user wait times instead of JMeter timers', 'why_bad': 'Thread.sleep() pauses the entire test thread and does not simulate real user think time properly, leading to inaccurate load simulation.', 'correct_approach': "Use JMeter's built-in timers like Constant Timer or Uniform Random Timer to simulate realistic user delays."}
Hardcoding user credentials inside the script without parameterization
Not setting ramp-up time, causing all users to hit the server at once
Bonus Challenge

Now add data-driven testing with 3 different user credentials to simulate varied login attempts.

Show Hint