我想测试的lambda函数需要一个复杂的json,根据我的理解,json需要进入csv文件。我的问题是我尝试了各种方法从csv加载json,但总是得到错误。我不确定这在炮兵中是否可行。我的示例csv看起来像这样。
- 后期数据. csv**
column1 {"个人资料":{"姓名":"irfan","电子邮件":"www.example.com "},"地址":["地址1 ","地址2 "]}{"个人资料":{"姓名":" Tomas ","电子邮件":" www.example.com "},"地址":["地址1","地址2"]}{"个人资料":{"姓名":"Joel","电子邮件":"www.example.com "},"地址":["地址1 ","地址2 "]} irfan@email.com "},"address":["address1","address2"]} { "profile":{"name":"Tomas","email":" tomas@email.com "},"address":["address1","address2"]} { "profile":{"name":"Joel","email":" joel@email.com "},"address":["address1","address2"]}
我只有一列,因为我想要的只是这个json文档作为请求体传递给我的hello world lambda,这样我就可以对它进行负载测试。
下面是我的炮兵脚本文件的内容。
config:
target: "https://api-gateway-load-testing-tst-ap-southeast-2.xxxxxxxxxxx.com"
phases:
-
duration: 5
arrivalRate: 1
defaults:
headers:
x-api-key: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Content-Type: "application/json"
payload:
# path is relative to the location of the test script
path: "post-data.csv"
fields:
- "column1"
order: sequence
delimiter: "~"
skipHeader: true
cast: false
plugins:
cloudwatch:
namespace: "serverless-artillery-loadtest"
scenarios:
- flow:
- post:
url: "/v1/hello-world"
json:
data: {{ column1 }}
当我在json中的键和值周围加上双引号时,我收到错误消息"执行任务时出错:在1579692773908中执行来自1579692773908的加载时遇到错误异常:炮兵已退出,代码为非零:"
有没有办法从csv加载json,让我的hello world lambda函数以json的形式接收请求主体,格式如下:
{"数据":{"个人资料":{"姓名":" irfan ","电子邮件":" www.example.com "},"地址":["地址1","地址2"]}}irfan@email.com"},"address":["address1","address2"]}}
任何帮助都将不胜感激。
2条答案
按热度按时间v2g6jxz61#
我自己设法通过编写自定义JavaScript来加载json有效负载而不是从csv文件来解决这个问题。我使用config.processor沿着beforeScenario钩子来定义我的自定义逻辑。
对于任何可能面临类似问题的人,以下是我的解决方案:
脚本.yml
下面的post-body.js包含了我自定义的JS逻辑。我引入了一个新的txt文件post-data.txt,它基本上取代了我在问题中提到的csv文件,以托管数千行,其中每行都是作为json的请求负载。每次执行一个场景时,都会拾取一个随机的json负载字符串。转换为json对象并作为POST请求的一部分发送。我还使用CloudWatch和InfluxDB来输出结果。
后正文.js
后期数据.txt
高温加热
xriantvc2#
只是想发布一个额外的解决方案,我的工作,将工作,如果你的JSON是相当简单的。问题是围绕引号和逗号在CSV文件时,分配JSON到一个字段。我改变了CSV分隔符从逗号到'|'。我还将JSON双引号改为单引号,以避免被误解为CSV字符串的开始/结束。我的CSV数据行如下所示:
然后在我的场景的处理器中,在解析JSON字段之前,我只需要将所有单引号替换为双引号。
下面是CSV文件的配置:
我会第一个承认它很笨拙,但它足够简单,并且完成了任务。如果您的JSON中有合法的单引号或管道,那么您需要处理它。