Serialization Benchmark
This benchmark compares the performance of different serialization formats: Protobuf, Symphony, Cap'n Proto, and FlatBuffers.
Schema
All serialization formats use the same message structure:
message BenchmarkMessage {
int32 id = 1;
int32 score = 2;
string username = 3;
string content = 4;
}
Metrics Tested
The benchmark measures the following performance metrics for each serialization format:
- Time per operation: Nanoseconds per marshal operation
- Memory allocations: Number of allocations per operation
- Memory usage: Bytes allocated per operation
- CPU profiling: CPU usage patterns during marshaling
- Time per operation: Nanoseconds per unmarshal operation
- Memory allocations: Number of allocations per operation
- Memory usage: Bytes allocated per operation
- CPU profiling: CPU usage patterns during unmarshaling
3. Serialized Data Size
- Bytes: Size of the serialized message in bytes
- Protobuf - Google's Protocol Buffers
- Symphony - Custom serialization format (part of aRPC)
- Cap'n Proto - Infinitely fast serialization
- FlatBuffers - Google's zero-copy serialization library
Test Data
The benchmark uses the following test data:
- ID: 42
- Score: 300
- Username: "alice"
- Content: "hello world"
Running the Benchmark
Execute the benchmark script:
./benchmark.sh
This script will:
- Generate code for all serialization formats
- Build the Go module dependencies
- Run the benchmark tests
- Generate performance measurements and CPU profiles
Output
The benchmark generates several output files in the results/ directory:
Timing Results
marshal_timing_results.log - Marshal performance measurements
unmarshal_timing_results.log - Unmarshal performance measurements
CPU Profiles
protobuf_marshal.prof - Protobuf marshal CPU profile
symphony_marshal.prof - Symphony marshal CPU profile
capnproto_marshal.prof - Cap'n Proto marshal CPU profile
flatbuffers_marshal.prof - FlatBuffers marshal CPU profile
protobuf_unmarshal.prof - Protobuf unmarshal CPU profile
symphony_unmarshal.prof - Symphony unmarshal CPU profile
capnproto_unmarshal.prof - Cap'n Proto unmarshal CPU profile
flatbuffers_unmarshal.prof - FlatBuffers unmarshal CPU profile
Analyzing CPU Profiles
Use Go's pprof tool to analyze the CPU profiles:
go tool pprof results/protobuf_marshal.prof
go tool pprof results/symphony_marshal.prof
go tool pprof results/capnproto_marshal.prof
go tool pprof results/flatbuffers_marshal.prof
Common pprof commands:
top - Show top functions by CPU usage
list <function> - Show source code for a function
web - Open web interface
png - Generate PNG visualization
Prerequisites
- Go 1.23 or later
- flatc (FlatBuffers compiler)
- capnp (Cap'n Proto compiler)
- protoc (Protocol Buffers compiler)
- protoc-gen-go (Go plugin for protoc)
- protoc-gen-symphony (Symphony plugin for protoc)