Skip to content

Commit

Permalink
Added zpp serialization library.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalz800 committed Apr 21, 2018
1 parent 4ed2bdb commit e9b64d4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
22 changes: 17 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
47 changes: 47 additions & 0 deletions benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "avro/record.hpp"
#include "flatbuffers/test_generated.h"
#include "yas/record.hpp"
#include "zpp/record.hpp"

#include "data.hpp"

Expand Down Expand Up @@ -518,6 +519,48 @@ 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::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++) {
std::vector<unsigned char> data;
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<std::chrono::milliseconds>(finish - start).count();

std::cout << "zpp: time = " << duration << " milliseconds" << std::endl << std::endl;
}

int
main(int argc, char** argv)
{
Expand Down Expand Up @@ -598,6 +641,10 @@ main(int argc, char** argv)
if (names.empty() || names.find("yas-compact") != names.end()) {
yas_serialization_test<yas::binary | yas::no_header | yas::compacted>(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;
Expand Down
51 changes: 51 additions & 0 deletions zpp/record.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#ifndef __ZPP_RECORD_HPP_INCLUDED__
#define __ZPP_RECORD_HPP_INCLUDED__

#include <vector>
#include <string>

#include <stdint.h>

#include "zpp/serializer.h"

namespace zpp_test {

typedef std::vector<int64_t> Integers;
typedef std::vector<std::string> 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 <typename Archive, typename Self>
static void serialize(Archive & archive, Self & self)
{
archive(self.ids, self.strings);
}
};

void to_string(const Record &record, std::string &string_data) {
std::vector<unsigned char> data;
zpp::serializer::memory_output_archive archive(data);
archive(record);
string_data.assign(reinterpret_cast<const char *>(data.data()), data.size());
}

void from_string(Record &record, const std::string &data) {
zpp::serializer::memory_view_input_archive archive(data.data(), data.size());
archive(record);
}

} // namespace

#endif

0 comments on commit e9b64d4

Please sign in to comment.