【性能测试】ChaosTesting(混沌测试)&ChaosBlade(混沌实验工具)(六)-servelt

文章详细介绍了Blade工具如何模拟JavaWeb请求的延迟和异常,包括如何配置参数、使用示例以及常见问题的解决方案。此外,还涉及server模式的web服务暴露和操作,如启动、停止和实验状态查询。
摘要由CSDN通过智能技术生成

7. servelt接口规范

7.0 创建servelt

        blade create servlet

7.0.1 介绍

Servlet 是 Java 的 web 的接口规范,Java web 服务器都遵循此规范实现。本场景主要模拟 Java Web 请求延迟、异常场景。

  1. [blade create servlet delay](blade create servlet delay.md) 请求延迟
  2. [blade create servlet throwCustomException](blade create servlet throwCustomException.md) 请求异常

7.0.2 参数

servlet 通用参数
–effect-count string: 影响的请求条数
–effect-percent string: 影响的请求百分比
–method string: HTTP 请求类型, 例如: GET, POST, or PUT.
–pathinfo string: 已废弃
–pid string: java进程号
–process string: java进程名
–querystring string: 请求参数,例如http://localhost:8080/dubbodemo/async?name=friend&timeout=2000 中 querystring的值是 name=friend&timeout=2000
–requestpath string: 请求 URI,不包含 Context 部分,例如 http://localhost:8080/dubbodemo/async?name=friend&timeout=2000,则 requestpath 的值是 /async,注意要带 /
–servletpath string: 已废弃

7.0.3 常见问题

Q: 下发规则不生效

A: 在命令后添加 --debug,然后触发业务请求,查看应用进程用户目录下 logs/chaosblade/chaosblade.log 日志,如下:

2019-12-09 21:15:12 DEBUG servlet matchers: {"matchers":{"querystring":"name=bobo","servletpath":"/servlet/path","method":"GET","requestpath":"/servlet/path"}}
2019-12-09 21:15:12 INFO  Match rule: {"action":{"name":"delay"},"actionName":"delay","matcher":{"matchers":{}},"target":"servlet"}

servlet matchers 日志表示获取应用的匹配数据,Match rule 表示和下发的命令匹配

7.1 Java web 请求延迟

        blade create servlet delay

7.1.1 参数

以下是此场景特有参数,通用参数详见:[blade create servlet](blade create servlet.md)
–time string: 延迟时间,单位是毫秒,必填项
–offset string: 延迟上下浮动时间,例如 --time 3000 --offset 1000,延迟时间的取值范围是 2000-4000 毫秒

7.1.2 案例

访问 http://localhost:8080/dubbodemo/servlet/path?name=bob 请求延迟 3 秒,影响 2 条请求

blade c servlet delay --time 3000 --requestpath /servlet/path --effect-count 2

{"code":200,"success":true,"result":"154c866919172119"}

访问请求进行验证。
请求参数是 name=family,延迟 2 秒,延迟时间上下浮动 1 秒,影响范围是 50% 的请求,同时开启 debug 日志用于排查问题,命令如下:

blade c servlet delay --time 2000 --offset 1000 --querystring name=family --effect-percent 50 --debug

{"code":200,"success":true,"result":"49236d2406d168f4"}

监控 应用进程用户目录/logs/chaosblade/chaosblade.log 日志
在这里插入图片描述
可以看到下发了 create 指令并开启 debug 日志。 请求两次 http://localhost:8080/dubbodemo/servlet/path?name=bob ,由于参数 querystring 和下发的命令不匹配,所以没有生效 随后请求两次 http://localhost:8080/dubbodemo/servlet/path?name=family,第一次打印了 Match rule 日志,说明匹配成功,延迟生效;第二次打印了 limited by,说明匹配成功,但是由于 effect-percent 参数的限制,所以场景被限制,此请求没有发生延迟

7.2 Java web 请求异常

        blade create servlet throwCustomException

7.2.1 参数

以下是此场景特有参数,通用参数详见:[blade create servlet](blade create servlet.md)
–exception string: 异常类,带全包名,必须继承 java.lang.Exception 或 java.lang.Exception 本身
–exception-message string: 指定异常类信息,默认值是 chaosblade-mock-exception

7.2.2 案例

访问 http://localhost:8080/dubbodemo/hello?code=1 请求异常,影响 3 条请求

blade c servlet throwCustomException --exception org.springframework.beans.BeansException --exception-message mock-beans-exception --requestpath /hello --effect-count 3

{"code":200,"success":true,"result":"d4a63f4f59f76f4a"}

访问请求进行验证。
在这里插入图片描述

查看日志,通过日志也可以看出
在这里插入图片描述

8. 其他

8.1 server

        blade server
后台启动 blade,会暴露出 web 服务,上层可通过 http 调用

8.1.1 介绍

在 server 模式下,blade 程序会对外暴露 web 服务,上层可通过 http 请求调用,请求格式是 chaosblade?cmd=具体命令,例如执行 CPU 满载,则请求是 chaosblade?cmd=create%20cpu%20fullload

