我正在尝试使用JSR223 PreProcessor在jmeter中使用csv数据创建动态json
以下是我使用CSV来存储ID和名称数据的代码:
def builder = new groovy.json.JsonBuilder()
@groovy.transform.Immutable
class Items {
String Id
String Name
}
def items = new File("Item.txt").readLines().collect { line -> new Items(line.split(",")[0], line.split(",")[1]) }
builder.Rule(
__type: "DataCollectionRule",
DeviceFamily: '${__P(DeviceFamily)}',
RuleId: 0,
Name: 'test-${__time(yyyy-MM-dd'T'hh:mm:ss)}-${__counter(TRUE,)}',
Targets:
[
Groups :
[
[
Id: '${logicalid1_1}',
]
],
Devices:
[
]
],
StartDate: '/Date(${__time(,)})/',
IsEnabled: true,
Priority: 0,
AlertType: 0,
DeliverySchedule:
[
Id : 1,
Name : "Every 30 Minutes",
Period : "30M"
],
CollectionSchedule:
[
Id : 1,
Name : "Every 30 Minutes",
Period : "30M"
],
Items : items.collect() [
[
Id : it.Id,
Name : it.Name
]
],
LocationAccuracy:
[
UseGPS : false,
DistanceInMeters : 100,
ReportToServer : true,
AccuracyInMeters : 10
],
HasDolphinCounters: false,
EnrollmentCertificateId: null,
EnrollmentCertificateName: "",
DatabaseHighWatermark: 28,
DatabaseLowWatermark: 14,
DeviceHighWatermark: 400,
DeviceLowWatermark: 200
)
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toPrettyString(), '')
sampler.setPostBodyRaw(true);
运行测试时,我收到HTTP 400的请求错误
日志消息如下所示:
2018-09-24 13:49:23,669错误o.a.j.m.JSR223PreProcessor:JSR223脚本,JSR223 PreProcessor中的问题 javax.script.ScriptException:groovy.lang.MissingPropertyException:无此类属性:对于类:Script32 在org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320)〜[groovy-all-2.4.13.jar:2.4.13] 在org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72)〜[groovy-all-2.4.13.jar:2.4.13] 在javax.script.CompiledScript.eval(未知来源)〜[?:1.8.0_151] 在org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221)〜[ApacheJMeter_core.jar:4.0 r1823414] 在org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44)[ApacheJMeter_components.jar:4.0 r1823414] 在org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:849)上[ApacheJMeter_core.jar:4.0 r1823414] 在org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:467)上[ApacheJMeter_core.jar:4.0 r1823414] 在org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416)[ApacheJMeter_core.jar:4.0 r1823414] 在org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250)上[ApacheJMeter_core.jar:4.0 r1823414] 在java.lang.Thread.run(未知来源)[?: 1.8.0_151] 引起原因:groovy.lang.MissingPropertyException:无此类属性:对于类:Script32 在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)〜[groovy-all-2.4.13.jar:2.4.13] 在org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)〜[groovy-all-2.4.13.jar:2.4.13] 在org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)〜[groovy-all-2.4.13.jar:2.4.13] 在Script32.run(Script32.groovy:46)〜[?:?] 在org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)〜[groovy-all-2.4.13.jar:2.4.13] ...还有9个
CSV如下:
-1,BatteryStatus
-3,AvailableMemory
-5,AvailableStorage
提前谢谢
答案 0 :(得分:1)
您必须在此处使用{}
作为闭包:
items.collect() { // wrong: [
// ...
} // wrong: ]
或者只是items.collect { ... }
使用[]
,编译器将其视为地图文字,并且您遇到上述错误(it
未定义)
答案 1 :(得分:0)
我相信您应该更准确地复制和粘贴example code,以使“项目”部分看起来像这样:
Items: items.collect() {
[
Id : it.Id,
Name: it.Name
]
}
还请注意,您不应直接在Groovy脚本中使用JMeter Functions and or Variables,因为它与GString Template功能冲突并且使caching of compiled scripts不可能对性能产生负面影响。
所以我也建议更改:
${__P(DeviceFamily)
至props.get('DeviceFamily
)`${__time(yyyy-MM-dd'T'hh:mm:ss)}
至new Date().format("yyyy-MM-dd'T'hh:mm:ss")
如果需要,请参见The Groovy Templates Cheat Sheet for JMeter文章以获取有关JMeter中Groovy脚本的更多信息