请求协助:用JMeter基于District.csv生成子代理并导出文件(需JMX示例)
需求说明
有一个包含10个Agent的District.csv文件,需要为每个Agent生成11个子代理(总计110个),输出格式要求为Agent1,subagent1重复11次、Agent2,subagent2重复11次等,并将结果写入文件。同时希望获取实现方法及JMeter JMX脚本示例。
已尝试的方案
- 设置包含10个线程的Thread Group
- 搭配循环次数为11的Loop Controller
- 使用Sampler创建代理
- 通过Post Processor将结果写入文件
JMeter实现方法
步骤1:配置CSV读取组件
在Thread Group下添加CSV Data Set Config,配置如下:
- 文件名:填写
District.csv的完整路径 - 变量名:设为
agentName(假设CSV每行是单个Agent名称,比如Agent1) - 分隔符:根据你的CSV实际分隔符设置(默认逗号即可)
- 循环读取:设为
False,保证每个线程对应唯一的Agent
步骤2:调整线程与循环逻辑
- Thread Group的线程数设为10(对应10个Agent)
- 在Thread Group下添加Loop Controller,循环次数设为11(每个Agent生成11个子代理)
步骤3:生成格式并写入文件
在Loop Controller下添加JSR223 Sampler,用Groovy代码完成格式生成与文件写入:
// 获取当前线程对应的Agent名称 def agent = vars.get("agentName") // 构造子代理字符串(如果你的Agent命名规则不同,修改replaceAll部分即可) def subAgentLine = "${agent},subagent${agent.replaceAll('Agent', '')}" // 以追加模式写入输出文件,路径可自行修改 def writer = new FileWriter("subagents_output.csv", true) writer.write(subAgentLine + System.getProperty("line.separator")) writer.close()
步骤4:初始化输出文件
在Test Plan最开头添加一个JSR223 Sampler,用于清空旧输出文件,避免每次运行追加历史数据:
def outputFile = new File("subagents_output.csv") if (outputFile.exists()) { outputFile.delete() }
JMX脚本核心结构示例
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.6.3"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="生成子代理" enabled="true"> <stringProp name="TestPlan.comments"></stringProp> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户自定义变量" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="TestPlan.user_define_classpath"></stringProp> </TestPlan> <hashTree> <JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="清空输出文件" enabled="true"> <stringProp name="scriptLanguage">groovy</stringProp> <stringProp name="parameters"></stringProp> <stringProp name="script">def outputFile = new File("subagents_output.csv") if (outputFile.exists()) { outputFile.delete() }</stringProp> </JSR223Sampler> <hashTree/> <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Agent线程组" enabled="true"> <stringProp name="ThreadGroup.on_sample_error">continue</stringProp> <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <stringProp name="LoopController.loops">1</stringProp> </elementProp> <stringProp name="ThreadGroup.num_threads">10</stringProp> <stringProp name="ThreadGroup.ramp_time">1</stringProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"></stringProp> <stringProp name="ThreadGroup.delay"></stringProp> <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp> </ThreadGroup> <hashTree> <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="读取Agent列表" enabled="true"> <stringProp name="filename">District.csv</stringProp> <stringProp name="fileEncoding"></stringProp> <stringProp name="variableNames">agentName</stringProp> <stringProp name="delimiter">,</stringProp> <boolProp name="quotedData">false</boolProp> <boolProp name="recycle">false</boolProp> <boolProp name="stopThread">true</boolProp> <stringProp name="shareMode">shareMode.all</stringProp> </CSVDataSet> <hashTree/> <LoopController guiclass="LoopControlPanel" testclass="LoopController" testname="生成11个子代理" enabled="true"> <boolProp name="LoopController.continue_forever">false</boolProp> <stringProp name="LoopController.loops">11</stringProp> </LoopController> <hashTree> <JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="写入子代理数据" enabled="true"> <stringProp name="scriptLanguage">groovy</stringProp> <stringProp name="parameters"></stringProp> <stringProp name="script">def agent = vars.get("agentName") def subAgentLine = "${agent},subagent${agent.replaceAll('Agent', '')}" def writer = new FileWriter("subagents_output.csv", true) writer.write(subAgentLine + System.getProperty("line.separator")) writer.close()</stringProp> </JSR223Sampler> <hashTree/> </hashTree> </hashTree> </hashTree> </hashTree> </jmeterTestPlan>
内容的提问来源于stack exchange,提问作者Kiran
相关产品推荐
相关产品推荐

