From 75597e1cb626877dda50ab3da413d7e1b0e41e7c Mon Sep 17 00:00:00 2001 From: eyalz800 Date: Sat, 21 Apr 2018 15:26:16 +0300 Subject: [PATCH] Added zpp serialization library. --- CMakeLists.txt | 22 +++++++++++++++++----- benchmark.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ zpp/record.hpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 zpp/record.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f642a9..8283fd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,9 +13,9 @@ include(ExternalProject) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -CHECK_CXX_COMPILER_FLAG("-std=c++11" CXX11) -if (NOT CXX11) - message(FATAL_ERROR "C++ compiler doesn't support C++11") +CHECK_CXX_COMPILER_FLAG("-std=c++14" CXX14) +if (NOT CXX14) + message(FATAL_ERROR "C++ compiler doesn't support C++14") endif() CHECK_INCLUDE_FILES("inttypes.h" HAVE_INTTYPES_H) @@ -157,6 +157,18 @@ ExternalProject_Add( ) include_directories(${cereal_PREFIX}/include) +set(zpp_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/external/zpp) +ExternalProject_Add( + zpp + PREFIX ${zpp_PREFIX} + URL "https://github.com/eyalz800/serializer/archive/v0.1.tar.gz" + URL_MD5 "96f1db7d8f80917d730cbb1a8d7f2fbe" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND mkdir -p ${zpp_PREFIX}/include/ && cp -r ${zpp_PREFIX}/src/zpp ${zpp_PREFIX}/include/ +) +include_directories(${zpp_PREFIX}/include) + set(avro_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/external/avro) ExternalProject_Add( avro @@ -305,6 +317,6 @@ add_executable( ${FLATBUFFERS_SERIALIZATION_SOURCES} ${YAS_SERIALIZATION_SOURCES} ) -add_dependencies(benchmark thrift msgpack protobuf capnproto boost cereal avro flatbuffers yas) +add_dependencies(benchmark thrift msgpack protobuf capnproto boost cereal avro flatbuffers yas zpp) target_link_libraries(benchmark ${LINKLIBS}) -set_target_properties(benchmark PROPERTIES COMPILE_FLAGS "-std=c++11") +set_target_properties(benchmark PROPERTIES COMPILE_FLAGS "-std=c++14") diff --git a/benchmark.cpp b/benchmark.cpp index f109d03..d7626cb 100644 --- a/benchmark.cpp +++ b/benchmark.cpp @@ -27,6 +27,7 @@ #include "avro/record.hpp" #include "flatbuffers/test_generated.h" #include "yas/record.hpp" +#include "zpp/record.hpp" #include "data.hpp" @@ -518,6 +519,50 @@ yas_serialization_test(size_t iterations) } } +void +zpp_serialization_test(size_t iterations) +{ + using namespace zpp_test; + + Record r1, r2; + + for (size_t i = 0; i < kIntegers.size(); i++) { + r1.ids.push_back(kIntegers[i]); + } + + for (size_t i = 0; i < kStringsCount; i++) { + r1.strings.push_back(kStringValue); + } + + std::string serialized; + + to_string(r1, serialized); + from_string(r2, serialized); + + if (r1 != r2) { + throw std::logic_error("zpp' case: deserialization failed"); + } + + std::vector data; + data.reserve(serialized.size()); + + std::cout << "zpp: version = " << "v0.1" << std::endl; + std::cout << "zpp: size = " << serialized.size() << " bytes" << std::endl; + + auto start = std::chrono::high_resolution_clock::now(); + for (size_t i = 0; i < iterations; i++) { + zpp::serializer::memory_output_archive out(data); + out(r1); + + zpp::serializer::memory_input_archive in(data); + in(r1); + } + auto finish = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(finish - start).count(); + + std::cout << "zpp: time = " << duration << " milliseconds" << std::endl << std::endl; +} + int main(int argc, char** argv) { @@ -598,6 +643,10 @@ main(int argc, char** argv) if (names.empty() || names.find("yas-compact") != names.end()) { yas_serialization_test(iterations); } + + if (names.empty() || names.find("zpp") != names.end()) { + zpp_serialization_test(iterations); + } } catch (std::exception& exc) { std::cerr << "Error: " << exc.what() << std::endl; return EXIT_FAILURE; diff --git a/zpp/record.hpp b/zpp/record.hpp new file mode 100644 index 0000000..d3a988d --- /dev/null +++ b/zpp/record.hpp @@ -0,0 +1,51 @@ + +#ifndef __ZPP_RECORD_HPP_INCLUDED__ +#define __ZPP_RECORD_HPP_INCLUDED__ + +#include +#include + +#include + +#include "zpp/serializer.h" + +namespace zpp_test { + +typedef std::vector Integers; +typedef std::vector Strings; + +struct Record { + + Integers ids; + Strings strings; + + bool operator==(const Record &other) { + return (ids == other.ids && strings == other.strings); + } + + bool operator!=(const Record &other) { + return !(*this == other); + } + + template + static void serialize(Archive & archive, Self & self) + { + archive(self.ids, self.strings); + } +}; + +inline void to_string(const Record &record, std::string &string_data) { + std::vector data; + zpp::serializer::memory_output_archive archive(data); + archive(record); + string_data.assign(reinterpret_cast(data.data()), data.size()); +} + +inline void from_string(Record &record, const std::string &data) { + zpp::serializer::memory_view_input_archive archive(data.data(), data.size()); + archive(record); +} + +} // namespace + +#endif