• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

jmeter 创建100个现场组,每个线程组里面有1个http请求,并针对每个http请求响应头进行断言

 

import org.apache.jmeter.control.LoopController
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy
import org.apache.jmeter.testelement.TestPlan
import org.apache.jmeter.threads.ThreadGroup
import org.apache.jorphan.collections.HashTree
import org.apache.jmeter.save.SaveService
import org.apache.jmeter.testelement.TestElement
import org.apache.jmeter.assertions.ResponseAssertion
import java.io.FileOutputStream

try {
    log.info("开始创建符合规范的JMX文件...")
    
    // 初始化SaveService
    SaveService.loadProperties()
    
    // 创建测试计划 - 使用JMeter的标准方式
    def testPlan = new TestPlan("标准测试计划")
    testPlan.setEnabled(true)
    testPlan.setProperty(TestElement.GUI_CLASS, "TestPlanGui")
    testPlan.setProperty(TestElement.TEST_CLASS, "TestPlan")
    
    def testPlanTree = new HashTree()
    testPlanTree.add(testPlan)
    
    def threadGroupsTree = new HashTree()
    
    // 创建3个线程组
    (1..3).each { i ->
        // 创建循环控制器
        def loopController = new LoopController()
        loopController.setEnabled(true)
        loopController.setName("Loop Controller")
        loopController.setLoops(1)
        loopController.setContinueForever(false)
        loopController.setProperty(TestElement.GUI_CLASS, "LoopControlPanel")
        loopController.setProperty(TestElement.TEST_CLASS, "LoopController")
        
        // 创建线程组
        def threadGroup = new ThreadGroup()
        threadGroup.setEnabled(true)
        threadGroup.setName("线程组-${i}")
        threadGroup.setNumThreads(1)
        threadGroup.setRampUp(1)
        threadGroup.setScheduler(false)
        threadGroup.setProperty(TestElement.GUI_CLASS, "ThreadGroupGui")
        threadGroup.setProperty(TestElement.TEST_CLASS, "ThreadGroup")
        threadGroup.setProperty("ThreadGroup.on_sample_error", "continue")
        threadGroup.setSamplerController(loopController)
        
        // 创建HTTP请求
        def httpSampler = new HTTPSamplerProxy()
        httpSampler.setEnabled(true)
        httpSampler.setName("HTTP请求-${i}")
        httpSampler.setDomain("10.52.1.60")
        httpSampler.setPort(8443)
        httpSampler.setProtocol("https")
        httpSampler.setPath("/download/195")
        httpSampler.setMethod("GET")
        httpSampler.setFollowRedirects(true)
        httpSampler.setUseKeepAlive(true)
        httpSampler.setProperty(TestElement.GUI_CLASS, "HttpTestSampleGui")
        httpSampler.setProperty(TestElement.TEST_CLASS, "HTTPSamplerProxy")
        
        // 创建响应断言 - 检查响应头是否包含attachment
        def responseAssertion = new ResponseAssertion()
        responseAssertion.setEnabled(true)
        responseAssertion.setName("检查响应头attachment")
        responseAssertion.setProperty(TestElement.GUI_CLASS, "AssertionGui")
        responseAssertion.setProperty(TestElement.TEST_CLASS, "ResponseAssertion")
        
        // 设置断言属性 - 使用正确的方法名
        responseAssertion.setTestFieldResponseHeaders()  // 测试响应头
        responseAssertion.setToContainsType()            // 包含匹配
        
        // 添加测试模式 - 使用正确的方法
        responseAssertion.addTestString("attachment")    // 匹配模式
        
        // 构建HTTP请求的子树(包含断言)
        def httpSamplerTree = new HashTree()
        httpSamplerTree.add(responseAssertion)  // 将断言添加到HTTP请求的子树中
        
        // 构建循环控制器的子树
        def loopTree = new HashTree()
        loopTree.add(httpSampler, httpSamplerTree)  // HTTP请求和它的断言子树
        
        def tgTree = new HashTree()
        tgTree.add(loopController, loopTree)
        
        threadGroupsTree.add(threadGroup, tgTree)
        
        log.info("创建线程组-${i} 并添加attachment断言")
    }
    
    testPlanTree.add(threadGroupsTree)
    
    // 保存文件
    def outputFile = new File("30_standard_test_plan_with_assertion.jmx")
    FileOutputStream fos = new FileOutputStream(outputFile)
    SaveService.saveTree(testPlanTree, fos)
    fos.close()
    
    def result = "✅ 带attachment断言的JMX文件创建成功: ${outputFile.absolutePath}"
    log.info(result)
    
    SampleResult.responseData = result
    SampleResult.successful = true
    
} catch (Exception e) {
    def error = "❌ 创建失败: ${e.message}"
    log.error(error, e)
    SampleResult.responseData = error
    SampleResult.successful = false
}

 

posted on 2025-10-30 10:48  yxchun  阅读(6)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3