---
title: "Chạy k6 (Grafana k6) load test / script HTTP qua proxy tĩnh Việt Nam"
url: https://memory.wiki/6Ki0dcSO
updated: 2026-09-29T00:20:36.238Z
source: "api"
---
# Chạy k6 (Grafana k6) load test / script HTTP qua proxy tĩnh Việt Nam

Grafana k6 là công cụ load test và script HTTP phổ biến. Khi bạn đo API hoặc storefront **nhìn từ phía khách Việt Nam**, gắn **proxy tĩnh** dân cư giúp mọi VU (virtual user) đi ra cùng một IP cố định — thuận tiện whitelist staging và so sánh latency giữa các lần chạy. Bài này hướng dẫn cấu hình proxy cho k6, verify exit IP, và vài pattern script an toàn.

## Khi nào k6 cần proxy tĩnh?

- Staging chỉ cho phép IP nguồn đã khai báo.
- Bạn so sánh p95 giữa hai bản release: IP đổi giữa các run làm nhiễu số liệu mạng.
- Smoke + soak trên endpoint geo-sensitive (giá, locale, cổng thanh toán).
- CI runner ở nước ngoài nhưng traffic phải mang IP VN.

IP xoay phù hợp khi mô phỏng nhiều ISP khác nhau. Với regression cố định, một IP tĩnh dễ đọc log phía server hơn.

## Thông số đã xác nhận

Proxy tĩnh dân cư Việt Nam: **1.000đ/ngày** hoặc **30.000đ/30 ngày**; Viettel / VNPT / FPT; HTTP hoặc SOCKS5; mạng dân cư thật, IP sạch. Chi tiết & [xem bảng giá](https://proxyviet.org/proxy-tinh). Checker: [check-proxy](https://proxyviet.org/check-proxy).

## Cài k6 (tóm tắt)

```bash
# macOS
brew install k6
# hoặc binary từ grafana/k6 releases
k6 version
```

## Biến môi trường

```bash
export PROXY_URL='http://USER:PASS@HOST:PORT'
export EXPECTED_EXIT_IP='x.x.x.x'
# Không commit secret vào repo
```

Smoke ngoài k6:

```bash
curl -x "$PROXY_URL" --max-time 25 https://api.ipify.org
echo
```

## k6 — HTTP proxy qua biến môi trường

k6 tôn trọng `HTTP_PROXY` / `HTTPS_PROXY` (và `NO_PROXY`) cho các request HTTP(S) của script:

```bash
export HTTP_PROXY="$PROXY_URL"
export HTTPS_PROXY="$PROXY_URL"
export NO_PROXY='localhost,127.0.0.1'

k6 run script.js
```

Hoặc truyền một lần:

```bash
HTTPS_PROXY="$PROXY_URL" HTTP_PROXY="$PROXY_URL" k6 run script.js
```

## Script mẫu — verify IP rồi gọi API

```javascript
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
  vus: 5,
  duration: '30s',
  thresholds: {
    http_req_failed: ['rate<0.05'],
    http_req_duration: ['p(95)<3000'],
  },
};

const expected = __ENV.EXPECTED_EXIT_IP;

export function setup() {
  const res = http.get('https://api.ipify.org');
  const ip = String(res.body).trim();
  console.log(`Exit IP: ${ip}`);
  if (expected && ip !== expected) {
    throw new Error(`IP lệch kỳ vọng: ${ip}`);
  }
  return { ip };
}

export default function (data) {
  const res = http.get('https://httpbin.org/get', {
    headers: { 'User-Agent': 'k6-proxyviet-smoke/1.0' },
    timeout: '25s',
  });
  check(res, {
    'status 200': (r) => r.status === 200,
  });
  sleep(1);
}
```

Chạy:

```bash
HTTPS_PROXY="$PROXY_URL" HTTP_PROXY="$PROXY_URL" \
  EXPECTED_EXIT_IP="$EXPECTED_EXIT_IP" \
  k6 run script.js
```

## SOCKS5

Một số bản k6/môi trường hỗ trợ `ALL_PROXY=socks5://USER:PASS@HOST:PORT`. Nếu SOCKS5 auth gặp hạn chế, dùng cổng HTTP của cùng IP dân cư — vẫn đủ cho hầu hết load test HTTP(S).

## Lưu ý khi tăng VU

1. Proxy tĩnh có băng thông và concurrent connection giới hạn theo gói — đừng nhầm k6 “phá” production qua một IP QA.
2. Bắt đầu VUs thấp (1–5), xem error rate và CPU proxy; tăng dần.
3. Tách scenario: smoke (1 VU) verify IP; load thật chỉ khi IP đã khớp.
4. `NO_PROXY` cho metrics push nội bộ / localhost để không đẩy k6 cloud agent qua proxy nếu không cần.
5. Không log full `PROXY_URL` trong stdout CI công khai.

## So sánh nhanh với JMeter

JMeter cấu hình proxy ở HTTP Request Defaults; k6 thường gọn hơn qua env. Cùng nguyên tắc: một IP nguồn cố định + threshold rõ. Nếu team đã có JMeter, có thể giữ song song; bài này tập trung k6.

## Checklist trước khi gắn CI

| Hạng mục | Pass? |
| --- | --- |
| curl qua proxy ra đúng IP panel | |
| `setup()` k6 in đúng IP | |
| Threshold hợp lý (không fail vì mạng) | |
| Secret chỉ trong CI variables | |
| Staging allowlist đã thêm IP | |

## Kết luận

k6 gắn proxy tĩnh chủ yếu qua `HTTP_PROXY`/`HTTPS_PROXY`, verify IP trong `setup()`, rồi chạy threshold như bình thường. Khi cần IP dân cư Việt Nam cố định để load test / smoke geo, [xem bảng giá](https://proxyviet.org/proxy-tinh) (1.000đ/ngày hoặc 30.000đ/30 ngày; Viettel/VNPT/FPT; HTTP/SOCKS5) và xác nhận trên [check-proxy](https://proxyviet.org/check-proxy) trước mỗi pipeline quan trọng.


---

## Themes
- k6 load testing
- static residential proxies
- Vietnam network testing
- CI/CD performance optimization

## Key takeaways
- k6 supports proxy configuration primarily through environment variables like HTTP_PROXY and HTTPS_PROXY.
- Static residential proxies are useful for geo-sensitive endpoints and regression testing where consistent source IPs are required.
- One should use the setup function to verify that the exit IP matches the expected value before running the main test logic.
- Proxy bandwidth and concurrent connection limits must be considered when scaling virtual users to avoid overloading the proxy.
- NO_PROXY should be configured to ensure internal metrics or localhost traffic do not route through the proxy.

## Insights
- Using a static residential proxy in k6 ensures consistent IP identity for staging whitelisting and reliable latency comparisons across test runs.
- The setup function in k6 is an effective place to perform an IP verification check before executing the main load test scenario.
- Proxy performance can become a bottleneck when scaling virtual users, requiring a gradual increase in load to monitor proxy CPU and error rates.

## Open questions / gaps
- How to handle proxy rotation if the static IP becomes unresponsive during a long-duration soak test?
- What are the specific performance overhead metrics when routing high-concurrency traffic through a single residential proxy versus a data center proxy?

