首页 > 解决方案 > 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 用于空格等。是否有可能以某种方式发布原始字符串?

标签: k6

解决方案


如果您只是自己将主体构造为字符串并将其传递给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"
}


推荐阅读