This benchmark compares serialization and deserialization performance between Apache Fory, Protocol Buffers, and JSON in Swift.
swift/Sources/Fory)apple/swift-protobufJSONEncoder and JSONDecoder| Data Type | Description |
|---|---|
| NumericStruct | Simple struct with 12 int32 fields |
| Sample | Complex struct with primitives and array fields |
| MediaContent | Nested object graph with strings, enums, and ids |
| NumericStructList | List of NumericStruct entries |
| SampleList | List of Sample entries |
| MediaContentList | List of MediaContent entries |
Benchmark data is aligned with benchmarks/cpp_benchmark for cross-language comparison.
cd benchmarks/swift ./run.sh
./run.sh --help
Supported flags:
--data <struct|sample|mediacontent|structlist|samplelist|mediacontentlist>--serializer <fory|protobuf|json>--duration <seconds>--no-report--no-copy-docsExamples:
# 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
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.
# 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
After running ./run.sh, the following files are generated in benchmarks/swift/results/:
benchmark_results.json: raw benchmark metrics and serialized-size comparisonthroughput.png: throughput comparison plotREADME.md and REPORT.md: markdown report with hardware/runtime info and result tablesSources/SwiftBenchmarkProto/bench.pb.swift from Sources/SwiftBenchmarkProto/bench.proto.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