如何用Groovy脚本设置SoapUI LoadTestRunner负载策略及参数
SoapUI Groovy脚本负载测试配置指南
测试步骤 vs Setup Script 选择
- 一次性全局配置负载参数:用Setup Script,它在测试套件/用例启动前仅执行一次,适合固定的初始化配置
- 动态调整负载参数:用Groovy测试步骤,可在测试流程中根据条件随时修改负载策略或参数,灵活性更强
负载策略设置(解决setLoadStrategy报错问题)
setLoadStrategy方法要求传入LoadStrategy对象而非字符串,需通过SoapUI内置的策略类实例化,步骤如下:
1. 导入必要类(如果未自动导入)
import com.eviware.soapui.impl.wsdl.loadtest.strategy.*
2. 不同负载策略的代码示例
Simple策略(基础固定线程)
// 获取目标负载测试对象 def loadTest = context.testCase.testSuite.getLoadTestByName("你的负载测试名称") // 实例化Simple策略 def simpleStrategy = new SimpleLoadStrategy(loadTest) // 设置参数 simpleStrategy.setThreadCount(10) // 线程数 simpleStrategy.setStartDelay(5000) // 启动延迟(毫秒) // 绑定到负载测试 loadTest.setLoadStrategy(simpleStrategy)
Burst策略(爆发式负载)
def loadTest = context.testCase.testSuite.getLoadTestByName("你的负载测试名称") def burstStrategy = new BurstLoadStrategy(loadTest) // 核心参数设置 burstStrategy.setThreadCount(20) // 总线程数 burstStrategy.setStartDelay(3000) // 初始启动延迟 burstStrategy.setBurstDelay(1000) // 两次爆发间隔(毫秒) burstStrategy.setBurstDuration(5000) // 单次爆发持续时间(毫秒) burstStrategy.setBurstCount(5) // 爆发次数 loadTest.setLoadStrategy(burstStrategy)
其他策略示例
- 渐变负载(RampUp):使用
RampUpLoadStrategy,调用setRampUpIncrement(每次递增线程数)、setRampUpDelay(递增间隔) - 波动负载(Variance):使用
VarianceLoadStrategy,调用setVariance(线程数波动范围)、setVarianceDelay(波动间隔)
注意事项
- 确保负载测试名称与SoapUI中定义的完全一致,否则
getLoadTestByName会返回null - 在Setup Script中使用时,需将
context.testCase.testSuite替换为直接的testSuite引用(Setup Script的上下文已包含testSuite对象) - 调试时可通过
log.info(loadTest.getLoadStrategy().getClass().simpleName)查看当前生效的策略类型
内容的提问来源于stack exchange,提问作者marecki
相关产品推荐
相关产品推荐

