为什么 JMeter 变量不是从 JSR223 Groovy 断言中设置的?
Why JMeter varibles aren't set from JSR223 Groovy Assertion?
我正在尝试设置从 JSR223 内部的 Jmeter 属性中提取的 Jmeter 变量 Groovy 断言。
我感兴趣的 Jmeter 属性如下所示:
...
created_blob_A_6= fde65de0-3e32-11e8-a5b4-3906549016d8
created_blob_A_8= fef92d70-3e32-11e8-a5b4-3906549016d8
created_blob_A_9= ff775e20-3e32-11e8-bac3-e51250ffea15
created_blob_B_1= fd7302a0-3e32-11e8-a5b4-3906549016d8
created_blob_B_10= 00141350-3e33-11e8-bac3-e51250ffea15
...
为了从 Jmeter 属性中提取值,我创建了 JSR223 Groovy 以下断言脚本:
def readParamPrefix = 'created_blob'
def writeParamPrefix = 'blob_to_delete'
def chucnkTypes = 'A'..'E'
def newBlobCounter = 1
chucnkTypes.each{ chunkLetter ->
(1..10).each{ streamNumber ->
String readParamName = readParamPrefix + '_' + chunkLetter + '_' + streamNumber
log.info('Read param name: ' + readParamName)
String writeParamName = writeParamPrefix + '_' + newBlobCounter
log.info('Write param name: ' + writeParamName)
String blob_id_to_delete = props.get(readParamName).toString().trim()
log.info('' + readParamName + ' => ' + writeParamName + ' (' + blob_id_to_delete + ')')
vars.put(writeParamName.toString(), blob_id_to_delete.toString())
newBlobCounter++
}
}
该脚本不适用于 JMeter 变量,但适用于 JMeter 属性。这是 JMeter 属性的样子:
JMeterProperties:
...
blob_to_delete_1=9b1c4f40-3e36-11e8-a5b4-3906549016d8
blob_to_delete_10=9da5e050-3e36-11e8-bac3-e51250ffea15
blob_to_delete_11=9b235420-3e36-11e8-bac3-e51250ffea15
blob_to_delete_50=9b656630-3e36-11e8-bac3-e51250ffea15
你能告诉我如何修复我的代码以正确设置 JMeter 变量吗?
不要在脚本中使用 ${varName},注意 JSR223 Best Practices:
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use :
vars.get("varName")
You can also pass them as Parameters to the script and use them this way.
改完后,如果还是不行,在日志中查找错误
我没有发现您的代码有任何问题:
所以我会推荐:
- 正在检查 jmeter.log 文件中是否有任何可疑条目
- 使用 Debug Sampler and View Results Tree listener combination. See How to Debug your Apache JMeter Script 文章检查定义了哪些变量,了解有关深入了解 JMeter 脚本失败或意外行为的更多信息。
我正在尝试设置从 JSR223 内部的 Jmeter 属性中提取的 Jmeter 变量 Groovy 断言。
我感兴趣的 Jmeter 属性如下所示:
... created_blob_A_6= fde65de0-3e32-11e8-a5b4-3906549016d8 created_blob_A_8= fef92d70-3e32-11e8-a5b4-3906549016d8 created_blob_A_9= ff775e20-3e32-11e8-bac3-e51250ffea15 created_blob_B_1= fd7302a0-3e32-11e8-a5b4-3906549016d8 created_blob_B_10= 00141350-3e33-11e8-bac3-e51250ffea15 ...
为了从 Jmeter 属性中提取值,我创建了 JSR223 Groovy 以下断言脚本:
def readParamPrefix = 'created_blob'
def writeParamPrefix = 'blob_to_delete'
def chucnkTypes = 'A'..'E'
def newBlobCounter = 1
chucnkTypes.each{ chunkLetter ->
(1..10).each{ streamNumber ->
String readParamName = readParamPrefix + '_' + chunkLetter + '_' + streamNumber
log.info('Read param name: ' + readParamName)
String writeParamName = writeParamPrefix + '_' + newBlobCounter
log.info('Write param name: ' + writeParamName)
String blob_id_to_delete = props.get(readParamName).toString().trim()
log.info('' + readParamName + ' => ' + writeParamName + ' (' + blob_id_to_delete + ')')
vars.put(writeParamName.toString(), blob_id_to_delete.toString())
newBlobCounter++
}
}
该脚本不适用于 JMeter 变量,但适用于 JMeter 属性。这是 JMeter 属性的样子:
JMeterProperties: ... blob_to_delete_1=9b1c4f40-3e36-11e8-a5b4-3906549016d8 blob_to_delete_10=9da5e050-3e36-11e8-bac3-e51250ffea15 blob_to_delete_11=9b235420-3e36-11e8-bac3-e51250ffea15 blob_to_delete_50=9b656630-3e36-11e8-bac3-e51250ffea15
你能告诉我如何修复我的代码以正确设置 JMeter 变量吗?
不要在脚本中使用 ${varName},注意 JSR223 Best Practices:
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName") You can also pass them as Parameters to the script and use them this way.
改完后,如果还是不行,在日志中查找错误
我没有发现您的代码有任何问题:
所以我会推荐:
- 正在检查 jmeter.log 文件中是否有任何可疑条目
- 使用 Debug Sampler and View Results Tree listener combination. See How to Debug your Apache JMeter Script 文章检查定义了哪些变量,了解有关深入了解 JMeter 脚本失败或意外行为的更多信息。