{"id":28600946,"url":"https://github.com/oldthreefeng/stress","last_synced_at":"2025-06-11T14:39:30.420Z","repository":{"id":57559626,"uuid":"284667944","full_name":"oldthreefeng/stress","owner":"oldthreefeng","description":"压力测试, 实现参考https://github.com/link1st/go-stress-testing, 使用cobra重写, 支持docker","archived":false,"fork":false,"pushed_at":"2020-12-29T18:39:26.000Z","size":189,"stargazers_count":19,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-06-20T15:58:07.409Z","etag":null,"topics":["cobra","go","golang","stress-testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oldthreefeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-03T10:14:02.000Z","updated_at":"2024-05-22T09:00:58.000Z","dependencies_parsed_at":"2022-08-28T10:20:21.003Z","dependency_job_id":null,"html_url":"https://github.com/oldthreefeng/stress","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oldthreefeng%2Fstress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oldthreefeng%2Fstress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oldthreefeng%2Fstress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oldthreefeng%2Fstress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oldthreefeng","download_url":"https://codeload.github.com/oldthreefeng/stress/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oldthreefeng%2Fstress/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259280932,"owners_count":22833476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cobra","go","golang","stress-testing"],"created_at":"2025-06-11T14:39:29.496Z","updated_at":"2025-06-11T14:39:30.392Z","avatar_url":"https://github.com/oldthreefeng.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stress\n\ngo 实现的压测工具，每个用户用一个协程的方式模拟，最大限度的利用 CPU 资源. [下载](https://github.com/oldthreefeng/stress/releases)\n\n### 1.1 安装\n\n使用make安装. 建议使用make安装.\n\n```shell\n$ git clone https://github.com/oldthreefeng/stress\n$ make linux\n```\n\n使用`golang build`\n\n```shell\n$ git clone https://github.com/oldthreefeng/stress\n$ go build -o stress main.go\n```\n\n使用脚本项目脚本构建 `build.sh`\n\n```shell \n$ git clone https://github.com/oldthreefeng/stress\n$ sh build.sh v1.0.0\n```\n\n### 1.2 项目体验\n\n\n参数说明:\n\n`-c` 表示并发数 默认为 1\n\n`-n` 每个并发执行请求的次数， 默认为 1 ，总请求的次数 = 并发数 `*` 每个并发执行请求的次数\n\n`-u` 需要压测的地址 example: http://www.baidu.com/\n\n```shell\n\n# 运行 以linux为示例\n./stress -c 1 -n 100 -u https://www.baidu.com/\n\n```\n\nor use in docker  ,高并发建议开启多docker。在性能差的机子上，运行效果不好。\n\n```shell\n$ docker pull louisehong/stress \ndocker run --rm louisehong/stress -c 10 -n 10 -u https://www.baidu.com/ \n\n 开始启动  并发数:10 请求数:10 请求参数: \nrequest:\n form:http \n url:https://www.baidu.com/ \n method:GET \n headers:map[] \n data: \n verify:statusCode \n timeout:30s \n debug:false \n\n\n\n─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬────────\n 耗时│ 并发数│ 成功数│ 失败数│   qps  │最长耗时│最短耗时│平均耗时│ 错误码\n─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼────────\n   1s│     10│     99│      0│  176.48│   82.88│   40.26│    5.67│200:99\n   2s│     10│    100│      0│  149.89│ 1061.94│   40.26│    6.67│200:100\n\n\n*************************  结果 stat  ****************************\n处理协程数量: 10\n请求总数（并发数*请求数 -c * -n）: 100 总请求时间: 1.557 秒 successNum: 100 failureNum: 0\n*************************  结果 end   ****************************\n```\n\n- 压测结果展示\n\n执行以后，终端每秒钟都会输出一次结果，压测完成以后输出执行的压测结果\n\n压测结果展示:\n\n```\n\n─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬────────\n 耗时│ 并发数 │ 成功数│ 失败数 │   qps  │最长耗时 │最短耗时│平均耗时 │ 错误码\n─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼────────\n   1s│      1│      8│      0│    8.09│  133.16│  110.98│  123.56│200:8\n   2s│      1│     15│      0│    8.02│  138.74│  110.98│  124.61│200:15\n   3s│      1│     23│      0│    7.80│  220.43│  110.98│  128.18│200:23\n   4s│      1│     31│      0│    7.83│  220.43│  110.23│  127.67│200:31\n   5s│      1│     39│      0│    7.81│  220.43│  110.23│  128.03│200:39\n   6s│      1│     46│      0│    7.72│  220.43│  110.23│  129.59│200:46\n   7s│      1│     54│      0│    7.79│  220.43│  110.23│  128.42│200:54\n   8s│      1│     62│      0│    7.81│  220.43│  110.23│  128.09│200:62\n   9s│      1│     70│      0│    7.79│  220.43│  110.23│  128.33│200:70\n  10s│      1│     78│      0│    7.82│  220.43│  106.47│  127.85│200:78\n  11s│      1│     84│      0│    7.64│  371.02│  106.47│  130.96│200:84\n  12s│      1│     91│      0│    7.63│  371.02│  106.47│  131.02│200:91\n  13s│      1│     99│      0│    7.66│  371.02│  106.47│  130.54│200:99\n  13s│      1│    100│      0│    7.66│  371.02│  106.47│  130.52│200:100\n\n\n*************************  结果 stat  ****************************\n处理协程数量: 1\n请求总数: 100 总请求时间: 13.055 秒 successNum: 100 failureNum: 0\n*************************  结果 end   ****************************\n\n```\n\n参数解释:\n\n**耗时**: 程序运行耗时。程序每秒钟输出一次压测结果\n\n**并发数**: 并发数，启动的协程数\n\n**成功数**: 压测中，请求成功的数量\n\n**失败数**: 压测中，请求失败的数量\n\n**qps**: 当前压测的QPS(每秒钟处理请求数量)\n\n**最长耗时**: 压测中，单个请求最长的响应时长\n\n**最短耗时**: 压测中，单个请求最短的响应时长\n\n**平均耗时**: 压测中，单个请求平均的响应时长\n\n**错误码**: 压测中，接口返回的 code码:返回次数的集合\n\n\n### 2.1 压测是什么\n\n压测，即压力测试，是确立系统稳定性的一种测试方法，通常在系统正常运作范围之外进行，以考察其功能极限和隐患。\n\n主要检测服务器的承受能力，包括用户承受能力（多少用户同时玩基本不影响质量）、流量承受等。\n\n- 压测的目的就是通过压测(模拟真实用户的行为)，测算出机器的性能(单台机器的QPS)，从而推算出系统在承受指定用户数(100W)时，需要多少机器能支撑得住\n- 压测是在上线前为了应对未来可能达到的用户数量的一次预估(提前演练)，压测以后通过优化程序的性能或准备充足的机器，来保证用户的体验。\n\n| 压测类型 |   解释  |\n| :----   | :---- |\n| 压力测试(Stress Testing)          |  也称之为强度测试，测试一个系统的最大抗压能力，在强负载(大数据、高并发)的情况下，测试系统所能承受的最大压力，预估系统的瓶颈    |\n| 并发测试(Concurrency Testing)     |  通过模拟很多用户同一时刻访问系统或对系统某一个功能进行操作，来测试系统的性能，从中发现问题(并发读写、线程控制、资源争抢)      |\n| 耐久性测试(Configuration Testing) |  通过对系统在大负荷的条件下长时间运行，测试系统、机器的长时间运行下的状况,从中发现问题(内存泄漏、数据库连接池不释放、资源不回收)     |\n\n### 2.2 如何计算压测指标\n\n- 压测我们需要有目的性的压测，这次压测我们需要达到什么目标(如:单台机器的性能为 100QPS?网站能同时满足100W人同时在线)\n- 可以通过以下计算方法来进行计算:\n- 压测原则:每天80%的访问量集中在20%的时间里，这20%的时间就叫做峰值\n- 公式: ( 总PV数`*`80% ) / ( 每天的秒数`*`20% ) = 峰值时间每秒钟请求数(QPS)\n- 机器: 峰值时间每秒钟请求数(QPS) / 单台机器的QPS = 需要的机器的数量\n\n- 假设:网站每天的用户数(100W)，每天的用户的访问量约为3000W PV，这台机器的需要多少QPS?\n\u003e ( 30000000\\*0.8 ) / (86400 * 0.2) ≈ 1389 (QPS)\n\u003e\n\n\n### 3.1 用法\n\n```shell\n$ ./stress -h\nstress is a test cli for http and websocket stress written by golang, \ngo 实现的压测工具，每个用户用一个协程的方式模拟，最大限度的利用 CPU 资源\n\nUsage:\n  stress [flags]\n  stress [command]\n\nExamples:\n        \n        # stress curl file to test \n        stress -f utils/curl.txt\n\n        # stress curl file read from stdin \n        cat utils/curl.txt | stress -f -\n\n        # stress concurrency 10 \u0026 10 times\n        stress -c 10 -n 10 -f  utils/curl.txt\n\n        # stress cli url\n        stress -c 10 -n 100 -u https://www.baidu.com\n\n\nAvailable Commands:\n  help        Help about any command\n  version     stress version\n\nFlags:\n  -c, --concurrency uint    并发数 (default 1)\n      --config string       config file for stress (default is $HOME/.stress.yaml)\n      --data string         http post data\n  -d, --debug               debug 模式\n  -H, --header strings      http headers\n  -h, --help                help for stress\n  -n, --number uint         单协程的请求数 (default 1)\n  -f, --path string         read curl file to build test\n  -u, --requestUrl string   curl文件路径\n  -t, --toggle              Help message for toggle\n  -v, --verify string        verify 验证方法 在server/verify中 http 支持:statusCode、json webSocket支持:json (default \"statusCode\")\n\nUse \"stress [command] --help\" for more information about a command.\n\n```\n\n\n- 使用 curl文件进行压测\n\ncurl是Linux在命令行下的工作的文件传输工具，是一款很强大的http命令行工具。\n\n使用curl文件可以压测使用非GET的请求，支持设置http请求的 method、cookies、header、body等参数\n\n\n**I:** chrome 浏览器生成 curl文件，打开开发者模式(快捷键F12)，如图所示，生成 curl 在终端执行命令\n![chrome cURL](https://img.mukewang.com/5d60eddd0001f4b016661114.png)\n\n**II:** postman 生成 curl 命令\n![postman cURL](https://img.mukewang.com/5ed79b590001837120581530.png)\n\n生成内容粘贴到项目目录下的**utils/curl.txt**文件中，执行下面命令就可以从curl.txt文件中读取需要压测的内容进行压测了\n\n- 支持多步压力测试\n\n目前使用的方法是按行来分割每个请求. \n\n\n```shell\n$ cat utils/curl.txt\ncurl 'https://mall.youpenglai.com/apis/version' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3' --compressed -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Cache-Control: max-age=0' -H 'TE: Trailers'\ncurl 'https://www.youpenglai.com/v2/sys/server' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3' --compressed -H 'Connection: keep-alive' -H 'Cookie: _ga=GA1.2.462595907.1596072242; Hm_lvt_16789552d8e4ee2108c29cc8197bfe19=1596077258; _gid=GA1.2.1014297080.1596441772; JSESSIONID=88EE56D0223D34DD83EB301962B121D1' -H 'Upgrade-Insecure-Requests: 1' -H 'Cache-Control: max-age=0'\n```\n\n支持从`stdin`读取`curl`请求. 从`stdin`读取的内容会保存在`/tmp/curl.tmp`\n\n```shell\n$ cat utils/curl.txt  | ./stress -f -\n```\n\n命令行示例. \n\n```shell\n$ ./stress -c 1 -n 1 -d -u 'https://page.aliyun.com/delivery/plan/list' \\\n  -H 'authority: page.aliyun.com' \\\n  -H 'accept: application/json, text/plain, */*' \\\n  -H 'content-type: application/x-www-form-urlencoded' \\\n  -H 'origin: https://cn.aliyun.com' \\\n  -H 'sec-fetch-site: same-site' \\\n  -H 'sec-fetch-mode: cors' \\\n  -H 'sec-fetch-dest: empty' \\\n  -H 'referer: https://cn.aliyun.com/' \\\n  -H 'accept-language: zh-CN,zh;q=0.9' \\\n  -H 'cookie: aliyun_choice=CN; JSESSIONID=J8866281-CKCFJ4BUZ7GDO9V89YBW1-KJ3J5V9K-GYUW7; maliyun_temporary_console0=1AbLByOMHeZe3G41KYd5WWZvrM%2BGErkaLcWfBbgveKA9ifboArprPASvFUUfhwHtt44qsDwVqMk8Wkdr1F5LccYk2mPCZJiXb0q%2Bllj5u3SQGQurtyPqnG489y%2FkoA%2FEvOwsXJTvXTFQPK%2BGJD4FJg%3D%3D; cna=L3Q5F8cHDGgCAXL3r8fEZtdU; isg=BFNThsmSCcgX-sUcc5Jo2s2T4tF9COfKYi8g9wVwr3KphHMmjdh3GrHFvPTqJD_C; l=eBaceXLnQGBjstRJBOfwPurza77OSIRAguPzaNbMiT5POw1B5WAlWZbqyNY6C3GVh6lwR37EODnaBeYBc3K-nxvOu9eFfGMmn' \\\n  --data 'adPlanQueryParam=%7B%22adZone%22%3A%7B%22positionList%22%3A%5B%7B%22positionId%22%3A83%7D%5D%7D%2C%22requestId%22%3A%2217958651-f205-44c7-ad5d-f8af92a6217a%22%7D' \\\n  --compressed\n```\n\nlike curl cmd , use `--data-raw` is the same with `--data`,  when use `--compressed`,  only support command line and `gzip`. \nfor curl file. the Header is in it. use `--compressed`, just  do this `req.Header.Add(\"Accept-Encoding\", \"gzip\")`. \n\n```shell\n$ ./stress -c 1 -n 1 -d -u 'https://page.aliyun.com/delivery/plan/list' \\\n  -H 'authority: page.aliyun.com' \\\n  -H 'accept: application/json, text/plain, */*' \\\n  -H 'content-type: application/x-www-form-urlencoded' \\\n  -H 'origin: https://cn.aliyun.com' \\\n  -H 'sec-fetch-site: same-site' \\\n  -H 'sec-fetch-mode: cors' \\\n  -H 'sec-fetch-dest: empty' \\\n  -H 'referer: https://cn.aliyun.com/' \\\n  -H 'accept-language: zh-CN,zh;q=0.9' \\\n  -H 'cookie: aliyun_choice=CN; JSESSIONID=J8866281-CKCFJ4BUZ7GDO9V89YBW1-KJ3J5V9K-GYUW7; maliyun_temporary_console0=1AbLByOMHeZe3G41KYd5WWZvrM%2BGErkaLcWfBbgveKA9ifboArprPASvFUUfhwHtt44qsDwVqMk8Wkdr1F5LccYk2mPCZJiXb0q%2Bllj5u3SQGQurtyPqnG489y%2FkoA%2FEvOwsXJTvXTFQPK%2BGJD4FJg%3D%3D; cna=L3Q5F8cHDGgCAXL3r8fEZtdU; isg=BFNThsmSCcgX-sUcc5Jo2s2T4tF9COfKYi8g9wVwr3KphHMmjdh3GrHFvPTqJD_C; l=eBaceXLnQGBjstRJBOfwPurza77OSIRAguPzaNbMiT5POw1B5WAlWZbqyNY6C3GVh6lwR37EODnaBeYBc3K-nxvOu9eFfGMmn' \\\n  --data-raw 'adPlanQueryParam=%7B%22adZone%22%3A%7B%22positionList%22%3A%5B%7B%22positionId%22%3A83%7D%5D%7D%2C%22requestId%22%3A%2217958651-f205-44c7-ad5d-f8af92a6217a%22%7D' \\\n  --compressed\n```\n\n### Docker Use\n\n```\n$ docker pull louisehong/stress\n\n$ docker run --rm   louisehong/stress  -c 1 -n 1 -d -u 'https://page.aliyun.com/delivery/plan/list' \\\n  -H 'authority: page.aliyun.com' \\\n  -H 'accept: application/json, text/plain, */*' \\\n  -H 'content-type: application/x-www-form-urlencoded' \\\n  -H 'origin: https://cn.aliyun.com' \\\n  -H 'sec-fetch-site: same-site' \\\n  -H 'sec-fetch-mode: cors' \\\n  -H 'sec-fetch-dest: empty' \\\n  -H 'referer: https://cn.aliyun.com/' \\\n  -H 'accept-language: zh-CN,zh;q=0.9' \\\n  -H 'cookie: aliyun_choice=CN; JSESSIONID=J8866281-CKCFJ4BUZ7GDO9V89YBW1-KJ3J5V9K-GYUW7; maliyun_temporary_console0=1AbLByOMHeZe3G41KYd5WWZvrM%2BGErkaLcWfBbgveKA9ifboArprPASvFUUfhwHtt44qsDwVqMk8Wkdr1F5LccYk2mPCZJiXb0q%2Bllj5u3SQGQurtyPqnG489y%2FkoA%2FEvOwsXJTvXTFQPK%2BGJD4FJg%3D%3D; cna=L3Q5F8cHDGgCAXL3r8fEZtdU; isg=BFNThsmSCcgX-sUcc5Jo2s2T4tF9COfKYi8g9wVwr3KphHMmjdh3GrHFvPTqJD_C; l=eBaceXLnQGBjstRJBOfwPurza77OSIRAguPzaNbMiT5POw1B5WAlWZbqyNY6C3GVh6lwR37EODnaBeYBc3K-nxvOu9eFfGMmn' \\\n  --data-raw 'adPlanQueryParam=%7B%22adZone%22%3A%7B%22positionList%22%3A%5B%7B%22positionId%22%3A83%7D%5D%7D%2C%22requestId%22%3A%2217958651-f205-44c7-ad5d-f8af92a6217a%22%7D' \\\n  --compressed\n```\n\n- 项目结构\n\n```\n├── build.sh\n├── cmd\n│   ├── dispose.go\n│   ├── root.go\n│   └── version.go\n├── main.go\n├── Makefile\n├── pkg\n│   ├── http_client.go\n│   ├── websocket_client.go\n│   ├── http.go\n│   ├── request.go\n│   ├── statistics.go\n│   ├── var.go\n│   ├── verify_http.go\n│   ├── verify_websocket.go\n│   └── websocket.go\n├── README.md\n└── utils\n    ├── curl.txt\n    ├── utils.go\n    └── utils_test.go\n```\n\n### 4.参考文献\n    \n[性能测试工具](https://testerhome.com/topics/17068)\n\n[性能测试常见名词解释](https://blog.csdn.net/r455678/article/details/53063989)\n\n[性能测试名词解释](https://codeigniter.org.cn/forums/blog-39678-2456.html)\n\n[PV、TPS、QPS是怎么计算出来的？](https://www.zhihu.com/question/21556347)\n\n[超实用压力测试工具－ab工具](https://www.jianshu.com/p/43d04d8baaf7)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foldthreefeng%2Fstress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foldthreefeng%2Fstress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foldthreefeng%2Fstress/lists"}