Stress testing concepts in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different user credentials to simulate varied login attempts.
Practice
stress testing in software testing?Solution
Step 1: Understand the purpose of stress testing
Stress testing is designed to push a system beyond normal operational capacity to see how it behaves under extreme conditions.Step 2: Compare options with stress testing goal
Only To push the system beyond its normal limits to find breaking points describes pushing the system beyond normal limits to find breaking points, which matches stress testing's main goal.Final Answer:
To push the system beyond its normal limits to find breaking points -> Option DQuick Check:
Stress testing = pushing limits [OK]
- Confusing stress testing with functional testing
- Thinking stress testing checks UI or installation
- Assuming stress testing is about normal load only
Solution
Step 1: Define stress test scenario
A stress test scenario involves applying a load that is higher than the system's maximum expected capacity to observe failure points.Step 2: Match options to definition
Testing the system with a load that exceeds its maximum capacity correctly states testing with load exceeding maximum capacity, which fits stress testing. Other options describe normal load or unrelated tests.Final Answer:
Testing the system with a load that exceeds its maximum capacity -> Option AQuick Check:
Stress test = load beyond max [OK]
- Choosing normal load as stress test
- Confusing stress test with functional or unit tests
- Ignoring the 'exceeds capacity' part
for users in range(1000, 1100):
simulate_login(users)
simulate_data_request(users)
check_response_time(users)What is the main purpose of increasing users from 1000 to 1099 in this test?
Solution
Step 1: Analyze the loop range and actions
The loop increases user count from 1000 to 1099, simulating login and data requests for each user count, then checking response time.Step 2: Understand the test goal
This gradual increase tests how the system handles increasing load, which is typical in stress testing to find breaking points or performance degradation.Final Answer:
To test system behavior under gradually increasing load -> Option CQuick Check:
Increasing users = increasing load [OK]
- Thinking test is only for exactly 1000 users
- Assuming test checks only login feature
- Ignoring the gradual increase aspect
for i in range(1000, 1100):
simulate_login(i)
simulate_data_request(i)
check_response_time()What is the main issue in this code?
Solution
Step 1: Review function calls inside the loop
The loop callssimulate_login(i)andsimulate_data_request(i)with the user parameteri. However,check_response_time()is called without any parameter.Step 2: Identify inconsistency
Since response time likely depends on the user or request, missing the user parameter incheck_response_time()is an error causing incorrect or failed measurement.Final Answer:
Thecheck_response_timefunction is missing the user parameter -> Option AQuick Check:
Missing parameter in function call = bug [OK]
- Ignoring missing parameters in function calls
- Thinking loop range is wrong for stress testing
- Assuming functions should be outside the loop
Solution
Step 1: Understand stress testing for capacity limits
Stress testing involves pushing the system beyond expected limits to find breaking points and observe behavior under extreme load.Step 2: Evaluate options for best stress test design
Gradually increase users from 1000 to 6000 while monitoring system response and failures gradually increases users beyond 5000 to 6000, monitoring response and failures, which aligns with stress testing goals. Other options either test only once, test fewer users, or do unrelated functional tests.Final Answer:
Gradually increase users from 1000 to 6000 while monitoring system response and failures -> Option BQuick Check:
Gradual increase beyond max = proper stress test [OK]
- Testing only once at max users
- Confusing stress test with functional test
- Assuming fewer users prove max capacity
Ready to prove it?
Pass the test and this pattern moves unknown
