Loading...
Loading...
Compare original and translation side by side
What am I trying to learn?
├─ Can my system handle expected traffic? → LOAD TEST
├─ What's the maximum capacity? → STRESS TEST
├─ Will it stay stable over time? → SOAK TEST
└─ Can it handle traffic spikes? → SPIKE TESTreferences/testing-types.md我想要了解什么?
├─ 我的系统能否处理预期流量? → 负载测试
├─ 系统的最大容量是多少? → 压力测试
├─ 系统能否长期保持稳定? → 浸泡测试
└─ 系统能否应对流量尖峰? → 尖峰测试references/testing-types.mdbrew install k6 # macOS
sudo apt-get install k6 # Linuximport http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m', target: 20 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}k6 run script.jsexamples/k6/brew install k6 # macOS
sudo apt-get install k6 # Linuximport http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m', target: 20 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}k6 run script.jsexamples/k6/pip install locustfrom locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 3)
host = "https://api.example.com"
@task(3)
def view_products(self):
self.client.get("/products")
@task(1)
def view_product_detail(self):
self.client.get("/products/123")locust -f locustfile.py --headless -u 100 -r 10 --run-time 10mexamples/locust/pip install locustfrom locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 3)
host = "https://api.example.com"
@task(3)
def view_products(self):
self.client.get("/products")
@task(1)
def view_product_detail(self):
self.client.get("/products/123")locust -f locustfile.py --headless -u 100 -r 10 --run-time 10mexamples/locust/| Symptom | Profiling Type | Tool |
|---|---|---|
| High CPU (>70%) | CPU Profiling | py-spy, pprof, DevTools |
| Memory growing | Memory Profiling | memory_profiler, pprof heap |
| Slow response, low CPU | I/O Profiling | Query logs, pprof block |
| 症状 | 分析类型 | 工具 |
|---|---|---|
| CPU使用率高(>70%) | CPU分析 | py-spy, pprof, DevTools |
| 内存持续增长 | 内存分析 | memory_profiler, pprof heap |
| 响应缓慢但CPU使用率低 | I/O分析 | 查询日志, pprof block |
pip install py-spypip install py-spy
**Memory Profiling:**
```python
from memory_profiler import profile
@profile
def my_function():
a = [1] * (10 ** 6)
return a
**内存分析:**
```python
from memory_profiler import profile
@profile
def my_function():
a = [1] * (10 ** 6)
return aundefinedundefinedimport (
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
startApp()
}undefinedimport (
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
startApp()
}undefinedundefinedundefinednode --inspect app.jsnode --inspect app.js
**clinic.js (Node.js):**
```bash
npm install -g clinic
clinic doctor -- node app.jsreferences/profiling-guide.mdexamples/profiling/
**clinic.js(Node.js):**
```bash
npm install -g clinic
clinic doctor -- node app.jsreferences/profiling-guide.mdexamples/profiling/import redis
r = redis.Redis()
def get_cached_data(key, fn, ttl=300):
cached = r.get(key)
if cached:
return json.loads(cached)
data = fn()
r.setex(key, ttl, json.dumps(data))
return dataimport redis
r = redis.Redis()
def get_cached_data(key, fn, ttl=300):
cached = r.get(key)
if cached:
return json.loads(cached)
data = fn()
r.setex(key, ttl, json.dumps(data))
return dataundefinedundefined
**Indexing:**
```sql
CREATE INDEX idx_users_email ON users(email);
**索引优化:**
```sql
CREATE INDEX idx_users_email ON users(email);app.get('/api/products', async (req, res) => {
const { cursor, limit = 20 } = req.query;
const products = await db.query(
'SELECT * FROM products WHERE id > ? ORDER BY id LIMIT ?',
[cursor || 0, limit]
);
res.json({
data: products,
next_cursor: products[products.length - 1]?.id,
});
});app.get('/api/products', async (req, res) => {
const { cursor, limit = 20 } = req.query;
const products = await db.query(
'SELECT * FROM products WHERE id > ? ORDER BY id LIMIT ?',
[cursor || 0, limit]
);
res.json({
data: products,
next_cursor: products[products.length - 1]?.id,
});
});references/optimization-strategies.mdreferences/frontend-performance.mdreferences/optimization-strategies.mdreferences/frontend-performance.md| Service Type | p95 Latency | p99 Latency | Availability |
|---|---|---|---|
| User-Facing API | < 200ms | < 500ms | 99.9% |
| Internal API | < 100ms | < 300ms | 99.5% |
| Database Query | < 50ms | < 100ms | 99.99% |
| Background Job | < 5s | < 10s | 99% |
| Real-time API | < 50ms | < 100ms | 99.95% |
| 服务类型 | p95延迟 | p99延迟 | 可用性 |
|---|---|---|---|
| 面向用户的API | < 200ms | < 500ms | 99.9% |
| 内部API | < 100ms | < 300ms | 99.5% |
| 数据库查询 | < 50ms | < 100ms | 99.99% |
| 后台作业 | < 5s | < 10s | 99% |
| 实时API | < 50ms | < 100ms | 99.95% |
references/slo-framework.mdreferences/slo-framework.mdname: performance_engineering
on:
pull_request:
branches: [main]
jobs:
load-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install k6
run: |
curl https://github.com/grafana/k6/releases/download/v0.48.0/k6-v0.48.0-linux-amd64.tar.gz -L | tar xvz
sudo mv k6-v0.48.0-linux-amd64/k6 /usr/local/bin/
- name: Run load test
run: k6 run tests/load/api-test.js// k6 test with thresholds (fail build if violated)
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};name: performance_engineering
on:
pull_request:
branches: [main]
jobs:
load-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install k6
run: |
curl https://github.com/grafana/k6/releases/download/v0.48.0/k6-v0.48.0-linux-amd64.tar.gz -L | tar xvz
sudo mv k6-v0.48.0-linux-amd64/k6 /usr/local/bin/
- name: Run load test
run: k6 run tests/load/api-test.js// k6测试阈值(违反则构建失败)
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};references/testing-types.mdreferences/profiling-guide.mdreferences/testing-types.mdreferences/profiling-guide.mdreferences/testing-types.mdreferences/profiling-guide.mdreferences/optimization-strategies.mdreferences/frontend-performance.mdreferences/slo-framework.mdreferences/benchmarking.mdexamples/k6/examples/locust/examples/profiling/examples/optimization/references/testing-types.mdreferences/profiling-guide.mdreferences/optimization-strategies.mdreferences/frontend-performance.mdreferences/slo-framework.mdreferences/benchmarking.mdexamples/k6/examples/locust/examples/profiling/examples/optimization/testing-strategiesbuilding-ci-pipelinesinfrastructure-as-codetesting-strategiesbuilding-ci-pipelinesinfrastructure-as-codeKaynak: k6 Methodology & The Art of Capacity Planning
| Aşama | Doğrulama |
|---|---|
| 1 | Test verisi (Database seed) yeterli hacimde mi? |
| 2 | Load Generator (Test makinesi) CPU darboğazına girdi mi? (False negative riski). |
| 3 | 3rd party API'lar (Stripe, Twilio) mock'landı mı? (Masraf ve ban riski). |
| 步骤 | 验证内容 |
|---|---|
| 1 | 测试数据(数据库种子数据)是否足够? |
| 2 | 负载生成器(测试机器)是否出现CPU瓶颈?(存在假阴性风险) |
| 3 | 第三方API(Stripe、Twilio)是否已被模拟?(存在成本和封禁风险) |