LiteLLM 代理 - 使用 locust 进行 1K RPS 负载测试
关于如何使用 locust 在 LiteLLM Proxy 上达到 1K+ RPS 的教程
测试前检查清单
- 确保使用的是 litellm 的最新
-stable版本 - 确保遵循所有 生产环境最佳实践
- Locust - 确保您的 Locust 实例能够每秒创建 1K+ 个请求
- 👉 您可以使用我们维护的 locust 实例
- 如果您是自托管 locust
- 请使用此 运行 litellm proxy 的机器规格
- 企业版 LiteLLM - 在
proxy_config.yaml中使用prometheus作为回调以获取负载测试指标。设置litellm_settings.callbacks以监控成功、失败及所有类型的错误。litellm_settings:
callbacks: ["prometheus"] # Enterprise LiteLLM Only - use prometheus to get metrics on your load test
使用此配置进行测试
注意:我们目前正在迁移到 aiohttp,其吞吐量提高了 10 倍。建议使用 openai/ 提供商进行负载测试。
设置模拟 OpenAI 端点
您可以使用我们托管的模拟端点,或者使用 github.com/BerriAI/example_openai_endpoint 自行托管。
model_list:
- model_name: "fake-openai-endpoint"
litellm_params:
model: openai/any
api_base: https://exampleopenaiendpoint-production.up.railway.app/ # or your self-hosted endpoint
api_key: "test"
负载测试 - 模拟 OpenAI 端点
预期性能
| 指标 | 值 |
|---|---|
| 每秒请求数 | 1174+ |
| 中位响应时间 | 96ms |
| 平均响应时间 | 142.18ms |
运行测试
- 将
fake-openai-endpoint添加到您的 proxy config.yaml 并启动 litellm proxy。litellm 提供了一个托管的fake-openai-endpoint供您进行负载测试。
model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
litellm_settings:
callbacks: ["prometheus"] # Enterprise LiteLLM Only - use prometheus to get metrics on your load test
-
pip install locust -
在您的本地机器上创建一个名为
locustfile.py的文件。从 这里 复制 litellm 负载测试的内容。 -
启动 locust。在与
locustfile.py相同的目录下运行locust(见第 2 步)。
locust -f locustfile.py --processes 4
- 在 locust 上运行负载测试
前往 locust UI:http://0.0.0.0:8089
设置 Users=1000, Ramp Up Users=1000,Host=您的 LiteLLM Proxy 的基础 URL
- 预期结果
负载测试 - 带有速率限制的端点
对 2 个 LLM 部署进行负载测试,每个部署拥有 10K RPM 配额。预期看到约 20K RPM。
预期性能
- 我们预期在 1 分钟内看到 20,000+ 次成功响应
- 剩余的请求会失败,因为端点超过了 LLM API 提供商设定的 10K RPM 配额限制
| 指标 | 值 |
|---|---|
| 1 分钟内的成功响应数 | 20,000+ |
| 每秒请求数 | ~1170+ |
| 中位响应时间 | 70ms |
| 平均响应时间 | 640.18ms |
运行测试
- 在您的 config.yaml 中添加 2 个
gemini-vision部署。每个部署可处理 10K RPM。(我们在下方的/v1/projects/bad-adroit-crow路由上设置了一个 1000 RPM 速率限制的模拟端点)
信息
所有 model="gemini-vision" 的请求都将在 2 个部署之间平均分配负载。
model_list:
- model_name: gemini-vision
litellm_params:
model: vertex_ai/gemini-1.0-pro-vision-001
api_base: https://exampleopenaiendpoint-production.up.railway.app/v1/projects/bad-adroit-crow-413218/locations/us-central1/publishers/google/models/gemini-1.0-pro-vision-001
vertex_project: "adroit-crow-413218"
vertex_location: "us-central1"
vertex_credentials: /etc/secrets/adroit_crow.json
- model_name: gemini-vision
litellm_params:
model: vertex_ai/gemini-1.0-pro-vision-001
api_base: https://exampleopenaiendpoint-production-c715.up.railway.app/v1/projects/bad-adroit-crow-413218/locations/us-central1/publishers/google/models/gemini-1.0-pro-vision-001
vertex_project: "adroit-crow-413218"
vertex_location: "us-central1"
vertex_credentials: /etc/secrets/adroit_crow.json
litellm_settings:
callbacks: ["prometheus"] # Enterprise LiteLLM Only - use prometheus to get metrics on your load test
-
pip install locust -
在您的本地机器上创建一个名为
locustfile.py的文件。从 这里 复制 litellm 负载测试的内容。 -
启动 locust。在与
locustfile.py相同的目录下运行locust(见第 2 步)。
locust -f locustfile.py --processes 4 -t 60
- 在 locust 上运行负载测试
前往 http://0.0.0.0:8089 上的 locust UI 并使用以下设置
- 预期结果
- 1 分钟内成功响应 = 19,800 = (69415 - 49615)
- 每秒请求数 = 1170
- 中位响应时间 = 70ms
- 平均响应时间 = 640ms
用于调试负载测试的 Prometheus 指标
使用以下 Prometheus 指标来调试您的负载测试/失败情况
| 指标名称 | 描述 |
|---|---|
litellm_deployment_failure_responses | 特定 LLM 部署的失败 LLM API 调用总数。标签:"requested_model", "litellm_model_name", "model_id", "api_base", "api_provider", "hashed_api_key", "api_key_alias", "team", "team_alias", "exception_status", "exception_class" |
litellm_deployment_cooled_down | 部署被 LiteLLM 负载均衡逻辑冷却的次数。标签:"litellm_model_name", "model_id", "api_base", "api_provider", "exception_status" |
运行 Locust 的机器规格
| 指标 | 值 |
|---|---|
locust --processes 4 | 4 |
负载测试机器上的 vCPUs | 2.0 vCPUs |
负载测试机器上的 内存 | 450 MB |
负载测试机器的 副本数 | 1 |
运行 LiteLLM Proxy 的机器规格
👉 LiteLLM Proxy 的副本数=4,以获得 1K+ RPS
| 服务 | 规范 | CPU | 内存 | 架构 | 版本 |
|---|---|---|---|---|---|
| 服务器 | t2.large. | 2vCPUs | 8GB | x86 |
测试使用的 Locust 文件
import os
import uuid
from locust import HttpUser, task, between
class MyUser(HttpUser):
wait_time = between(0.5, 1) # Random wait time between requests
@task(100)
def litellm_completion(self):
# no cache hits with this
payload = {
"model": "fake-openai-endpoint",
"messages": [{"role": "user", "content": f"{uuid.uuid4()} This is a test there will be no cache hits and we'll fill up the context" * 150 }],
"user": "my-new-end-user-1"
}
response = self.client.post("chat/completions", json=payload)
if response.status_code != 200:
# log the errors in error.txt
with open("error.txt", "a") as error_log:
error_log.write(response.text + "\n")
def on_start(self):
self.api_key = os.getenv('API_KEY', 'sk-1234')
self.client.headers.update({'Authorization': f'Bearer {self.api_key}'})