{"title":"Jmeter\u8fd0\u884c\u8ba1\u5212\u6bcf\u6bb55\u5206\u949f\u5de6\u53f3\u8f6e\u64ad\u67e5\u8be2\u63a5\u53e3\uff0c\u67e5\u5230\u6570\u636e\u5c31\u7528forEach Controller \u8fdb\u884c\u66f4\u65b0\uff1f\u793a\u4f8b\u89e3\u91ca\u8f91\u7801","description":"\u672c\u7bc7\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Jmeter\u4e2d\u8bbe\u7f6e\u5b9a\u65f6\u8f6e\u64ad\u67e5\u8be2\u63a5\u53e3\uff0c\u5e76\u4f7f\u7528ForEach\u63a7\u5236\u5668\u66f4\u65b0\u6570\u636e\u3002\u793a\u4f8b\u89e3\u91ca\u8f91\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u6d4b\u8bd5\u8ba1\u5212\u3001\u7ebf\u7a0b\u7ec4\u3001HTTP\u8bf7\u6c42\u3001ForEach\u63a7\u5236\u5668\u548cWhile\u63a7\u5236\u5668\uff0c\u5e76\u8bbe\u7f6e\u5b9a\u65f6\u8f6e\u64ad\u548c\u6570\u636e\u66f4\u65b0\u903b\u8f91\u3002","keywords":"Jmeter\uff0c\u5b9a\u65f6\u8f6e\u64ad\uff0c\u63a5\u53e3\uff0c\u6570\u636e\u66f4\u65b0\uff0cForEach\u63a7\u5236\u5668\uff0cWhile\u63a7\u5236\u5668\uff0c\u89e3\u91ca\u8f91\u7801\uff0c\u793a\u4f8b","content":"\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u89e3\u91ca\u8f91\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728JMeter\u4e2d\u8bbe\u7f6e\u5b9a\u65f6\u8f6e\u64ad\u67e5\u8be2\u63a5\u53e3\uff0c\u5e76\u4f7f\u7528ForEach\u63a7\u5236\u5668\u66f4\u65b0\u6570\u636e\u3002\n\njava\nimport org.apache.jmeter.control.LoopController\nimport org.apache.jmeter.control.WhileController\nimport org.apache.jmeter.threads.ThreadGroup\nimport org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy\nimport org.apache.jmeter.testelement.TestPlan\nimport org.apache.jmeter.util.JMeterUtils\nimport org.apache.jorphan.collections.HashTree\n\n// \u521b\u5efa\u6d4b\u8bd5\u8ba1\u5212\nTestPlan testPlan = new TestPlan(\"Test Plan\")\ntestPlan.setProperty(TestPlan.COMMENTS, \"Test Plan created by Groovy\")\n\n// \u521b\u5efa\u7ebf\u7a0b\u7ec4\nThreadGroup threadGroup = new ThreadGroup()\nthreadGroup.setName(\"Thread Group\")\nthreadGroup.setNumThreads(1)\nthreadGroup.setRampUp(1)\nthreadGroup.setSamplerController(new LoopController())\nthreadGroup.getSamplerController().setLoops(1)\n\n// \u521b\u5efaHTTP Sampler\nHTTPSamplerProxy httpSampler = new HTTPSamplerProxy()\nhttpSampler.setDomain(\"example.com\")\nhttpSampler.setPort(80)\nhttpSampler.setPath(\"/api/query\")\nhttpSampler.setMethod(\"GET\")\n\n// \u521b\u5efaForEach\u63a7\u5236\u5668\nForEachController forEachController = new ForEachController()\nforEachController.setName(\"ForEach Controller\")\nforEachController.setUseSeparator(false)\nforEachController.setReturnAll(false)\nforEachController.setVariableNames(\"data\")\nforEachController.setStartIndex(1)\n\n// \u521b\u5efaWhile\u63a7\u5236\u5668\nWhileController whileController = new WhileController()\nwhileController.setName(\"While Controller\")\nwhileController.setCondition(\"${data} != null\")\n\n// \u5c06HTTP\u8bf7\u6c42\u6dfb\u52a0\u5230ForEach\u63a7\u5236\u5668\u4e0b\nforEachController.addTestElement(httpSampler)\n\n// \u5c06ForEach\u63a7\u5236\u5668\u6dfb\u52a0\u5230While\u63a7\u5236\u5668\u4e0b\nwhileController.addTestElement(forEachController)\n\n// \u5c06While\u63a7\u5236\u5668\u6dfb\u52a0\u5230\u7ebf\u7a0b\u7ec4\u4e0b\nthreadGroup.setSamplerController(whileController)\n\n// \u5c06\u7ebf\u7a0b\u7ec4\u6dfb\u52a0\u5230\u6d4b\u8bd5\u8ba1\u5212\u4e0b\nHashTree testPlanTree = new HashTree()\nHashTree threadGroupTree = testPlanTree.add(testPlan, threadGroup)\nthreadGroupTree.add(httpSampler)\n\n// \u8bbe\u7f6eJMeter\u5c5e\u6027\nJMeterUtils.setProperty(\"httpsampler.ignore_failed_embedded_resources\", \"true\")\nJMeterUtils.setProperty(\"httpsampler.ignore_failed_embedded_resources\", \"true\")\nJMeterUtils.setProperty(\"httpsampler.ignore_failed_embedded_resources\", \"true\")\n\n// \u8fd0\u884c\u6d4b\u8bd5\u8ba1\u5212\ndef jmeter = new org.apache.jmeter.JMeter()\njmeter.configure(testPlanTree)\njmeter.run()\n\n\n\u4e0a\u8ff0\u793a\u4f8b\u8f91\u7801\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u8ba1\u5212\uff08Test Plan\uff09\uff0c\u7ebf\u7a0b\u7ec4\uff08Thread Group\uff09\uff0cHTTP\u8bf7\u6c42\uff08HTTP Sampler\uff09\uff0cForEach\u63a7\u5236\u5668\uff08ForEach Controller\uff09\u548cWhile\u63a7\u5236\u5668\uff08While Controller\uff09\u3002\n\n\u5728ForEach\u63a7\u5236\u5668\u4e2d\uff0c\u8bbe\u7f6e\u4e86\u53d8\u91cf\u540d\u79f0\u4e3a"data"\uff0c\u5728While\u63a7\u5236\u5668\u4e2d\uff0c\u8bbe\u7f6e\u4e86\u6761\u4ef6"${data} != null"\uff0c\u5373\u5f53\u63a5\u53e3\u8fd4\u56de\u7684\u6570\u636e\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u5faa\u73af\u6267\u884cForEach\u63a7\u5236\u5668\u4e2d\u7684HTTP\u8bf7\u6c42\u3002\n\n\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u793a\u4f8b\u8f91\u7801\u4e2d\u7684\u63a5\u53e3\u8def\u5f84\u3001\u53d8\u91cf\u540d\u79f0\u3001\u6761\u4ef6\u7b49\u53c2\u6570\u6765\u9002\u5e94\u4f60\u7684\u9700\u6c42\u3002

Jmeter定时轮询接口并更新数据:示例脚本代码

原文地址: https://www.cveoy.top/t/topic/pSAN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录