时髦 创建动态 json 时没有这样的属性错误



我正在尝试使用 JSR223 预处理器在 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.JSR223预处理器:JSR223 脚本中的问题,JSR223 预处理器 javax.script.ScriptException: groovy.lang.MissingPropertyException: No 这样的属性: it for class: Script32 at org.codehaus.groovy.jsr223.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] at javax.script.CompiledScript.eval(未知来源( ~[?:1.8.0_151] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221( ~[ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44( [ApacheJMeter_components.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.runPreProcessor(JMeterThread.java:849( [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:467( [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416( [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250( [ApacheJMeter_core.jar:4.0 r1823414] at java.lang.Thread.run(未知来源( [?:1.8.0_151] 原因:groovy.lang.MissingProperty异常:没有这样的属性:它对于类:Script32 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java: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] at 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( ~[?:?] at 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

提前谢谢你

您必须在此处使用{}作为闭包:

items.collect() { // wrong: [
// ...
} // wrong: ]

或者只是items.collect { ... }

使用[]编译器将看到它作为映射文字,并且您得到上述错误(it未定义(

我相信您应该更准确地复制和粘贴示例代码,以便您的"项目"部分应如下所示:

Items: items.collect() {
[
Id  : it.Id,
Name: it.Name
]
}

另请注意,您不应该直接在Groovy脚本中使用JMeter函数和/或变量,因为它与GString Template功能冲突,并且无法缓存已编译的脚本,从而对性能产生负面影响。

所以我也建议更改:

  • ${__P(DeviceFamily)props.get('DeviceFamily('
  • ${__time(yyyy-MM-dd'T'hh:mm:ss)}new Date().format("yyyy-MM-dd'T'hh:mm:ss")
  • 等。

如果需要,请参阅 JMeter 的 Groovy 模板备忘单文章,了解有关 JMeter 中 Groovy 脚本的更多信息

相关内容

  • 没有找到相关文章

最新更新