哪个网站可以看免费的电视剧,代码运行框wordpress,电子产品商务网站模板,北京百度seo推广引言
在编写 HTTP 服务的过程中#xff0c;集成测试 1 是保证程序正确性的重要一环#xff0c;如下图所示#xff0c;其基本的流程就是不断向服务发起请求然后校验响应的状态和数据等#xff1a; 为大量的 API 和用例编写测试是一件繁琐的工作#xff0c;而 Loadgen 2 正…引言
在编写 HTTP 服务的过程中集成测试 1 是保证程序正确性的重要一环如下图所示其基本的流程就是不断向服务发起请求然后校验响应的状态和数据等 为大量的 API 和用例编写测试是一件繁琐的工作而 Loadgen 2 正是为了简化这一过程而设计的。
一个简单的测试
假定我们在 127.0.0.1:9100 端口监听了一个 Pizza 3 服务现在我们通过如下配置来测试集合collection的创建
# loadgen.yml
requests:- request:method: PUTurl: http://127.0.0.1:9100/test_create_document然后运行 loadgen -config loadgen.yml
$ loadgen -config loadgen.yml__ ___ _ ___ ___ __ __/ / /___\/_\ / \/ _ \ /__\/\ \ \/ / // ///_\\ / /\ / /_\//_\ / \/ /
/ /__/ \_// _ \/ /_// /_\\//__/ /\ /
\____|___/\_/ \_/___,\____/\__/\_\ \/[LOADGEN] A http load generator and testing suite.
[INF] warmup started
[INF] loadgen is up and running now.
[INF] [PUT] http://127.0.0.1:9100/test_create_document -
[INF] status: 200, error: nil, response: {success:true,collection:test_create_document}
[INF] warmup finished
...为了便于阅读笔者对程序输出进行了简化实际会略有区别 可以看到Loadgen 实际上帮我们做了类似这样的操作
curl -XPUT http://127.0.0.1:9100/test_create_document一些简单的测试
上述示例中我们只测试了创建单个集合但是实际情况下短时间内会有许多请求涌入对于创建大量的集合我们又该如何测试呢
这里就需要用到变量 4 的概念
# loadgen.yml
variables:- name: idtype: sequence
requests:- request:method: PUTurl: http://127.0.0.1:9100/test_create_document_$[[id]]上述配置中我们定义了一个名为 id 的变量sequence 是一个特殊的类型——每次被读取时它的值会递增因此 Loadgen 会不断发起类似这样的请求
curl -XPUT http://127.0.0.1:9100/test_create_document_0
curl -XPUT http://127.0.0.1:9100/test_create_document_1
curl -XPUT http://127.0.0.1:9100/test_create_document_2
...在 Pizza 的日志中也记录了这些请求
$ pizza___ _____ __________ _/ _ \\_ \/ _ / _ / /_\/ /_)/ / /\/\// /\// / //_\\
/ ___/\/ /_ / //\/ //\/ _ \
\/ \____/ /____/____/\_/ \_/[PIZZA] The Next-Gen Real-Time Hybrid Search AI-Native Innovation Engine.
[INFO] Collection test_create_document_0 created
[INFO] Collection test_create_document_1 created
[INFO] Collection test_create_document_2 created
...不那么简单的测试
目前为止我们只是不断的向一个服务“塞”大量的请求但比起发起请求我们常常更关心程序的响应是否符合预期也就是说响应需要满足我们定义的一些条件这可以通过 Loadgen 提供的 断言 5 功能来实现
# loadgen.yml
variables:- name: idtype: sequence
runner:# 检查返回值是否正常assert_error: true# 检查断言是否通过assert_invalid: true
requests:- request:method: PUTurl: http://127.0.0.1:9100/test_create_document_$[[id]]assert:equals:# 注意这里我们故意设置了一个“不正常”的值以迫使断言失败_ctx.response.body_json.success: false在上述配置中我们启用了 Loadgen 的检查然后定义了一个会失败的断言
equals 会校验给定路径 _ctx.response.body_json.success 是否与期望值 false 相等_ctx.response.body_json 表示 JSON 格式的响应体success 表示响应体中该字段对应的值可以用 path.to.nested.key 来访问嵌套的字段
也就是说给定响应体 {success:true,collection:test_create_document}Loadgen 会检查 success 的值是否为 false
$ loadgen -debug -r 1 -d 3 -config loadgen.yml
#0 request, PUT http://127.0.0.1:9100/test_create_document_$[[id]], assertion failed, skiping subsequent requests
[WRN] _ctx.response.body_json.success is not equal to expected value: true
#0 request, PUT http://127.0.0.1:9100/test_create_document_$[[id]], assertion failed, skiping subsequent requests
[WRN] _ctx.response.body_json.success is not equal to expected value: true
#0 request, PUT http://127.0.0.1:9100/test_create_document_$[[id]], assertion failed, skiping subsequent requests
[WRN] _ctx.response.body_json.success is not equal to expected value: true
#0 request, PUT http://127.0.0.1:9100/test_create_document_$[[id]], assertion failed, skiping subsequent requests
[WRN] _ctx.response.body_json.success is not equal to expected value: true上述命令我们使用了 -debug 启用更详细的报错-r 1 -d 3 减少发起的请求数1req/s 持续 3s 还有一个需要注意的细节是 ... is not equal to expected value: true这里报告的是 success 字段实际的值而不是断言中定义的期望值。 可以看到Loadgen 每次请求的断言都失败了不过我们可以通过日志来快速定位出错的原因以便于调试。
更进一步的测试
现在我们创建了大量的空集合是时候向其中添加一些文档document了但是一个首要解决的问题是每次测试创建的集合名称是带有 $[[id]] 这个变量的我们如何知道应该向哪个集合上传数据呢一个可靠的解决方案是借助 Loadgen 的寄存器 6 功能
# loadgen.yml
variables:- name: idtype: sequence
runner:assert_error: trueassert_invalid: true
requests:- request:method: PUTurl: http://127.0.0.1:9100/test_create_document_$[[id]]assert:equals:_ctx.response.body_json.success: trueregister:# 把响应体的 collection 字段赋值给 $[[collection]]- collection: _ctx.response.body_json.collection- request:method: POST# 在上个请求创建的集合里添加一个文档url: http://127.0.0.1:9100/$[[collection]]/_docbody: {hello: world}assert:equals:_ctx.response.body_json.result: created上述示例中我们利用动态注册的变量记录了每次测试创建的集合以便于后续请求使用。
最后的优化
为了使我们的配置更加灵活和“便携”我们可以用环境变量来替换一些硬编码的值
# loadgen.yml
variables:- name: idtype: sequence
runner:assert_error: trueassert_invalid: true
requests:- request:method: PUT# 读取 PIZZA_SERVER 这个环境变量url: $[[env.PIZZA_SERVER]]/test_create_document_$[[id]]assert:equals:_ctx.response.body_json.success: trueregister:- collection: _ctx.response.body_json.collection- request:method: POSTurl: $[[env.PIZZA_SERVER]]/$[[collection]]/_docbody: {hello: world}assert:equals:_ctx.response.body_json.result: created这样就可以通过
PIZZA_SERVERhttp://127.0.0.1:9101 loadgen -config loadgen.yml在不同的 Pizza 服务上运行测试。 https://en.wikipedia.org/wiki/Integration_testing ↩︎ https://www.infinilabs.com/docs/latest/gateway/getting-started/benchmark ↩︎ https://www.infinilabs.com/en/docs/latest/pizza ↩︎ https://www.infinilabs.com/docs/latest/gateway/getting-started/benchmark#变量的使用 ↩︎ https://www.infinilabs.com/docs/latest/gateway/getting-started/benchmark#返回值判断 ↩︎ https://www.infinilabs.com/docs/latest/gateway/getting-started/benchmark#动态变量注册 ↩︎
文章转载自: http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.xkjrq.cn.gov.cn.xkjrq.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn