For Loop 在 Beanshell 脚本中的表现不如预期

huangapple go评论67阅读模式
英文:

For Loop not working as expected in Beanshell scripting Jmeter

问题

以下是我的代码,我正在尝试循环遍历一组状态,并在状态等于N时中断,并在同一索引处保存EpisodeId。 (使用Javascript在beanshell postprocessor中)

var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;

log.info("Status的数量:" + count)

for (i = 1; i <= count; i++) {
    var AuthStatus_i = vars.get("AuthStatus_" + i);
    log.info("Auth状态:" + AuthStatus_i);
    
    if (AuthStatus_i.equals("N")) {
        EpisodeID = vars.get("corr_EpisodeID_" + i);
        break;
    }
}

log.info("EpisodeID:" + EpisodeID);
vars.put("EpisodeID", EpisodeID);

但我的循环没有按预期工作。循环运行了2次迭代,并在找到N状态之前中断。以下是日志响应:

捕获 'N' 状态的 EpisodeID: Status的数量:3
捕获 'N' 状态的 EpisodeID: Auth状态:null
捕获 'N' 状态的 EpisodeID: Auth状态:A
捕获 'N' 状态的 EpisodeID: EpisodeID:undefined
英文:

Below is my code, where I am trying to loop through set of Status's and break if Status is equal N. And Save EpisodeId at same index. (using Javascript in beanshell postprocessor)

var i;
var count = vars.get(&quot;AuthStatus_matchNr&quot;);
var EpisodeID;

log.info(&quot;Count of Status:&quot;+count)

for(i=0;i&lt;=count;i++){
	var AuthStatus_i;
	AuthStatus_i = vars.get(&quot;AuthStatus_&quot;+i);
	log.info(&quot;Auth:&quot;+AuthStatus_i);
	if (AuthStatus_i == &quot;N&quot;){
	EpisodeID = vars.get(&quot;corr_EpisodeID_&quot;+i);
	break;
	}
else{
	i++;
	}
}
log.info(&quot;EpisodeID:&quot;+EpisodeID)
vars.put(&quot;EpisodeID&quot;,EpisodeID);

But my loop is not working as expected. Loop runs for 2 iteration and breaks before finding N status. Below is the Log response

Capture &#39;N&#39; Status EpisodeID: Count of Status:3
Capture &#39;N&#39; Status EpisodeID: Auth:null
Capture &#39;N&#39; Status EpisodeID: Auth:A
Capture &#39;N&#39; Status EpisodeID: EpisodeID:undefined

答案1

得分: 1

你需要移除这部分代码:

else {
    i++;
}

因为你已经在for循环中进行了增量操作。

另外要注意,根据JMeter最佳实践,你应该使用JSR223测试元素和Groovy语言进行脚本编写,所以考虑迁移。

更多信息:Apache Groovy: Groovy 用于什么?

英文:

You need to remove this bit:

else {
        i++;
    }

because you're already doing the increment in for loop.

Also be aware that according to JMeter Best Practices you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating.

More information: Apache Groovy: What Is Groovy Used For?

答案2

得分: 0

我明白问题出在哪里。在JavaScript中,数组和列表是从零开始索引的,这意味着索引从0开始,最后一个索引是count - 1。因此,您需要修改条件i <= count为i < count,以便遍历AuthStatus数组中的所有元素。

因此,正确的方法是这样的:

var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;

log.info("Status的数量: " + count);

for (i = 0; i < count; i++) {
    var AuthStatus_i = vars.get("AuthStatus_" + i);
    log.info("Auth: " + AuthStatus_i);

    if (AuthStatus_i === "N") {
        EpisodeID = vars.get("corr_EpisodeID_" + i);
        break;
    }
}

log.info("EpisodeID: " + EpisodeID);
vars.put("EpisodeID", EpisodeID);

现在基本上循环将遍历所有元素,直到找到"N"状态并分配相应的EpisodeID。

英文:

I see where the issue is. In JavaScript, arrays and lists are zero-based, which means the index starts from 0, and the last index is count - 1. Therefore, you need to modify the condition i <= count to i < count in order to iterate over all the elements in the AuthStatus array.

So correct way to do it would be like this:

var i;
var count = vars.get(&quot;AuthStatus_matchNr&quot;);
var EpisodeID;

log.info(&quot;Count of Status: &quot; + count);

for (i = 0; i &lt; count; i++) {
    var AuthStatus_i = vars.get(&quot;AuthStatus_&quot; + i);
    log.info(&quot;Auth: &quot; + AuthStatus_i);

    if (AuthStatus_i === &quot;N&quot;) {
        EpisodeID = vars.get(&quot;corr_EpisodeID_&quot; + i);
        break;
    }
}

log.info(&quot;EpisodeID: &quot; + EpisodeID);
vars.put(&quot;EpisodeID&quot;, EpisodeID);

Now basically the loop will iterate through all the elements until it finds the "N" status and assigns the corresponding EpisodeID.

huangapple
  • 本文由 发表于 2023年6月12日 17:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定