Show HN:Benchi - 一款使用 Go 编写的基准测试工具
ConduitIO/benchi
main BranchesTags Go to file Code
Folders and files
Name| Name| Last commit message| Last commit date
---|---|---|---
Latest commit
History
71 Commits
.github| .github
cmd/benchi| cmd/benchi
config| config
dockerutil| dockerutil
example| example
metrics| metrics
tools| tools
.gitignore| .gitignore
.golangci.goheader.template| .golangci.goheader.template
.golangci.yml| .golangci.yml
.goreleaser.yml| .goreleaser.yml
LICENSE.md| LICENSE.md
Makefile| Makefile
README.md| README.md
go.mod| go.mod
go.sum| go.sum
install.sh| install.sh
runner.go| runner.go
step_string.go| step_string.go
View all files
Repository files navigation
Benchi
Benchi 是一个极简的基准测试框架,旨在帮助你衡量应用程序和基础设施的性能。它利用 Docker 创建隔离环境来运行基准测试并收集指标。
它的开发目的是简化为 Conduit 设置和运行基准测试的过程。
特性
- Docker 集成:使用 Docker Compose 定义和管理你的基准测试环境。
- 指标收集:收集指标并以 CSV 格式导出,以便进一步分析。
- 自定义钩子:定义自定义钩子,以便在基准测试的各个阶段运行命令。
- 进度监控:在基准测试运行期间实时监控容器状态和指标。
安装
要安装 Benchi,请下载最新版本或使用 Go 安装:
go install github.com/conduitio/benchi/cmd/benchi@latest
或者,你也可以在 Linux 操作系统上只安装 Benchi 二进制文件:
curl https://raw.githubusercontent.com/ConduitIO/benchi/main/install.sh | sh
用法
运行基准测试
运行 Benchi 并将 -config
指向基准测试配置文件。该仓库包含一个示例基准测试,可以使用以下命令运行:
benchi -config ./example/bench-kafka-kafka/bench.yml
结果
运行基准测试会将结果存储在 results
内部以当前日期和时间命名的文件夹中(例如 results/20060102_150405
)。你可以使用 -out
标志调整输出文件夹。
输出文件夹将包含两个文件:
benchi.log
: 包含 Benchi 完整输出的日志文件。aggregated-results.csv
: 来自所有收集器和所有测试的聚合指标结果。结果使用截尾平均值进行聚合,其中删除顶部和底部 5% 的结果。Benchi 还会忽略测试开始和结束时的任何 0 值,以适应预热和冷却阶段。
输出文件夹还将包含每个基准测试运行的一个文件夹(即,每个测试和工具组合)。每个基准测试运行文件夹将包含:
infra_NAME.log
: 包含基础设施 Docker 容器输出的日志文件,每个基础设施服务拆分。tool_NAME.log
: 包含工具 Docker 容器输出的日志文件。COLLECTOR.csv
: 使用相应的指标收集器收集的原始指标。
命令行标志
-
-config
: 基准测试配置文件的路径(必需)。 -
-out
: 输出文件夹的路径(默认值:./results/${now}
*)。 -
-tool
: 要测试的过滤器工具(可以多次提供)。 -
-tests
: 要运行的过滤器测试(可以多次提供)。 -
${now}
替换为当前时间,格式为YYYYMMDD_HHMMSS
。
Docker 网络
Benchi 创建一个名为 benchi
的 Docker 网络,用于连接基础设施服务和工具。此网络会自动创建,并在基准测试运行后删除。请确保将你的服务连接到此网络,以确保它们可以相互通信。
示例 Docker Compose 配置:
services:
my-service:
networks:
- benchi
networks:
benchi:
external: true
环境变量
Benchi 使用与当前 shell 相同的环境变量运行所有 Docker Compose 命令。这意味着你可以使用环境变量将值传递给你的服务。
例如,具有以下 Docker Compose 配置:
services:
my-service:
environment:
- MY_ENV_VAR=${MY_ENV_VAR}
你可以通过运行 Benchi 如下注入环境变量:
MY_ENV_VAR=my-value benchi -config ./my-benchmark.yml
配置
Benchi 使用 YAML 配置文件来定义与 Docker Compose 配置相结合的基准测试。
以下是一个示例配置:
infrastructure:
database:
compose: "./compose-database.yml"
cache:
compose: "./compose-cache.yml"
tools:
my-app:
compose: "./compose-my-app.yml"
metrics:
prometheus:
collector: "prometheus"
settings:
url: "http://localhost:9090/metrics"
queries:
- name: "http_requests_rate"
query: "rate(request_count{endpoint=hello}[2s])"
unit: "req/s"
interval: "1s"
tests:
- name: Endpoint Load
duration: 2m
steps:
pre-infrastructure:
post-infrastructure:
- name: Setup Database
container: database
run: /scripts/setup-database.sh
pre-tools:
post-tools:
pre-test:
during:
- name: Run Load Test
container: my-app
run: /scripts/run-load-test.sh
post-test:
pre-cleanup:
- name: Cleanup
container: my-app
run: /scripts/cleanup.sh
post-cleanup:
infrastructure
infrastructure
部分定义了基准测试所需的基础设施服务的 Docker Compose 配置。每个服务都由一个自定义名称标识,用于日志记录和关联测试中指定的覆盖配置(参见tests。Docker Compose 文件的路径相对于基准测试配置文件的位置。
示例:
infrastructure:
name-of-infrastructure-service:
compose: "./path/to/compose-file.yml"
tools
tools
部分定义了正在进行基准测试的工具的 Docker Compose 配置。每个工具都由一个自定义名称标识,用于日志记录和关联测试中指定的覆盖配置(参见tests。Docker Compose 文件的路径相对于基准测试配置文件的位置。
示例:
tools:
name-of-tool:
compose: "./path/to/compose-file.yml"
metrics
metrics
部分定义了在基准测试期间运行的指标收集器。每个指标收集器都有一个用于日志记录的自定义名称。collector
字段指定要使用的指标收集器的类型。settings
字段包含所选收集器的配置。
示例:
metrics:
name-of-metric-collector:
collector: "conduit"
settings:
url: "http://localhost:8080/metrics"
注意 指标收集器在 Benchi 进程中运行,该进程在主机上的 Docker 外部运行。确保指标收集器可以通过在 Docker Compose 配置中公开必要的端口来访问正在进行基准测试的服务的端点。
有关可用收集器及其配置,请参见 collectors。
tests
tests
部分定义了要运行的基准测试。每个测试都有一个用于日志记录的自定义名称。duration
字段指定测试的持续时间。steps
字段包含在基准测试的各个阶段运行的命令。
steps
字段包含以下阶段:
pre-infrastructure
: 启动基础设施服务之前要运行的命令。post-infrastructure
: 启动基础设施服务之后要运行的命令。pre-tools
: 启动工具之前要运行的命令。post-tools
: 启动工具之后要运行的命令。pre-test
: 启动测试之前要运行的命令。during
: 测试期间要运行的命令。post-test
: 测试之后要运行的命令。pre-cleanup
: 清理测试之前要运行的命令。post-cleanup
: 清理测试之后要运行的命令。
注意
步骤通常按顺序执行,并按照配置中指定的顺序执行。但是,during
步骤是一个例外,因为此步骤下的所有命令都会并发执行,并且将在测试期间运行。
每个钩子都可以在现有容器中或在从指定镜像创建的临时容器中运行其命令。container
字段指定要在其中运行命令的容器的名称。image
字段指定用于临时容器的镜像。如果未指定 container
和 image
,则命令将在使用 alpine:latest
镜像的临时容器中运行。请注意,在容器中运行自定义脚本需要容器包含必要的脚本(提示:将脚本作为卷挂载)。
你可以选择配置 tools
字段,以便仅针对某些工具运行钩子。如果该字段不存在或为空,则该钩子适用于所有工具。
示例:
tests:
- name: My Test
duration: 2m
steps:
pre-infrastructure:
post-infrastructure:
# This script will run after the infrastructure services have started.
# It will run in the `database` container (presumably started by the
# infrastructure service).
- name: Setup Database
container: database
run: /scripts/setup-database.sh
pre-tools:
post-tools:
pre-test:
during:
# This script will run for the duration of the test (2 minutes). It will
# run in the `my-app` container (presumably started by the tool). The
# script will receive a SIGINT signal when the test duration is over.
- name: Run Load Test
container: my-app
tools:
- my-app # Only run this hook when benchmarking my-app
run: /scripts/run-load-test.sh
post-test:
pre-cleanup:
# This script will run before infrastructure and tools containers are
# stopped and removed. It will run in a temporary container created
# from the `busybox:latest` image and connected to the `benchi` network.
- name: Cleanup
image: "busybox:latest"
run: |
echo "Cleaning up..."
sleep 5
post-cleanup:
你还可以包含自定义 infrastructure
和 tools
配置,以覆盖 infrastructure
和 tools
部分中指定的默认配置。请注意,全局配置仍将应用,其他配置将与全局配置合并(参见合并 compose 文件。这对于为特定测试注入自定义配置非常有用。
重要 由于提供的配置与全局配置合并,因此自定义 Docker Compose 配置中指定的任何路径都应相对于全局 Docker Compose 配置文件的位置。请参见合并规则。
示例:
tests:
- name: My Test
duration: 2m
infrastructure:
name-of-infrastructure-service:
compose: "./compose-file-infra.override.yml"
tools:
name-of-tool:
compose: "./compose-file-tool.override.yml"
收集器
收集器用于在基准测试运行期间从各种来源收集指标。收集的指标以 CSV 格式导出,以便进一步分析。
收集器在基准测试配置文件的 metrics
部分中配置。
支持的收集器:
Conduit
Conduit 指标收集器跟踪 Conduit 中每个配置的管道的吞吐量。
设置:
url
: Conduit 指标端点的 URL(需要从 Benchi 进程访问)。pipelines
: 要跟踪的管道数组。
metrics:
my-conduit-collector:
collector: "conduit"
settings:
url: "http://localhost:8080/metrics"
pipelines:
- "pipeline1"
- "pipeline2"
指标:
msg-rate-per-second[PIPELINE]
: 每个管道每秒的消息数。msg-megabytes-in-per-second[PIPELINE]
: 每个管道每秒的传入兆字节数(根据源连接器读取的记录数进行测量)。msg-megabytes-out-per-second[PIPELINE]
: 每个管道每秒的传出兆字节数(根据目标连接器写入的记录数进行测量)。
Docker
Docker 指标收集器跟踪基准测试运行期间的容器资源使用情况。
设置:
containers
: 要跟踪的容器数组。
metrics:
my-docker-collector:
collector: "docker"
settings:
containers:
- "my-app"
- "database"
- "cache"
指标:
cpu-percentage[CONTAINER]
: 每个容器在特定时间点的 CPU 使用率百分比。memory-usage[CONTAINER]
: 每个容器在特定时间点的内存使用量(以兆字节为单位)。
Kafka
Kafka 指标收集器跟踪 Apache Kafka 中每个配置的主题的吞吐量。
收集器希望 Kafka 通过 Prometheus JMX 导出器 公开 Prometheus 指标端点。要了解如何配置它,请查看示例配置。
设置:
url
: Kafka 指标端点的 URL(需要从 Benchi 进程访问)。topics
: 要跟踪的主题数组。
metrics:
my-kafka-collector:
collector: "kafka"
settings:
url: "http://localhost:7071/metrics"
topics:
- "topic1"
- "topic2"
指标:
msg-rate-in-per-second[TOPIC]
: 每个主题每秒的传入消息数。msg-megabytes-in-per-second[TOPIC]
: 每个主题每秒的传入兆字节数。msg-megabytes-out-per-second[TOPIC]
: 每个主题每秒的传出兆字节数。
Prometheus
Prometheus 指标收集器持续抓取指标端点,将指标存储在内存中,并使用 PromQL 查询它们。
预计查询将返回一个包含单个序列的矩阵。查询应为范围查询,该查询将在测试开始和测试结束之间进行评估。
设置:
url
: Prometheus 指标端点的 URL。queries
: 要运行的查询数组。name
: 查询的名称。query
: PromQL 查询。unit
: 查询的单位(可选,仅用于在 CLI 中显示)。interval
: 范围查询的分辨率。
metrics:
my-prometheus-collector:
collector: "prometheus"
settings:
url: "http://localhost:8080/metrics"
queries:
- name: "http_request_success_rate"
query: "rate(request_count{endpoint=hello,status=200}[2s])"
unit: "req/s"
interval: "1s"
- name: "http_request_fail_rate"
query: "rate(request_count{endpoint=hello,status!=200}[2s])"
unit: "req/s"
interval: "1s"
指标是使用 queries
字段用户定义的。name
字段用作 CSV 输出中的指标名称。
故障排除
- Benchi 失败,并显示错误
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
。
Benchi 正在使用默认的 Docker 套接字与 Docker 通信。如果你使用的是 Docker Desktop,则可以在“设置”->“高级”下启用它,并选中“允许使用默认 Docker 套接字”的框。
许可证
Benchi 在 Apache 许可证 2.0 版下获得许可。有关更多详细信息,请参见 LICENSE
文件。
关于
从 CLI 对任何工具进行基准测试
主题
资源
许可证
Code of conduct
Code of conduct Activity Custom properties
Stars
Watchers
Forks
Releases 4
v0.4.0 Latest Mar 20, 2025 + 3 releases