8.1.2 命令

start: 启动 server 模式, 暴露 web 服务
stop: 停止 server 模式, 关闭 web 服务

8.1.3 start 命令参数

-p, --port string 服务端口号,默认是 9526

8.1.4 案例

# 启动 server 模式,服务端口是 8080
blade server start --port 8080
success, listening on 8080

# 触发 CPU 负载 50% 场景
curl "http://xxx.xxx.xxx.xxx:8080/chaosblade?cmd=create%20cpu%20load%20--cpu-percent%2050"

{"code":200,"success":true,"result":"e08a64a9af02c393"}

# 销毁实验场景
curl "http://xxx.xxx.xxx.xxx:8080/chaosblade?cmd=destroy%20e08a64a9af02c393"

# 停止 blade server
blade server stop

{"code":200,"success":true,"result":"pid is 12619"}

8.1.5 常见问题

Q: {“code”:605,“success”:false,“error”:“the chaosblade has been started. If you want to stop it, you can execute blade server stop command”}
A: 服务已经启动

Q: {“code”:500,“success”:false,“error”:“time=“2019-09-25T11:36:28.321495762+08:00” level=error msg=“start blade server error, listen tcp :8080: bind: address already in use”\n”}
A: 端口已被占用

8.2 status

        blade status
查询混沌实验和混沌实验环境状态

8.2.1 介绍

查询混沌实验和混沌实验环境状态,可通过创建的混沌实验的 uid 或命令类型来查询混沌实验。 status 可以简写为 s,即 blade status 可以简写为 blade s

8.2.2 参数

–asc bool: 默认值为 false,按 CreateTime 进行降序排序
–limit string: 查询实验数目限制,支持 OFFSET 子句,例如:limit 4,3 就表示从位置5开始,返回后3项
–status string: 实验状态,create 类型支持 Created|Success|Error|Destroyed 状态,prepare 类型支持 Created|Running|Error|Revoked 状态
–target string: 实验目标,例如:dubbo
–type string: 命令类型,attach|create|destroy|detach
–uid string: prepare 或 experiment 的 uid
-h, --help: 查看 create 命令帮助
可使用的父命令参数
-d, --debug: 设置 DEBUG 执行模式

8.2.3 案例

# 查看 status 命令帮助文档
blade status -h

# 查询 uid 为 4c6b4a3fc313e1d4 的实验信息
blade status 4c6b4a3fc313e1d4
{
        "code": 200,
        "success": true,
        "result": {
            "Uid": "4c6b4a3fc313e1d4",
            "Command": "cpu",
            "SubCommand": "fullload",
            "Flag": " --cpu-percent=60",
            "Status": "Destroyed",
            "Error": "",
            "CreateTime": "2020-01-14T14:09:49.152708+08:00",
            "UpdateTime": "2020-01-14T14:10:45.605888+08:00"
        }
}

# 查询 create 类型命令的实验信息
blade status --type create
{
        "code": 200,
        "success": true,
        "result": [
            {
                "Uid": "4c6b4a3fc313e1d4",
                "Command": "cpu",
                "SubCommand": "fullload",
                "Flag": " --cpu-percent=60",
                "Status": "Destroyed",
                "Error": "",
                "CreateTime": "2020-01-14T14:09:49.152708+08:00",
                "UpdateTime": "2020-01-14T14:10:45.605888+08:00"
            }
        ]
}

# 查询 prepare 类型命令的实验信息
blade status --type prepare
{
        "code": 200,
        "success": true,
        "result": [
                {
                        "Uid": "e669d57f079a00cc",
                        "ProgramType": "jvm",
                        "Process": "dubbo.consumer",
                        "Port": "59688",
                        "Status": "Running",
                        "Error": "",
                        "CreateTime": "2019-03-29T16:19:37.284579975+08:00",
                        "UpdateTime": "2019-03-29T17:05:14.183382945+08:00"
                }
        ]
}

8.2.4 常见问题

Q: {“code”:406,“success”:false,“error”:“data not found”}
A: 查询的实验不存在,可能是数据文件 chaosblade.dat 丢失,这时如果需要停止实验,需要手工停止 blade 进程

8.3 打印 blade 工具版本信息

        blade version

8.3.1 介绍

打印 blade 工具版本信息。 version 可以简写为 v,即 blade version 可以简写为 blade v。

8.3.2 参数

-h, --help: 查看 create 命令帮助
可使用的父命令参数
-d, --debug: 设置 DEBUG 执行模式

8.3.3 案例

# 查看 blade 工具版本信息
blade version
version: 0.4.0  # 版本
env: Darwin Kernel Version 19.0.0: Wed Sep 25 20:18:50 PDT 2019; root:xnu-6153.11.26~2/RELEASE_X86_64 x86_64    # 环境信息
build-time: Thu Dec  5 14:29:44 CST 2019    # 构建时间
  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值