tree: f0d71d8ba9e14792a20ac5240b89516a80f0e135
  1. Sources/
  2. .gitignore
  3. benchmark_report.py
  4. Package.swift
  5. README.md
  6. run.sh
benchmarks/swift/README.md

Fory Swift Benchmark

This benchmark compares serialization and deserialization performance between Apache Fory, Protocol Buffers, and JSON in Swift.

Serializers Compared

  • Fory: Apache Fory Swift implementation (swift/Sources/Fory)
  • Protocol Buffers: apple/swift-protobuf
  • JSON: Foundation JSONEncoder and JSONDecoder

Benchmarked Data Types

Data TypeDescription
NumericStructSimple struct with 12 int32 fields
SampleComplex struct with primitives and array fields
MediaContentNested object graph with strings, enums, and ids
NumericStructListList of NumericStruct entries
SampleListList of Sample entries
MediaContentListList of MediaContent entries

Benchmark data is aligned with benchmarks/cpp_benchmark for cross-language comparison.

Quick Start

cd benchmarks/swift
./run.sh

Run Options

./run.sh --help

Supported flags:

  • --data <struct|sample|mediacontent|structlist|samplelist|mediacontentlist>
  • --serializer <fory|protobuf|json>
  • --duration <seconds>
  • --no-report
  • --no-copy-docs

Examples:

# Run only NumericStruct benchmarks
./run.sh --data struct

# Run only protobuf benchmarks
./run.sh --serializer protobuf

# Run a single serializer and datatype
./run.sh --data sample --serializer json --duration 5

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

Schema Mismatch Mode

Set FORY_BENCH_SCHEMA_MISMATCH=1 to run the Fory-only compatible-read schema-mismatch mode. This mode is off by default. When enabled, run with --serializer fory; protobuf and JSON benchmark modes fail with a configuration error. Fory serialization uses the normal v1 benchmark models, and Fory deserialization uses v2 models registered with the same Fory type IDs where one int32 field is widened to int64.

Manual Commands

# Build benchmark
swift build -c release

# Run benchmark executable
swift run -c release swift-benchmark --duration 3 --output results/benchmark_results.json

# Generate markdown report and plot
python3 benchmark_report.py --json-file results/benchmark_results.json --output-dir results

Output Files

After running ./run.sh, the following files are generated in benchmarks/swift/results/:

  • benchmark_results.json: raw benchmark metrics and serialized-size comparison
  • throughput.png: throughput comparison plot
  • README.md and REPORT.md: markdown report with hardware/runtime info and result tables

Notes

  • Protobuf Swift types are generated in Sources/SwiftBenchmarkProto/bench.pb.swift from Sources/SwiftBenchmarkProto/bench.proto.
  • Regenerate protobuf code:
protoc \
  --plugin=protoc-gen-swift=.build/arm64-apple-macosx/release/protoc-gen-swift-tool \
  --swift_opt=Visibility=Public \
  --swift_out=Sources/SwiftBenchmarkProto \
  --proto_path=Sources/SwiftBenchmarkProto \
  Sources/SwiftBenchmarkProto/bench.proto
  • The benchmark intentionally includes plain-model conversion for protobuf to mirror real-world usage.
  • Results vary across machines and runtime environments.