我正在尝试使用带有 JSR223 PreProcessor 的 jmeter 中的 csv 数据创建一个动态 json
下面是我使用 CSV 获取 Id 和 Name 数据的代码:
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 错误 oajmJSR223PreProcessor:JSR223 脚本中的问题,JSR223 PreProcessor javax.script.ScriptException:groovy.lang.MissingPropertyException:没有这样的属性:它适用于类:org.codehaus.groovy.jsr223 的 Script32。 GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-all-2.4.13.jar:2.4.13] at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all -2.4.13.jar:2.4.13] 在 javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_151] at 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(Unknown Source) [?:1.8.0_151] 引起:groovy.lang.MissingPropertyException:没有这样的属性:它用于类:org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java 的 Script32 :53) ~[groovy-all-2.4.13.jar:2.4.13] at 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] at 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
先感谢您