Get random values from an array

前端 未结 3 2098
醉话见心
醉话见心 2020-12-16 23:15

I defined a new variable

Name        Value            Description
categories  (1, 2, 3, 4, 5)  my categories ids

and in my path i want to

3条回答
  •  余生分开走
    2020-12-16 23:41

    For your scenario you could try to use the JSR233 components (Sampler, PreProcessor, PostProcessor) with a bit of java/groovy code.

    E.g.:

    • Define your data as you've done:

      Name        Value          
      categories  1,2,3,4,5
      

      (i.e. use comma as delimiter, no spaces before and after comma).

    • Use the JSR233 Sampler / PreProcessor / PostProcessor with the following code:

      import java.util.Random;
      
      String[] categories = (vars.get("categories")).split(",");
      
      int idx = new Random().nextInt(categories.length);
      String category = (categories[idx]);
      
      vars.put("rnd_cat", category);
      
    • Refer to the randomly picked category using ${rnd_cat}.

提交回复
热议问题