无法从非GUI模式运行Jmeter测试

时间:2020-09-30 11:19:18

标签: groovy jmeter command-line-interface

尝试从非GUI模式运行我的Jmeter测试,并在jmeter.log中收到错误

2020-09-30 14:08:54,802 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, Prepare payload from DB dump.csv
javax.script.ScriptException: java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at javax.script.CompiledScript.eval(CompiledScript.java:92) ~[?:1.8.0_251]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:222) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:45) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:950) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_251]
Caused by: java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
    at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[?:1.8.0_251]
    at java.util.ArrayList.get(ArrayList.java:433) ~[?:1.8.0_251]
    at java_util_List$get.call(Unknown Source) ~[?:?]
    at Script1$_run_closure2.doCall(Script1.groovy:21) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_251]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_251]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_251]
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107) ~[groovy-3.0.3.jar:3.0.3]
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:263) ~[groovy-3.0.3.jar:3.0.3]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1029) ~[groovy-3.0.3.jar:3.0.3]
    at groovy.lang.Closure.call(Closure.java:412) ~[groovy-3.0.3.jar:3.0.3]
    at groovy.lang.Closure.call(Closure.java:428) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.upto(DefaultGroovyMethods.java:16062) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.dgm$847.invoke(Unknown Source) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:247) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148) ~[groovy-3.0.3.jar:3.0.3]
    at Script1.run(Script1.groovy:20) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    ... 9 more

这是我从数据库dump.csv准备负载的样子

def adjustStatuses = []
def adjustItems = []

1.upto(${amount_of_items}, { index ->
    def data = new File('/Jmeter/items.csv').readLines().get(index).split(',') // Path to file with variant_id(s)
    def attr = [:]
    attr.put('variant_id', data[0].toInteger()) // Parse needed amount of variants from file, indicating proper index of variants column in file
    def attributes = [:]
    def param = [:]
        param.put('quantity', ${quantity})
    param.put('status_id', 4)
    attributes.put('adjust_statuses', [param])
    attributes.put('identity', attr)
    adjustItems.add(attributes)
})

def items = []
def attributes = [:]

1.upto(${amount_of_locations}, { index ->
    def data = new File('/Jmeter/locations.csv').readLines().get(index).split(',') // Path to file with allotment(s)/location(s)
    def attr = [:]
    attr.put('adjust_items', adjustItems)
    attr.put('location_id', data[0].toInteger()) // Parse needed amount of allotments from file, indicating proper index of allotment column in file
    items.add(attr)
    attributes.put('items', items)
})

def json = new groovy.json.JsonBuilder(attributes)

sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)

最奇怪的是,当尝试从GUI运行测试时,我没有错误 我所有的api调用都成功了,我不明白为什么CLI会出错

1 个答案:

答案 0 :(得分:0)

IndexOutOfBoundsException:索引:11,大小:11

IndexOutOfBoundsException表示您的amount_of_itemsamount_of_locations大于items.csvlocations.csv中的行数

“安全”的方式是迭代文件中的行,因为尚不清楚在何处以及如何获得这些amount_of_itemsamount_of_locations变量

也不要在Groovy脚本中引用诸如${amount_of_items}之类的JMeter变量,而是使用vars作为vars.get('amount_of_items')之类的JMeterVariables类实例的简写,因为您使用了缓存编译脚本功能Groovy将缓存第一次出现的变量,并且不会在以后的调用中对其进行更新。

更多信息:

所以改变这个:

1.upto($ { amount_of_locations }, { index ->
    def data = new File('/Jmeter/locations.csv').readLines().get(index).split(',')

对此:

new File('/Jmeter/locations.csv').readLines().each { line ->
    def data =  line.split(',')

并对“项目”执行相同操作