tree: b436963daae196730edccae33caf972e7c22a1a3 [path history] [tgz]
  1. benchmark_report.py
  2. benchmark_test.go
  3. go.mod
  4. go.sum
  5. models.go
  6. proto_convert.go
  7. README.md
  8. run.sh
benchmarks/go_benchmark/README.md

Go Serialization Benchmark

This directory contains benchmarks comparing Apache Fory with Protocol Buffers and MessagePack for Go serialization.

Serializers Compared

  • Fory: Apache Fory Go implementation - high-performance cross-language serialization
  • Protocol Buffers: Google's protobuf via google.golang.org/protobuf
  • MessagePack: Efficient binary serialization via github.com/vmihailenco/msgpack/v5

Data Types Benchmarked

Data TypeDescription
NumericStructSimple struct with 8 int32 fields
SampleComplex struct with primitives and 7 array types
MediaContentNested objects with strings, enums, and lists
StructListList of NumericStruct (20 elements)
SampleListList of Sample (20 elements)
MediaContentListList of MediaContent (20 elements)

The benchmark data matches the C++ benchmark for cross-language comparison.

Quick Start

Prerequisites

  • Go 1.21+
  • Protocol Buffers compiler (protoc)

Install protoc:

# macOS
brew install protobuf

# Ubuntu/Debian
apt-get install protobuf-compiler

# Or download from https://github.com/protocolbuffers/protobuf/releases

Run Benchmarks

# Run all benchmarks
./run.sh

# Run specific data type
./run.sh --data struct
./run.sh --data sample
./run.sh --data mediacontent
./run.sh --data structlist
./run.sh --data samplelist
./run.sh --data mediacontentlist

# Run specific serializer
./run.sh --serializer fory
./run.sh --serializer protobuf
./run.sh --serializer msgpack

# Combine filters
./run.sh --data struct --serializer fory

# Customize benchmark parameters
./run.sh --count 10 --benchtime 2s

# Skip report generation
./run.sh --no-report

Manual Run

# Generate protobuf code
mkdir -p proto
protoc --proto_path=../proto \
    --go_out=proto \
    --go_opt=paths=source_relative \
    ../proto/bench.proto

# Download dependencies
go mod tidy

# Run benchmarks
go test -bench=. -benchmem

# Run specific benchmark
go test -bench=BenchmarkFory_Struct -benchmem

Results

Example results on Apple M1 Pro:

Data TypeOperationFory (ops/s)Protobuf (ops/s)Msgpack (ops/s)Fory vs PBFory vs MP
StructSerialize15.16M10.23M5.41M1.48x2.80x
StructDeserialize12.10M11.01M3.23M1.10x3.75x
StructlistSerialize1.58M561K299K2.82x5.28x
StructlistDeserialize1.10M529K175K2.09x6.30x
SampleSerialize7.28M2.72M670K2.68x10.87x
SampleDeserialize3.79M2.37M376K1.60x10.09x
SamplelistSerialize510K141K38K3.61x13.34x
SamplelistDeserialize236K107K19K2.20x12.66x
MediacontentSerialize3.72M2.12M1.29M1.75x2.88x
MediacontentDeserialize2.34M1.81M698K1.30x3.35x
MediacontentlistSerialize268K110K72K2.44x3.72x
MediacontentlistDeserialize138K87K36K1.58x3.86x

Note: Results vary by hardware. Run benchmarks on your own system for accurate comparisons. List benchmarks are included in the generated report.

Serialized Data Sizes (bytes)

Data TypeForyProtobufMsgpack
Struct586157
Sample446375524
MediaContent342301400
StructList56012601146
SampleList7600756010486
MediaContentList577660808006

Benchmark Methodology

Fair Comparison

The benchmarks follow the same methodology as the C++ benchmark:

  1. Conversion Cost Included: For protobuf, the benchmark includes converting plain Go structs to/from protobuf messages, matching real-world usage patterns.

  2. Buffer Reuse: Each serializer uses its optimal pattern for buffer management.

  3. Same Test Data: Test data matches the C++ benchmark exactly for cross-language comparisons.

What's Measured

  • Serialize: Time to convert a Go struct to bytes
  • Deserialize: Time to convert bytes back to a Go struct
  • Memory: Allocations per operation (with -benchmem)

Output Files

After running ./run.sh:

  • benchmark_results.txt - Human-readable benchmark output
  • benchmark_results.json - JSON format for programmatic analysis
  • benchmark_report.md - Generated markdown report
  • benchmark_*.png - Performance comparison charts (requires matplotlib)

Directory Structure

go_benchmark/
├── README.md              # This file
├── go.mod                 # Go module definition
├── models.go              # Data structures (NumericStruct, Sample, MediaContent)
├── proto_convert.go       # Protobuf conversion utilities
├── benchmark_test.go      # Benchmark tests
├── run.sh                 # Build and run script
├── benchmark_report.py    # Report generation script
└── proto/                 # Generated protobuf code (after running)
    └── bench.pb.go

Contributing

When adding new benchmarks:

  1. Add data structures to models.go
  2. Add protobuf conversions to proto_convert.go
  3. Add benchmark functions to benchmark_test.go
  4. Update this README with new data types

License

Licensed under the Apache License, Version 2.0.