k6 - K6 我如何制作原始(非编码)帖子
问题描述
我正在尝试使用 K6 来加载测试 prometheus pushgateway,它需要以下格式的帖子。
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
每行的末尾必须有一个换行符(最后还有一个额外的换行符) - 但是我似乎只得到 url 编码的字符串,如 %20 用于空格等。是否有可能以某种方式发布原始字符串?
解决方案
如果您只是自己将主体构造为字符串并将其传递给http.post()
,则应按原样发送,无需任何修改。此代码应使用 httpbin.org 说明这一点:
import http from "k6/http";
import crypto from "k6/crypto";
let payload = `http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
`;
export default function (data) {
console.log(crypto.sha256(payload, "hex"));
let resp = http.post("https://httpbin.org/anything", payload);
console.log(crypto.sha256(resp.json().data, "hex"));
console.log(resp.body);
}
它将输出如下内容:
INFO[0000] 773f0d81713fca0663ad7a01135bf674b93b0859854b2248368125af3f070d29
INFO[0001] 773f0d81713fca0663ad7a01135bf674b93b0859854b2248368125af3f070d29
INFO[0001] {
"args": {},
"data": "http_request_duration_seconds_bucket{le=\"0.05\"} 24054\nhttp_request_duration_seconds_bucket{le=\"0.1\"} 33444\nhttp_request_duration_seconds_bucket{le=\"0.2\"} 100392\n",
"files": {},
"form": {},
"headers": {
"Connection": "close",
"Content-Length": "161",
"Host": "httpbin.org",
"User-Agent": "k6/0.23.1 (https://k6.io/)"
},
"json": null,
"method": "POST",
"url": "https://httpbin.org/anything"
}
推荐阅读
- javascript - 如何开玩笑地打印已安装组件的子元素?
- mysql - 2026,SSL 连接错误:未知错误号 - 我的 django 应用程序在将 mysql 从 5 升级到 8 后遇到
- c++ - 如何在我的 C++ 项目中实现多语言支持?
- google-chrome - 带有可移动/可调整大小弹出窗口的 Chrome 扩展
- javascript - 我怎样才能用jquery做一个循环?
- javascript - 所有视频都正确调整大小,但 YouTube 嵌入似乎阻止了它始终有效
- tensorflow - ExampleGen 在生产中
- git - 如何列出所有提交比 HEAD 更多的分支?
- html - 如何正确对齐占据中型及以上设备一半屏幕的引导模式
- python - 如何使用 seaborn violinplot 绘制值的总和(而不是计数)?