Performance test planning 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.threads.gui.ThreadGroupGui; import org.apache.jmeter.util.JMeterUtils; import org.apache.jorphan.collections.HashTree; public class PerformanceTestPlan { public static void main(String[] args) throws Exception { // Initialize JMeter engine StandardJMeterEngine jmeter = new StandardJMeterEngine(); // JMeter initialization (properties, log levels, locale, etc) JMeterUtils.loadJMeterProperties("/path/to/jmeter.properties"); JMeterUtils.setJMeterHome("/path/to/jmeter/home"); JMeterUtils.initLocale(); // Test Plan TestPlan testPlan = new TestPlan("Login Performance Test Plan"); // HTTP Sampler - Login request HTTPSamplerProxy loginSampler = new HTTPSamplerProxy(); loginSampler.setDomain("example.com"); loginSampler.setPort(80); loginSampler.setPath("/login"); loginSampler.setMethod("POST"); loginSampler.addArgument("username", "user1"); loginSampler.addArgument("password", "pass123"); loginSampler.setName("Login Request"); loginSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); loginSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName()); // Thread Group ThreadGroup threadGroup = new ThreadGroup(); threadGroup.setName("Login Users"); threadGroup.setNumThreads(1000); // 1000 users threadGroup.setRampUp(60); // ramp up in 60 seconds threadGroup.setSamplerController(new LoopController()); threadGroup.getSamplerController().setLoops(1); threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName()); threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName()); // 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); saveConfig.setDataType(true); saveConfig.setEncoding(true); saveConfig.setAssertions(true); saveConfig.setSubresults(true); saveConfig.setResponseData(false); saveConfig.setSamplerData(false); saveConfig.setRequestHeaders(false); saveConfig.setResponseHeaders(false); saveConfig.setAssertionResultsFailureMessage(true); saveConfig.setBytes(true); saveConfig.setSentBytes(true); saveConfig.setUrl(true); saveConfig.setFileName(false); saveConfig.setHostname(true); saveConfig.setThreadCounts(true); saveConfig.setSampleCount(true); saveConfig.setIdleTime(true); saveConfig.setConnectTime(true); logger.setSaveConfig(saveConfig); testPlanTree.add(testPlanTree.getArray()[0], logger); // Run Test Plan jmeter.configure(testPlanTree); jmeter.run(); } }
This code sets up a JMeter test plan programmatically to test the login feature's performance.
First, it initializes JMeter engine and loads properties.
Then, it creates a Test Plan named "Login Performance Test Plan".
An HTTP Sampler is configured to send a POST request to the login URL with username and password parameters.
A Thread Group simulates 1000 users ramping up over 60 seconds, each sending one login request.
The test plan tree organizes these elements.
A Result Collector is added to capture test results with detailed metrics.
Finally, the test plan is executed by the JMeter engine.
This setup follows best practices: parameterization can be added for credentials, assertions can be included in the sampler, and ramp-up simulates real user load.
Now add data-driven testing with 3 different sets of user credentials to simulate multiple users logging in.
Practice
Solution
Step 1: Understand the goal of performance test planning
Performance test planning focuses on preparing tests that measure how fast and stable software is under load.Step 2: Compare options with this goal
Only To organize how to check software speed and stability matches this goal; others relate to development or bug fixing, not planning performance tests.Final Answer:
To organize how to check software speed and stability -> Option AQuick Check:
Performance test planning = Organize speed and stability checks [OK]
- Confusing performance planning with coding or UI design
- Thinking bug fixing is part of test planning
- Mixing test planning with test execution
Solution
Step 1: Identify key steps in performance test planning
Good planning includes setting goals, choosing tools, and scheduling tests.Step 2: Evaluate each option
Only choosing appropriate testing tools is a correct and necessary step; others are mistakes.Final Answer:
Choosing appropriate testing tools -> Option CQuick Check:
Performance planning step = Choose tools [OK]
- Ignoring user load expectations
- Skipping scheduling of tests
- Not setting clear performance goals
test_plan = {
"goals": ["Check response time under 2s"],
"tools": ["LoadRunner"],
"schedule": "Weekly"
}
print(test_plan["goals"][0])What will be the output when this code runs?
Solution
Step 1: Understand the dictionary structure
test_plan has keys 'goals', 'tools', and 'schedule'. 'goals' is a list with one string: "Check response time under 2s".Step 2: Analyze the print statement
print(test_plan["goals"][0]) accesses the first item in the 'goals' list, which is "Check response time under 2s".Final Answer:
"Check response time under 2s" -> Option DQuick Check:
Access first goal string = "Check response time under 2s" [OK]
- Confusing keys and values in dictionary
- Trying to print the whole list instead of first item
- Expecting a KeyError due to wrong key
test_plan = {
"goals": "Check response time",
"tools": ["JMeter"],
"schedule": "Daily"
}
print(test_plan["goals"][0])What is the error in this code?
Solution
Step 1: Check the data type of 'goals'
'goals' is a string, but the code tries to access it like a list with [0]. Strings support indexing, but here it prints the first character, which may be unintended.Step 2: Understand the intention and error
The plan expects 'goals' to be a list of goals, not a single string. Accessing [0] on a string returns 'C', not the full goal. This is a logical error in planning structure.Final Answer:
Using a string instead of a list for 'goals' causes an error -> Option AQuick Check:
'goals' should be list, not string [OK]
- Treating string as list of goals
- Ignoring data types in test plan
- Assuming print syntax error instead of data error
A. Set clear performance goals like max response time
B. Skip tool selection to save time
C. Schedule tests during peak user hours
D. Ignore analyzing test results after execution
Solution
Step 1: Identify important planning actions for performance tests
Setting clear performance goals (e.g., max response time under 1000 user load) is essential to measure success.Step 2: Evaluate each option
Only A is a correct action; B skips necessary tools, C risks production disruption during real peak usage (simulate in staging), D skips vital analysis.Final Answer:
Set clear performance goals like max response time -> Option BQuick Check:
Clear goals = effective performance test plan [OK]
- Skipping tool selection reduces test accuracy
- Ignoring results wastes test effort
- Not simulating real user load
Ready to prove it?
Pass the test and this pattern moves unknown
