Skip to content

Commit dffc961

Browse files
committed
feat(bench): add comprehensive performance benchmarking suite
- create `src/bench.zig` with complete performance benchmark coverage - implement benchmarks for basic types, strings, binary data, arrays, and maps - add extension type and timestamp performance tests - create complex structure and mixed-type performance scenarios - include detailed throughput (ops/sec) and latency (ns/op) metrics - add `bench` build target in `build.zig` - provide flexible benchmark runner with warmup and multiple iterations
1 parent 596086b commit dffc961

File tree

5 files changed

+900
-0
lines changed

5 files changed

+900
-0
lines changed

AGENT.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
src/
4343
├── msgpack.zig # 核心实现(主文件)
4444
├── test.zig # 完整测试套件
45+
├── bench.zig # 性能基准测试
4546
└── compat.zig # 跨版本兼容层
4647
```
4748

@@ -63,6 +64,13 @@ src/
6364
- 覆盖所有 MessagePack 类型
6465
- 测试边界条件和错误处理
6566
- 验证格式选择逻辑(最小编码原则)
67+
- 包含 Fuzz 测试覆盖随机数据
68+
69+
#### `src/bench.zig` (性能基准测试)
70+
- 基本类型序列化/反序列化性能测试
71+
- 不同大小容器(数组/Map)的性能对比
72+
- 嵌套结构和混合类型的实际场景测试
73+
- 提供详细的吞吐量和延迟指标
6674

6775
---
6876

@@ -350,6 +358,12 @@ error {
350358
# 运行所有测试
351359
zig build test
352360

361+
# 运行性能基准测试
362+
zig build bench
363+
364+
# 使用 Release 模式运行以获得准确性能数据
365+
zig build bench -Doptimize=ReleaseFast
366+
353367
# 详细输出
354368
zig build test --summary all
355369
```
@@ -385,6 +399,31 @@ test "描述性测试名称" {
385399
}
386400
```
387401

402+
### 8.4 基准测试
403+
404+
```bash
405+
# 运行所有基准测试
406+
zig build bench
407+
408+
# 使用 ReleaseFast 优化获取最佳性能数据
409+
zig build bench -Doptimize=ReleaseFast
410+
```
411+
412+
基准测试覆盖范围:
413+
- ✅ 基本类型(nil, bool, int, uint, float)的序列化/反序列化
414+
- ✅ 字符串和二进制数据(不同大小)
415+
- ✅ 数组和 Map(小型/中型/大型)
416+
- ✅ 扩展类型和 Timestamp
417+
- ✅ 嵌套结构和混合类型的实际场景
418+
419+
输出格式示例:
420+
```
421+
Benchmark Name | Iterations | ns/op | ops/sec
422+
------------------------------------------------------------------------
423+
Nil Write | 1000000 | 45 | 22222222
424+
Small Int Read | 1000000 | 123 | 8130081
425+
```
426+
388427
---
389428

390429
## 9. 开发最佳实践
@@ -468,6 +507,7 @@ while (iterator.next()) |entry| {
468507
- 使用 `fixedBufferStream` 避免动态分配
469508
- 预分配足够大的缓冲区
470509
- 批量写入时重用 Pack 实例
510+
- 参考 `bench.zig` 中的性能基准数据优化热点路径
471511

472512
### 11.2 反序列化优化
473513

@@ -529,6 +569,7 @@ try expect(arr[0] == 0xd6); // 检查是否为 FIXEXT4
529569
|------|---------|
530570
| 修改核心逻辑 | `src/msgpack.zig` |
531571
| 添加测试 | `src/test.zig` |
572+
| 添加/运行基准测试 | `src/bench.zig` |
532573
| 修复版本兼容 | `src/compat.zig` |
533574
| 更新构建配置 | `build.zig` |
534575

@@ -577,6 +618,14 @@ TIMESTAMP_EXT_TYPE: i8 = -1
577618

578619
### 示例
579620
```
621+
[2025-10-18] [Feature] 添加性能基准测试套件
622+
- 创建 src/bench.zig 完整基准测试文件
623+
- 覆盖所有主要类型的序列化/反序列化性能
624+
- 包含小/中/大规模容器的性能对比测试
625+
- 测试嵌套结构和混合类型的实际应用场景
626+
- 在 build.zig 中添加 `bench` 构建目标
627+
- 提供详细的吞吐量(ops/sec)和延迟(ns/op)指标输出
628+
580629
[2025-10-03] [Feature] 添加 Timestamp 支持
581630
- 实现三种 timestamp 格式(32/64/96 位)
582631
- 添加 Timestamp.toFloat() 转换方法

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ zig build test
190190
zig build test --summary all
191191
```
192192
193+
## Benchmarks
194+
195+
To run performance benchmarks:
196+
197+
```sh
198+
# Run benchmarks (default build mode)
199+
zig build bench
200+
201+
# Run with optimizations for accurate performance measurements
202+
zig build bench -Doptimize=ReleaseFast
203+
```
204+
205+
The benchmark suite includes:
206+
- Basic types (nil, bool, integers, floats)
207+
- Strings and binary data of various sizes
208+
- Arrays and maps (small, medium, large)
209+
- Extension types and timestamps
210+
- Nested structures and mixed-type payloads
211+
212+
Output provides throughput (ops/sec) and latency (ns/op) metrics for each operation.
213+
193214
## Documentation
194215
195216
To generate documentation for this library:

README_CN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ zig build test
190190
zig build test --summary all
191191
```
192192

