From 10bad9381daffa6cedf2a4d14feabe11c961edc2 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 10 Jan 2018 23:13:48 +0100 Subject: [PATCH] :construction: added size benchmark for binary formats --- benchmarks/Makefile | 5 ++++- benchmarks/src/formats.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 benchmarks/src/formats.cpp diff --git a/benchmarks/Makefile b/benchmarks/Makefile index ef2de8a3..463a1fbb 100644 --- a/benchmarks/Makefile +++ b/benchmarks/Makefile @@ -17,5 +17,8 @@ json_benchmarks_simple: src/benchmarks_simple.cpp ../src/json.hpp number_jsons: (test -e files/numbers/floats.json -a -e files/numbers/signed_ints.json -a -e files/numbers/unsigned_ints.json) || (cd files/numbers ; python generate.py) +json_formats: src/formats.cpp ../src/json.hpp number_jsons + $(CXX) -std=c++11 $(CXXFLAGS) -DNDEBUG -O3 -flto -I../src $(<) $(LDFLAGS) -o $@ + clean: - rm -f json_benchmarks json_benchmarks_simple files/numbers/*.json + rm -f json_benchmarks json_benchmarks_simple json_formats files/numbers/*.json diff --git a/benchmarks/src/formats.cpp b/benchmarks/src/formats.cpp new file mode 100644 index 00000000..6070e1b3 --- /dev/null +++ b/benchmarks/src/formats.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + std::vector files = { + "files/jeopardy/jeopardy.json", + "files/nativejson-benchmark/canada.json", + "files/nativejson-benchmark/citm_catalog.json", + "files/nativejson-benchmark/twitter.json", + "files/numbers/floats.json", + "files/numbers/signed_ints.json", + "files/numbers/unsigned_ints.json" + }; + + for (const auto& file: files) + { + std::ifstream f(file); + json j = json::parse(f); + auto v_cbor = json::to_cbor(j); + auto v_msgpack = json::to_msgpack(j); + auto v_ubjson = json::to_ubjson(j, true, true); + + double baseline = j.dump().size(); + + std::cout << file << ", JSON: " << j.dump(2).size() << " " << j.dump(2).size()/baseline << std::endl; + std::cout << file << ", JSON (minified): " << j.dump().size() << std::endl; + std::cout << file << ", CBOR: " << v_cbor.size() << " " << v_cbor.size()/baseline << std::endl; + std::cout << file << ", MessagePack: " << v_msgpack.size() << " " << v_msgpack.size()/baseline << std::endl; + std::cout << file << ", UBJSON (optimized): " << v_ubjson.size() << " " << v_ubjson.size()/baseline << std::endl; + } +}