如何为json jatling编写自定义进纸器?

时间:2016-06-07 03:22:46

标签: json gatling

我有一个jsonurl我想读它,看起来像这样:

> {
>     "elements": [
>       {
>         "box": {
>           "x": 0,
>           "y": 0,
>           "width": 186,
>           "height": 10.526316
>         },
>         "type": "Header"
>       },
>       {
>         "box": {
>           "x": 0,
>           "y": 0,
>           "width": 0,
>           "height": 0
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 0,
>           "y": 14.035088,
>           "width": 43.333332,
>           "height": 3.508772
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 95.08772,
>           "y": 14.035088,
>           "width": 90.87719,
>           "height": 45.614037
>         },
>         "type": "Image"
>       },
>       {
>         "box": {
>           "x": 95.08772,
>           "y": 63.157894,
>           "width": 90.87719,
>           "height": 3.508772
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 95.08772,
>           "y": 63.157894,
>           "width": 90.87719,
>           "height": 3.508772
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 47.54386,
>           "y": 42.105263,
>           "width": 43.333332,
>           "height": 5.9649124
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 0,
>           "y": 0,
>           "width": 43.333332,
>           "height": 98.24561
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 47.54386,
>           "y": 0,
>           "width": 43.333332,
>           "height": 98.24561
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 95.08772,
>           "y": 0,
>           "width": 43.333332,
>           "height": 98.24561
>         },
>         "type": "Regular"
>       },
>       {
>         "box": {
>           "x": 142.63158,
>           "y": 0,
>           "width": 43.333332,
>           "height": 98.24561
>         },
>         "type": "Regular"
>       }
>     ],
>     "points": [
>       {
>         "x": 190,
>         "y": 22.192982
>       },
>       {
>         "x": 376,
>         "y": 22.192982
>       },
>       {
>         "x": 376,
>         "y": 120.4386
>       },
>       {
>         "x": 190,
>         "y": 120.4386
>       }
>     ],
>     "state": "UNUSED",
>     "contentPath": "/content/ffx/print-authoring/en/newsholes/FNZ/DPT/2016/05/30/test_pages/q001/newshole-63019301",
>     "assetId": null   },

然后我想阅读"state""contentPath"并映射它们。 目前我使用的是静态源:

  

val nhfeeder = jsonFile(" shapes-data.json")

使用as,

  

.feed(nhfeeder)

这是一个静态来源,所以我想要一个可以直接从jsonurl读取的自定义馈线,并做必要的事情。

2 个答案:

答案 0 :(得分:0)

Gatling documentation中,您可以在“JSON馈线”部分找到解决方案

val jsonUrlFeeder = jsonUrl("http://me.com/foo.json")

答案 1 :(得分:0)

你唯一需要改变的是让你的JSON的Root元素成为一个数组。 如果不这样做,将导致:#

java.lang.IllegalArgumentException: Root element of JSON feeder file isn't an array

您的JSON结构应该是:

[ 
    {"box": {
       "x": 0,
       "y": 0,
       "width": 186,
       "height": 10.526316
     },
     "type": "Header"
    },
    {
     "box": {
       "x": 0,
       "y": 0,
       "width": 0,
       "height": 0
     },
     "type": "Regular"
    },
    {
     "box": {
       "x": 0,
       "y": 14.035088,
       "width": 43.333332,
       "height": 3.508772
     },
     "type": "Regular"
    }
]

从现在开始,当您创建Feedr时:

val nhfeeder = jsonUrl("http://url-to-json/shapes-data.json").records

你现在有一个在会话表达中使用时会提供值的馈线,如:

scenario("my-scenario")
.foreach(nhfeeder, "shape", "index") {
 exec(
  http("calculate-for-shape-{index}")
  .get("/calculate-area/${shape.box.width}/${shape.box.height}")
 )}

我想象一下你想测试一个计算你的形状面积的函数的场景。这里的重要部分是您可以使用表达式语言来浏览记录。 Json数组中的Json对象。