193+
## 性能基准测试
194+
195+
运行性能基准测试:
196+
197+
```sh
198+
# 运行基准测试(默认构建模式)
199+
zig build bench
200+
201+
# 使用优化模式运行以获得准确的性能测量结果
202+
zig build bench -Doptimize=ReleaseFast
203+
```
204+
205+
基准测试套件包括:
206+
- 基本类型(nil、bool、整数、浮点数)
207+
- 不同大小的字符串和二进制数据
208+
- 数组和映射表(小型、中型、大型)
209+
- 扩展类型和时间戳
210+
- 嵌套结构和混合类型载荷
211+
212+
输出提供每个操作的吞吐量(ops/sec)和延迟(ns/op)指标。
213+
193214
## 文档
194215

195216
要生成此库的文档:

build.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub fn build(b: *std.Build) void {
1717

1818
const test_step = b.step("test", "Run unit tests");
1919

20+
const bench_step = b.step("bench", "Run benchmarks");
21+
2022
const msgpack_unit_tests = if (builtin.zig_version.minor == 14) b.addTest(.{
2123
.root_source_file = b.path(b.pathJoin(&.{ "src", "test.zig" })),
2224
.target = target,
@@ -37,6 +39,32 @@ pub fn build(b: *std.Build) void {
3739
msgpack_unit_tests.root_module.addImport("msgpack", msgpack);
3840
const run_msgpack_tests = b.addRunArtifact(msgpack_unit_tests);
3941
test_step.dependOn(&run_msgpack_tests.step);
42+
43+
// Add benchmark executable
44+
const benchmark = if (builtin.zig_version.minor == 14) b.addExecutable(.{
45+
.name = "msgpack-bench",
46+
.root_source_file = b.path(b.pathJoin(&.{ "src", "bench.zig" })),
47+
.target = target,
48+
.optimize = optimize,
49+
}) else if (builtin.zig_version.minor >= 16) b.addExecutable(.{
50+
.name = "msgpack-bench",
51+
.root_module = b.createModule(.{
52+
.root_source_file = b.path(b.pathJoin(&.{ "src", "bench.zig" })),
53+
.target = target,
54+
.optimize = optimize,
55+
}),
56+
}) else b.addExecutable(.{
57+
.name = "msgpack-bench",
58+
.root_module = b.addModule("bench", .{
59+
.root_source_file = b.path(b.pathJoin(&.{ "src", "bench.zig" })),
60+
.target = target,
61+
.optimize = optimize,
62+
}),
63+
});
64+
benchmark.root_module.addImport("msgpack", msgpack);
65+
66+
const run_benchmark = b.addRunArtifact(benchmark);
67+
bench_step.dependOn(&run_benchmark.step);
4068
}
4169

4270
fn generateDocs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget) void {

0 commit comments

Comments
 (0)