JMeter支持的线程数取决于系统的硬件资源和测试计划中的配置。针对'1核2GB RAM”,可以通过以下步骤来配置JMeter的线程数:
- 打开JMeter并创建一个新的测试计划。
- 添加一个'线程组”元件,以模拟并发用户负载。
- 在线程组中,设置所需的线程数,可以逐步增加这个数字,直到达到理想的负载。
- 调整'请求默认值”元件中的'连接”和'超时”设置,以确保测试用例能够有效地处理并发请求。
例如,以下代码示例演示了如何创建一个包含10个线程的测试计划:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.protocol.http.config.gui.HttpDefaultsGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
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 JMeterTest {
public static void main(String[] argv) throws Exception {
JMeterUtils.setJMeterHome("C:/apache-jmeter-5.4.1");
JMeterUtils.loadJMeterProperties("C:/apache-jmeter-5.4.1/bin/jmeter.properties");
HashTree testPlanTree = new HashTree();
TestPlan testPlan = new TestPlan();
testPlan.setName("Test Plan");
testPlan.setProperty("thread_groups", "1");
testPlan.setProperty("threads_per_group", "10");
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setName("HTTP Request");
httpSampler.setDomain("example.com");
httpSampler.setPath("/index.html");
httpSampler.setMethod("GET");
HttpDefaultsGui httpDefaultsGui = new HttpDefaultsGui();
httpDefaultsGui.setConnectTimeout(new CompoundVariable("5000"));
httpDefaultsGui.setResponseTimeout(new CompoundVariable("5000"));
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Thread Group");
threadGroup.setNumThreads(Integer.parseInt(testPlan.getPropertyAsString("threads_per_group")));
threadGroup.setProperty("ramp_time", "5");
threadGroup.setProperty("duration", "30");
threadGroup.setProperty("delay", "0");
threadGroup.setProperty("on_sample_error", "continue");
threadGroup.add