Jmeter - 如果使用Java已经存在变量,则创建一个新变量

时间:2017-07-25 10:25:47

标签: java jmeter

我正在使用Jmeter并希望使用Java来更新变量,

我有一个名为XXVONO的变量,它存储值并在循环中执行时添加数字后缀。例如:

XXVONO_1 = value1

XXVONO_2 = value2

XXVONO_3 = value3

这些变量包含在执行循环时自动存储的值。但是,我试图创建一个代码来检查变量是否为空,如果为true,它将保存新值,如果为false,它将创建一个新变量(XXVONO_4)并将值保存在那里而不覆盖现有变量。

我该怎么做呢?我是否使用了while循环?

if (vars.get("VONO_2") != "") {
    if (vars.get("XXVONO_" + vars.get("aps200_count_3")) == "") {
        vars.put("XXVONO_" + vars.get("aps200_count_3"), vars.get("VONO_2"));
        vars.put("XXJRNO_" + vars.get("aps200_count_3"), vars.get("JRNO_2"));
    } else {
        while (vars.get("XXVONO_" + vars.get("aps200_count_3")) != "") {
            vars.put("new_count", vars.get("aps200_count_3"));
            Integer temp = Integer.parseInt(vars.get("new_count")) + 1;
            vars.put("new_count", temp.toString());         
        }
        vars.put("XXVONO_" + vars.get("new_count"), vars.get("VONO_2"));
        vars.put("XXJRNO_" + vars.get("new_count"), vars.get("JRNO_2"));
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试使用地图而不是在运行时创建变量

Mat frame;
Mat stat, centroid;
int threshval = 100;
static void on_trackbar(int, void*){
 Mat bw = threshval < 128 ? (frame < threshval) : (frame > threshval);
 Mat labelImage(frame.size(), CV_32S);
 int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);
 std::vector<Vec3b> colors(nLabels);
      colors[0] = Vec3b(0, 0, 0);//background
   for (int label = 1; label < nLabels; ++label) {
 colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));}
 at dst(frame.size(), CV_8UC3);
 for (int r = 0; r < dst.rows; ++r) {
     for (int c = 0; c < dst.cols; ++c) {
         int label = labelImage.at<int>(r, c);
         Vec3b &pixel = dst.at<Vec3b>(r, c);

         pixel = colors[label];} 
for (int i = 0;i < nLabels;i++)
    {

        vector<Rect> rComp;
        rComp.push_back(Rect(Point((stat.at<int>(i, CC_STAT_LEFT) ), (stat.at<int>(i, CC_STAT_TOP) )), Size((stat.at<int>(i, CC_STAT_WIDTH) ), (stat.at<int>(i, CC_STAT_HEIGHT)))));
    //  

            rectangle(dst, Rect(Point(stat.at<int>(i, CC_STAT_LEFT  ) , stat.at<int>(i, CC_STAT_TOP  ) ), Size(stat.at<int>(i, CC_STAT_WIDTH   ) , stat.at<int>(i, CC_STAT_HEIGHT  ))), Scalar(0, 255, 255));}
}

    for (int i = 0;i < nLabels;i++) {
        int x = stat.at<int>(i, CC_STAT_LEFT);
        int y = stat.at<int>(i, CC_STAT_TOP);
        int w = stat.at<int>(i, CC_STAT_WIDTH) ;
        int h = stat.at<int>(i, CC_STAT_HEIGHT);
        rectangle(frame, Rect(x,y,w,h), Scalar(0, 255, 255));
    }
}
imshow("Connected Components", dst);

循环内部

Map<String,Object> map = new HashMap<>();

答案 1 :(得分:1)

你可以做的是使用if / else语句:

if (XXVONO_1 == null)
{
  XXYVONO_1 = //Insert data here
}
else if (XXVONO_2 == null)
{
  XXVONO_2 = //Insert data here
}
else  if (XXVONO_3 == null)
{
  XXVONO_3 == //Insert data here
}
else
{
  XXVONO_4 == //Insert data here
}

当然,您可以继续添加变量。

如果变量数量没有限制,请尝试:

HashMap<String, String> XXVONO = new HashMap<String, Integer>();

for (i = 1; i <= /*Number of variables*/; i += 1; i++) {
  if (XXVONO["XXVONO_" + i] == null) {
    XXVONO.put("XXVONO_" + i, /*insert data here*/);
  }
}