繁体   English   中英

如何编写用于加特林的自定义供稿器?

[英]How to write a custom feeder for gatling json?

我有一个要读取的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”)

用作

.feed(nhfeeder)

这是一个静态源,所以我想要一个可以直接从jsonurl读取并做有需要的自定义Feeder。

加特林文档中,您可以在“ JSON馈送器”部分中找到解决方案

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

您唯一需要更改的就是使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"
    }
]

从这一点开始,当您创建Feeder时:

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对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM