infomankit 0.3.23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- infoman/__init__.py +1 -0
- infoman/cli/README.md +378 -0
- infoman/cli/__init__.py +7 -0
- infoman/cli/commands/__init__.py +3 -0
- infoman/cli/commands/init.py +312 -0
- infoman/cli/scaffold.py +634 -0
- infoman/cli/templates/Makefile.template +132 -0
- infoman/cli/templates/app/__init__.py.template +3 -0
- infoman/cli/templates/app/app.py.template +4 -0
- infoman/cli/templates/app/models_base.py.template +18 -0
- infoman/cli/templates/app/models_entity_init.py.template +11 -0
- infoman/cli/templates/app/models_schemas_init.py.template +11 -0
- infoman/cli/templates/app/repository_init.py.template +11 -0
- infoman/cli/templates/app/routers_init.py.template +15 -0
- infoman/cli/templates/app/services_init.py.template +11 -0
- infoman/cli/templates/app/static_index.html.template +39 -0
- infoman/cli/templates/app/static_main.js.template +31 -0
- infoman/cli/templates/app/static_style.css.template +111 -0
- infoman/cli/templates/app/utils_init.py.template +11 -0
- infoman/cli/templates/config/.env.dev.template +43 -0
- infoman/cli/templates/config/.env.prod.template +43 -0
- infoman/cli/templates/config/README.md.template +28 -0
- infoman/cli/templates/docker/.dockerignore.template +60 -0
- infoman/cli/templates/docker/Dockerfile.template +47 -0
- infoman/cli/templates/docker/README.md.template +240 -0
- infoman/cli/templates/docker/docker-compose.yml.template +81 -0
- infoman/cli/templates/docker/mysql_custom.cnf.template +42 -0
- infoman/cli/templates/docker/mysql_init.sql.template +15 -0
- infoman/cli/templates/project/.env.example.template +1 -0
- infoman/cli/templates/project/.gitignore.template +60 -0
- infoman/cli/templates/project/Makefile.template +38 -0
- infoman/cli/templates/project/README.md.template +137 -0
- infoman/cli/templates/project/deploy.sh.template +97 -0
- infoman/cli/templates/project/main.py.template +10 -0
- infoman/cli/templates/project/manage.sh.template +97 -0
- infoman/cli/templates/project/pyproject.toml.template +47 -0
- infoman/cli/templates/project/service.sh.template +203 -0
- infoman/config/__init__.py +25 -0
- infoman/config/base.py +67 -0
- infoman/config/db_cache.py +237 -0
- infoman/config/db_relation.py +181 -0
- infoman/config/db_vector.py +39 -0
- infoman/config/jwt.py +16 -0
- infoman/config/llm.py +16 -0
- infoman/config/log.py +627 -0
- infoman/config/mq.py +26 -0
- infoman/config/settings.py +65 -0
- infoman/llm/__init__.py +0 -0
- infoman/llm/llm.py +297 -0
- infoman/logger/__init__.py +57 -0
- infoman/logger/context.py +191 -0
- infoman/logger/core.py +358 -0
- infoman/logger/filters.py +157 -0
- infoman/logger/formatters.py +138 -0
- infoman/logger/handlers.py +276 -0
- infoman/logger/metrics.py +160 -0
- infoman/performance/README.md +583 -0
- infoman/performance/__init__.py +19 -0
- infoman/performance/cli.py +215 -0
- infoman/performance/config.py +166 -0
- infoman/performance/reporter.py +519 -0
- infoman/performance/runner.py +303 -0
- infoman/performance/standards.py +222 -0
- infoman/service/__init__.py +8 -0
- infoman/service/app.py +67 -0
- infoman/service/core/__init__.py +0 -0
- infoman/service/core/auth.py +105 -0
- infoman/service/core/lifespan.py +132 -0
- infoman/service/core/monitor.py +57 -0
- infoman/service/core/response.py +37 -0
- infoman/service/exception/__init__.py +7 -0
- infoman/service/exception/error.py +274 -0
- infoman/service/exception/exception.py +25 -0
- infoman/service/exception/handler.py +238 -0
- infoman/service/infrastructure/__init__.py +8 -0
- infoman/service/infrastructure/base.py +212 -0
- infoman/service/infrastructure/db_cache/__init__.py +8 -0
- infoman/service/infrastructure/db_cache/manager.py +194 -0
- infoman/service/infrastructure/db_relation/__init__.py +41 -0
- infoman/service/infrastructure/db_relation/manager.py +300 -0
- infoman/service/infrastructure/db_relation/manager_pro.py +408 -0
- infoman/service/infrastructure/db_relation/mysql.py +52 -0
- infoman/service/infrastructure/db_relation/pgsql.py +54 -0
- infoman/service/infrastructure/db_relation/sqllite.py +25 -0
- infoman/service/infrastructure/db_vector/__init__.py +40 -0
- infoman/service/infrastructure/db_vector/manager.py +201 -0
- infoman/service/infrastructure/db_vector/qdrant.py +322 -0
- infoman/service/infrastructure/mq/__init__.py +15 -0
- infoman/service/infrastructure/mq/manager.py +178 -0
- infoman/service/infrastructure/mq/nats/__init__.py +0 -0
- infoman/service/infrastructure/mq/nats/nats_client.py +57 -0
- infoman/service/infrastructure/mq/nats/nats_event_router.py +25 -0
- infoman/service/launch.py +284 -0
- infoman/service/middleware/__init__.py +7 -0
- infoman/service/middleware/base.py +41 -0
- infoman/service/middleware/logging.py +51 -0
- infoman/service/middleware/rate_limit.py +301 -0
- infoman/service/middleware/request_id.py +21 -0
- infoman/service/middleware/white_list.py +24 -0
- infoman/service/models/__init__.py +8 -0
- infoman/service/models/base.py +441 -0
- infoman/service/models/type/embed.py +70 -0
- infoman/service/routers/__init__.py +18 -0
- infoman/service/routers/health_router.py +311 -0
- infoman/service/routers/monitor_router.py +44 -0
- infoman/service/utils/__init__.py +8 -0
- infoman/service/utils/cache/__init__.py +0 -0
- infoman/service/utils/cache/cache.py +192 -0
- infoman/service/utils/module_loader.py +10 -0
- infoman/service/utils/parse.py +10 -0
- infoman/service/utils/resolver/__init__.py +8 -0
- infoman/service/utils/resolver/base.py +47 -0
- infoman/service/utils/resolver/resp.py +102 -0
- infoman/service/vector/__init__.py +20 -0
- infoman/service/vector/base.py +56 -0
- infoman/service/vector/qdrant.py +125 -0
- infoman/service/vector/service.py +67 -0
- infoman/utils/__init__.py +2 -0
- infoman/utils/decorators/__init__.py +8 -0
- infoman/utils/decorators/cache.py +137 -0
- infoman/utils/decorators/retry.py +99 -0
- infoman/utils/decorators/safe_execute.py +99 -0
- infoman/utils/decorators/timing.py +99 -0
- infoman/utils/encryption/__init__.py +8 -0
- infoman/utils/encryption/aes.py +66 -0
- infoman/utils/encryption/ecc.py +108 -0
- infoman/utils/encryption/rsa.py +112 -0
- infoman/utils/file/__init__.py +0 -0
- infoman/utils/file/handler.py +22 -0
- infoman/utils/hash/__init__.py +0 -0
- infoman/utils/hash/hash.py +61 -0
- infoman/utils/http/__init__.py +8 -0
- infoman/utils/http/client.py +62 -0
- infoman/utils/http/info.py +94 -0
- infoman/utils/http/result.py +19 -0
- infoman/utils/notification/__init__.py +8 -0
- infoman/utils/notification/feishu.py +35 -0
- infoman/utils/text/__init__.py +8 -0
- infoman/utils/text/extractor.py +111 -0
- infomankit-0.3.23.dist-info/METADATA +632 -0
- infomankit-0.3.23.dist-info/RECORD +143 -0
- infomankit-0.3.23.dist-info/WHEEL +4 -0
- infomankit-0.3.23.dist-info/entry_points.txt +5 -0
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
# 性能测试模块
|
|
2
|
+
|
|
3
|
+
Infomankit 性能测试模块提供标准化的 API 性能测试工具,支持定制化配置和精美的 HTML 报告生成。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 📊 **标准化性能评估**: 内置 4 种接口类型的性能标准(fast/normal/complex/heavy)
|
|
8
|
+
- 🎯 **定制化测试配置**: 支持 YAML 配置文件,灵活定义测试用例
|
|
9
|
+
- ⚡ **高并发测试**: 基于 asyncio 和 httpx 的异步并发测试
|
|
10
|
+
- 📈 **详细统计分析**: P50/P95/P99 响应时间、吞吐量、成功率等指标
|
|
11
|
+
- 🎨 **精美 HTML 报告**: 响应式设计、色彩分级、打印友好
|
|
12
|
+
- 🔐 **认证支持**: Bearer Token、Basic Auth 等多种认证方式
|
|
13
|
+
|
|
14
|
+
## 快速开始
|
|
15
|
+
|
|
16
|
+
### 1. 创建配置文件
|
|
17
|
+
|
|
18
|
+
创建 `performance-test.yaml`:
|
|
19
|
+
|
|
20
|
+
```yaml
|
|
21
|
+
project_name: "My API"
|
|
22
|
+
base_url: "http://localhost:8000"
|
|
23
|
+
|
|
24
|
+
# 并发配置
|
|
25
|
+
concurrent_users: 50
|
|
26
|
+
duration: 60 # 秒
|
|
27
|
+
spawn_rate: 5 # 每秒启动用户数
|
|
28
|
+
|
|
29
|
+
# 认证(可选)
|
|
30
|
+
auth_type: "bearer"
|
|
31
|
+
auth_token: "your-token-here"
|
|
32
|
+
|
|
33
|
+
# 测试用例
|
|
34
|
+
test_cases:
|
|
35
|
+
- name: "健康检查"
|
|
36
|
+
url: "/api/health"
|
|
37
|
+
method: "GET"
|
|
38
|
+
interface_type: "fast"
|
|
39
|
+
description: "健康检查接口"
|
|
40
|
+
|
|
41
|
+
- name: "用户列表"
|
|
42
|
+
url: "/api/v1/users"
|
|
43
|
+
method: "GET"
|
|
44
|
+
interface_type: "normal"
|
|
45
|
+
params:
|
|
46
|
+
page: 1
|
|
47
|
+
page_size: 20
|
|
48
|
+
|
|
49
|
+
- name: "创建用户"
|
|
50
|
+
url: "/api/v1/users"
|
|
51
|
+
method: "POST"
|
|
52
|
+
interface_type: "normal"
|
|
53
|
+
json:
|
|
54
|
+
username: "testuser"
|
|
55
|
+
email: "test@example.com"
|
|
56
|
+
|
|
57
|
+
- name: "复杂查询"
|
|
58
|
+
url: "/api/v1/analytics"
|
|
59
|
+
method: "GET"
|
|
60
|
+
interface_type: "complex"
|
|
61
|
+
params:
|
|
62
|
+
start_date: "2024-01-01"
|
|
63
|
+
end_date: "2024-12-31"
|
|
64
|
+
group_by: "month"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. 运行测试
|
|
68
|
+
|
|
69
|
+
使用 Python 代码运行测试:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import asyncio
|
|
73
|
+
from infoman.performance import TestConfig, PerformanceTestRunner, HTMLReporter
|
|
74
|
+
|
|
75
|
+
async def main():
|
|
76
|
+
# 加载配置
|
|
77
|
+
config = TestConfig.from_yaml("performance-test.yaml")
|
|
78
|
+
|
|
79
|
+
# 运行测试
|
|
80
|
+
runner = PerformanceTestRunner(config)
|
|
81
|
+
results = await runner.run()
|
|
82
|
+
|
|
83
|
+
# 生成 HTML 报告
|
|
84
|
+
reporter = HTMLReporter(config)
|
|
85
|
+
report_path = reporter.generate(results)
|
|
86
|
+
print(f"报告已生成: {report_path}")
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
asyncio.run(main())
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
或使用命令行工具:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
infoman perf-test -c performance-test.yaml
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 3. 查看报告
|
|
99
|
+
|
|
100
|
+
在浏览器中打开生成的 `performance-report.html` 查看详细结果。
|
|
101
|
+
|
|
102
|
+
## 性能标准
|
|
103
|
+
|
|
104
|
+
模块内置了 4 种接口类型的性能标准:
|
|
105
|
+
|
|
106
|
+
### 快速接口 (fast)
|
|
107
|
+
适用于:健康检查、静态资源、简单查询
|
|
108
|
+
|
|
109
|
+
- 优秀: < 10ms
|
|
110
|
+
- 良好: < 30ms
|
|
111
|
+
- 可接受: < 50ms
|
|
112
|
+
- 较差: < 100ms
|
|
113
|
+
- 严重: ≥ 100ms
|
|
114
|
+
|
|
115
|
+
### 一般接口 (normal)
|
|
116
|
+
适用于:列表查询、单条数据获取、简单 CRUD
|
|
117
|
+
|
|
118
|
+
- 优秀: < 50ms
|
|
119
|
+
- 良好: < 100ms
|
|
120
|
+
- 可接受: < 200ms
|
|
121
|
+
- 较差: < 500ms
|
|
122
|
+
- 严重: ≥ 500ms
|
|
123
|
+
|
|
124
|
+
### 复杂接口 (complex)
|
|
125
|
+
适用于:复杂查询、多表关联、数据聚合
|
|
126
|
+
|
|
127
|
+
- 优秀: < 100ms
|
|
128
|
+
- 良好: < 200ms
|
|
129
|
+
- 可接受: < 500ms
|
|
130
|
+
- 较差: < 1s
|
|
131
|
+
- 严重: ≥ 1s
|
|
132
|
+
|
|
133
|
+
### 重型接口 (heavy)
|
|
134
|
+
适用于:文件处理、批量操作、报表生成
|
|
135
|
+
|
|
136
|
+
- 优秀: < 200ms
|
|
137
|
+
- 良好: < 500ms
|
|
138
|
+
- 可接受: < 1s
|
|
139
|
+
- 较差: < 3s
|
|
140
|
+
- 严重: ≥ 3s
|
|
141
|
+
|
|
142
|
+
## 配置详解
|
|
143
|
+
|
|
144
|
+
### 基础配置
|
|
145
|
+
|
|
146
|
+
```yaml
|
|
147
|
+
project_name: "项目名称"
|
|
148
|
+
base_url: "http://localhost:8000"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 并发配置
|
|
152
|
+
|
|
153
|
+
```yaml
|
|
154
|
+
concurrent_users: 50 # 并发用户数
|
|
155
|
+
duration: 60 # 测试持续时间(秒)
|
|
156
|
+
spawn_rate: 5 # 每秒启动用户数
|
|
157
|
+
think_time_min: 1 # 最小思考时间(秒)
|
|
158
|
+
think_time_max: 3 # 最大思考时间(秒)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 认证配置
|
|
162
|
+
|
|
163
|
+
#### Bearer Token
|
|
164
|
+
|
|
165
|
+
```yaml
|
|
166
|
+
auth_type: "bearer"
|
|
167
|
+
auth_token: "your-jwt-token"
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### Basic Auth
|
|
171
|
+
|
|
172
|
+
```yaml
|
|
173
|
+
auth_type: "basic"
|
|
174
|
+
auth_username: "admin"
|
|
175
|
+
auth_password: "password"
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 全局请求头
|
|
179
|
+
|
|
180
|
+
```yaml
|
|
181
|
+
global_headers:
|
|
182
|
+
User-Agent: "My-Test/1.0"
|
|
183
|
+
Accept: "application/json"
|
|
184
|
+
X-Custom-Header: "value"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### 测试用例配置
|
|
188
|
+
|
|
189
|
+
#### GET 请求
|
|
190
|
+
|
|
191
|
+
```yaml
|
|
192
|
+
test_cases:
|
|
193
|
+
- name: "获取用户列表"
|
|
194
|
+
url: "/api/v1/users"
|
|
195
|
+
method: "GET"
|
|
196
|
+
interface_type: "normal"
|
|
197
|
+
params:
|
|
198
|
+
page: 1
|
|
199
|
+
page_size: 20
|
|
200
|
+
timeout: 30
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### POST 请求
|
|
204
|
+
|
|
205
|
+
```yaml
|
|
206
|
+
test_cases:
|
|
207
|
+
- name: "创建订单"
|
|
208
|
+
url: "/api/v1/orders"
|
|
209
|
+
method: "POST"
|
|
210
|
+
interface_type: "normal"
|
|
211
|
+
json:
|
|
212
|
+
product_id: 123
|
|
213
|
+
quantity: 2
|
|
214
|
+
headers:
|
|
215
|
+
Content-Type: "application/json"
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### 表单提交
|
|
219
|
+
|
|
220
|
+
```yaml
|
|
221
|
+
test_cases:
|
|
222
|
+
- name: "上传文件"
|
|
223
|
+
url: "/api/v1/upload"
|
|
224
|
+
method: "POST"
|
|
225
|
+
interface_type: "heavy"
|
|
226
|
+
data:
|
|
227
|
+
filename: "test.txt"
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### 禁用测试用例
|
|
231
|
+
|
|
232
|
+
```yaml
|
|
233
|
+
test_cases:
|
|
234
|
+
- name: "临时禁用的测试"
|
|
235
|
+
url: "/api/test"
|
|
236
|
+
method: "GET"
|
|
237
|
+
enabled: false # 不会执行此测试
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 报告配置
|
|
241
|
+
|
|
242
|
+
```yaml
|
|
243
|
+
report_title: "API 性能测试报告"
|
|
244
|
+
report_output: "./reports/performance-report.html"
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## 编程 API
|
|
248
|
+
|
|
249
|
+
### 配置管理
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
from infoman.performance import TestConfig, APITestCase
|
|
253
|
+
|
|
254
|
+
# 从 YAML 加载
|
|
255
|
+
config = TestConfig.from_yaml("test.yaml")
|
|
256
|
+
|
|
257
|
+
# 保存为 YAML
|
|
258
|
+
config.to_yaml("output.yaml")
|
|
259
|
+
|
|
260
|
+
# 动态添加测试用例
|
|
261
|
+
config.add_test_case(
|
|
262
|
+
APITestCase(
|
|
263
|
+
name="新测试",
|
|
264
|
+
url="/api/test",
|
|
265
|
+
method="GET",
|
|
266
|
+
interface_type="fast"
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
# 获取启用的测试用例
|
|
271
|
+
enabled_cases = config.get_enabled_test_cases()
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 运行测试
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
from infoman.performance import PerformanceTestRunner
|
|
278
|
+
|
|
279
|
+
runner = PerformanceTestRunner(config)
|
|
280
|
+
results = await runner.run()
|
|
281
|
+
|
|
282
|
+
# 访问结果
|
|
283
|
+
for name, result in results.items():
|
|
284
|
+
print(f"测试: {name}")
|
|
285
|
+
print(f" 总请求: {result.total_requests}")
|
|
286
|
+
print(f" 成功率: {result.success_rate:.2f}%")
|
|
287
|
+
print(f" 平均响应时间: {result.avg_response_time:.2f}ms")
|
|
288
|
+
print(f" P95: {result.p95_response_time:.2f}ms")
|
|
289
|
+
print(f" 吞吐量: {result.throughput:.2f} req/s")
|
|
290
|
+
print(f" 综合评级: {result.overall_level}")
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### 生成报告
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
from infoman.performance import HTMLReporter
|
|
297
|
+
|
|
298
|
+
reporter = HTMLReporter(config)
|
|
299
|
+
report_path = reporter.generate(
|
|
300
|
+
results=results,
|
|
301
|
+
output_path="custom-report.html"
|
|
302
|
+
)
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### 性能评估
|
|
306
|
+
|
|
307
|
+
```python
|
|
308
|
+
from infoman.performance import PerformanceStandards, StandardLevel
|
|
309
|
+
|
|
310
|
+
# 评估响应时间
|
|
311
|
+
level = PerformanceStandards.evaluate_response_time(
|
|
312
|
+
response_time=120, # 毫秒
|
|
313
|
+
interface_type="normal"
|
|
314
|
+
)
|
|
315
|
+
print(level) # StandardLevel.GOOD
|
|
316
|
+
|
|
317
|
+
# 评估吞吐量
|
|
318
|
+
level = PerformanceStandards.evaluate_throughput(
|
|
319
|
+
throughput=300, # req/s
|
|
320
|
+
interface_type="normal"
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
# 评估成功率
|
|
324
|
+
level = PerformanceStandards.evaluate_success_rate(99.5)
|
|
325
|
+
|
|
326
|
+
# 获取标签和建议
|
|
327
|
+
label = PerformanceStandards.get_level_label(level)
|
|
328
|
+
recommendation = PerformanceStandards.get_recommendation(level)
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## 高级用法
|
|
332
|
+
|
|
333
|
+
### 自定义性能标准
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
from infoman.performance.standards import PerformanceStandards, PerformanceThreshold
|
|
337
|
+
|
|
338
|
+
# 添加自定义接口类型
|
|
339
|
+
PerformanceStandards.STANDARDS["custom"] = PerformanceThreshold(
|
|
340
|
+
excellent=80,
|
|
341
|
+
good=150,
|
|
342
|
+
acceptable=300,
|
|
343
|
+
poor=800
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
# 使用自定义类型
|
|
347
|
+
level = PerformanceStandards.evaluate_response_time(
|
|
348
|
+
response_time=200,
|
|
349
|
+
interface_type="custom"
|
|
350
|
+
)
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### 多环境测试
|
|
354
|
+
|
|
355
|
+
```python
|
|
356
|
+
environments = {
|
|
357
|
+
"dev": "http://dev.example.com",
|
|
358
|
+
"staging": "http://staging.example.com",
|
|
359
|
+
"prod": "http://prod.example.com"
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
for env_name, base_url in environments.items():
|
|
363
|
+
config = TestConfig.from_yaml("test.yaml")
|
|
364
|
+
config.base_url = base_url
|
|
365
|
+
config.report_output = f"report-{env_name}.html"
|
|
366
|
+
|
|
367
|
+
runner = PerformanceTestRunner(config)
|
|
368
|
+
results = await runner.run()
|
|
369
|
+
|
|
370
|
+
reporter = HTMLReporter(config)
|
|
371
|
+
reporter.generate(results)
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### 集成到 CI/CD
|
|
375
|
+
|
|
376
|
+
```yaml
|
|
377
|
+
# .github/workflows/performance.yml
|
|
378
|
+
name: Performance Test
|
|
379
|
+
|
|
380
|
+
on:
|
|
381
|
+
schedule:
|
|
382
|
+
- cron: '0 0 * * *' # 每天运行
|
|
383
|
+
|
|
384
|
+
jobs:
|
|
385
|
+
performance:
|
|
386
|
+
runs-on: ubuntu-latest
|
|
387
|
+
steps:
|
|
388
|
+
- uses: actions/checkout@v4
|
|
389
|
+
|
|
390
|
+
- name: Run Performance Test
|
|
391
|
+
run: |
|
|
392
|
+
pip install infomankit
|
|
393
|
+
infoman perf-test -c performance-test.yaml
|
|
394
|
+
|
|
395
|
+
- name: Upload Report
|
|
396
|
+
uses: actions/upload-artifact@v4
|
|
397
|
+
with:
|
|
398
|
+
name: performance-report
|
|
399
|
+
path: performance-report.html
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## 最佳实践
|
|
403
|
+
|
|
404
|
+
### 1. 合理设置并发数
|
|
405
|
+
|
|
406
|
+
根据服务器资源调整并发用户数:
|
|
407
|
+
|
|
408
|
+
- 开发环境: 10-20
|
|
409
|
+
- 测试环境: 50-100
|
|
410
|
+
- 生产环境压测: 100-500
|
|
411
|
+
|
|
412
|
+
### 2. 设置思考时间
|
|
413
|
+
|
|
414
|
+
模拟真实用户行为,避免过度压测:
|
|
415
|
+
|
|
416
|
+
```yaml
|
|
417
|
+
think_time_min: 2
|
|
418
|
+
think_time_max: 5
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
### 3. 分类测试用例
|
|
422
|
+
|
|
423
|
+
按接口类型正确分类,获得准确的性能评估:
|
|
424
|
+
|
|
425
|
+
```yaml
|
|
426
|
+
test_cases:
|
|
427
|
+
- name: "健康检查"
|
|
428
|
+
interface_type: "fast" # 简单接口
|
|
429
|
+
|
|
430
|
+
- name: "用户列表"
|
|
431
|
+
interface_type: "normal" # 一般接口
|
|
432
|
+
|
|
433
|
+
- name: "数据分析"
|
|
434
|
+
interface_type: "complex" # 复杂接口
|
|
435
|
+
|
|
436
|
+
- name: "导出报表"
|
|
437
|
+
interface_type: "heavy" # 重型接口
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### 4. 逐步增加负载
|
|
441
|
+
|
|
442
|
+
使用 `spawn_rate` 控制用户启动速率,避免瞬间冲击:
|
|
443
|
+
|
|
444
|
+
```yaml
|
|
445
|
+
concurrent_users: 100
|
|
446
|
+
spawn_rate: 10 # 10秒内逐步启动所有用户
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### 5. 监控服务器资源
|
|
450
|
+
|
|
451
|
+
在测试期间监控:
|
|
452
|
+
- CPU 使用率
|
|
453
|
+
- 内存使用率
|
|
454
|
+
- 网络带宽
|
|
455
|
+
- 数据库连接数
|
|
456
|
+
|
|
457
|
+
## 故障排查
|
|
458
|
+
|
|
459
|
+
### 连接失败
|
|
460
|
+
|
|
461
|
+
```
|
|
462
|
+
错误: 连接失败
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
**解决方案**:
|
|
466
|
+
- 检查 `base_url` 是否正确
|
|
467
|
+
- 确认服务是否运行
|
|
468
|
+
- 检查网络连接和防火墙
|
|
469
|
+
|
|
470
|
+
### 超时错误
|
|
471
|
+
|
|
472
|
+
```
|
|
473
|
+
错误: 请求超时
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
**解决方案**:
|
|
477
|
+
- 增加 `timeout` 配置
|
|
478
|
+
- 检查接口性能
|
|
479
|
+
- 降低并发数
|
|
480
|
+
|
|
481
|
+
### 认证失败
|
|
482
|
+
|
|
483
|
+
```
|
|
484
|
+
错误: HTTP 401: Unauthorized
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
**解决方案**:
|
|
488
|
+
- 检查 `auth_token` 是否有效
|
|
489
|
+
- 确认认证方式配置正确
|
|
490
|
+
- 检查 token 是否过期
|
|
491
|
+
|
|
492
|
+
## 示例场景
|
|
493
|
+
|
|
494
|
+
### 场景 1: REST API 性能测试
|
|
495
|
+
|
|
496
|
+
```yaml
|
|
497
|
+
project_name: "REST API Performance Test"
|
|
498
|
+
base_url: "https://api.example.com"
|
|
499
|
+
concurrent_users: 100
|
|
500
|
+
duration: 300 # 5分钟
|
|
501
|
+
|
|
502
|
+
test_cases:
|
|
503
|
+
- name: "列表查询"
|
|
504
|
+
url: "/api/v1/items"
|
|
505
|
+
method: "GET"
|
|
506
|
+
interface_type: "normal"
|
|
507
|
+
|
|
508
|
+
- name: "详情查询"
|
|
509
|
+
url: "/api/v1/items/1"
|
|
510
|
+
method: "GET"
|
|
511
|
+
interface_type: "fast"
|
|
512
|
+
|
|
513
|
+
- name: "创建记录"
|
|
514
|
+
url: "/api/v1/items"
|
|
515
|
+
method: "POST"
|
|
516
|
+
interface_type: "normal"
|
|
517
|
+
json:
|
|
518
|
+
name: "Test Item"
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
### 场景 2: 微服务压力测试
|
|
522
|
+
|
|
523
|
+
```yaml
|
|
524
|
+
project_name: "Microservices Stress Test"
|
|
525
|
+
base_url: "http://gateway.local"
|
|
526
|
+
concurrent_users: 500
|
|
527
|
+
duration: 600 # 10分钟
|
|
528
|
+
spawn_rate: 50
|
|
529
|
+
|
|
530
|
+
test_cases:
|
|
531
|
+
- name: "用户服务 - 登录"
|
|
532
|
+
url: "/user-service/api/login"
|
|
533
|
+
method: "POST"
|
|
534
|
+
interface_type: "normal"
|
|
535
|
+
|
|
536
|
+
- name: "订单服务 - 创建订单"
|
|
537
|
+
url: "/order-service/api/orders"
|
|
538
|
+
method: "POST"
|
|
539
|
+
interface_type: "complex"
|
|
540
|
+
|
|
541
|
+
- name: "支付服务 - 支付"
|
|
542
|
+
url: "/payment-service/api/pay"
|
|
543
|
+
method: "POST"
|
|
544
|
+
interface_type: "heavy"
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
### 场景 3: 搜索性能测试
|
|
548
|
+
|
|
549
|
+
```yaml
|
|
550
|
+
project_name: "Search Performance Test"
|
|
551
|
+
base_url: "https://search.example.com"
|
|
552
|
+
concurrent_users: 200
|
|
553
|
+
duration: 120
|
|
554
|
+
|
|
555
|
+
test_cases:
|
|
556
|
+
- name: "简单搜索"
|
|
557
|
+
url: "/api/search"
|
|
558
|
+
method: "GET"
|
|
559
|
+
interface_type: "normal"
|
|
560
|
+
params:
|
|
561
|
+
q: "test"
|
|
562
|
+
|
|
563
|
+
- name: "高级搜索"
|
|
564
|
+
url: "/api/search/advanced"
|
|
565
|
+
method: "POST"
|
|
566
|
+
interface_type: "complex"
|
|
567
|
+
json:
|
|
568
|
+
query: "test"
|
|
569
|
+
filters:
|
|
570
|
+
category: "tech"
|
|
571
|
+
date_range: "2024"
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
## 相关链接
|
|
575
|
+
|
|
576
|
+
- [性能标准定义](./standards.py)
|
|
577
|
+
- [配置模型](./config.py)
|
|
578
|
+
- [测试运行器](./runner.py)
|
|
579
|
+
- [报告生成器](./reporter.py)
|
|
580
|
+
|
|
581
|
+
## 许可证
|
|
582
|
+
|
|
583
|
+
MIT License - 详见项目根目录 LICENSE 文件
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
性能测试模块
|
|
3
|
+
|
|
4
|
+
提供标准化的性能测试工具,支持定制化接口测试和 HTML 报告生成
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .standards import PerformanceStandards, StandardLevel
|
|
8
|
+
from .config import TestConfig, APITestCase
|
|
9
|
+
from .runner import PerformanceTestRunner
|
|
10
|
+
from .reporter import HTMLReporter
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"PerformanceStandards",
|
|
14
|
+
"StandardLevel",
|
|
15
|
+
"TestConfig",
|
|
16
|
+
"APITestCase",
|
|
17
|
+
"PerformanceTestRunner",
|
|
18
|
+
"HTMLReporter",
|
|
19
|
+
]
|