1+ #include < chrono>
2+ #include < random>
3+ #include < boost/describe/class.hpp>
4+ #define ANKERL_NANOBENCH_IMPLEMENT
5+ #include " nanobench.h"
6+ #include " msgpack.h"
7+ #include " msgpack_sinks.h"
8+ #include " msgpack_describe.h"
9+
10+ using namespace std ::chrono_literals;
11+ using msgpackcpp::serialize;
12+ using msgpackcpp::deserialize;
13+ using msgpackcpp::sink;
14+
15+ template <class T , class Generator >
16+ T random (Generator& gen)
17+ {
18+ if constexpr (std::is_same_v<T, char > || std::is_same_v<T, int8_t > || std::is_same_v<T, uint8_t >)
19+ return std::uniform_int_distribution<int >{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
20+ else if constexpr (std::is_integral_v<T>)
21+ return std::uniform_int_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
22+ else if constexpr (std::is_floating_point_v<T>)
23+ return std::uniform_real_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
24+ }
25+
26+ std::string make_string ()
27+ {
28+ return " and talk gravely to each"
29+ " other; he read of the Obelisk in the Place de la Concorde that weeps"
30+ " tears of granite in its lonely sunless exile and longs to be back by"
31+ " the hot, lotus-covered Nile, where there are Sphinxes, and rose-red"
32+ " ibises, and white vultures with gilded claws, and crocodiles with small"
33+ " beryl eyes that crawl over the green steaming mud; he began to brood"
34+ " over those verses which, drawing music from kiss-stained marble, tell"
35+ " of that curious statue that Gautier compares to a contralto voice, the"
36+ " _monstre charmant_ that couches in the porphyry-room of the Louvre."
37+ " But after a time the book fell from his hand. He grew nervous, and a"
38+ " horrible fit of terror came over him. What if Alan Campbell should be"
39+ " out of England? Days would elapse before he could come back. Perhaps he"
40+ " might refuse to come. What could he do then? Every moment was of vital"
41+ " importance." ;
42+ }
43+
44+ namespace custom_namespace
45+ {
46+ struct custom_struct
47+ {
48+ char c;
49+ int8_t i8 ;
50+ uint8_t u8 ;
51+ int16_t i16 ;
52+ uint16_t u16 ;
53+ int32_t i32 ;
54+ uint32_t u32 ;
55+ int64_t i64 ;
56+ uint64_t u64 ;
57+ float f32 ;
58+ double f64 ;
59+ std::string str;
60+ };
61+
62+ BOOST_DESCRIBE_STRUCT (custom_struct, (), (
63+ c, i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , f32 , f64 ,
64+ str
65+ ))
66+ }
67+
68+ int main ()
69+ {
70+ std::mt19937_64 eng (std::chrono::high_resolution_clock::now ().time_since_epoch ().count ());
71+
72+ custom_namespace::custom_struct data;
73+ data.c = random<char >(eng);
74+ data.i8 = random<int8_t >(eng);
75+ data.u8 = random<uint8_t >(eng);
76+ data.i16 = random<int16_t >(eng);
77+ data.u16 = random<uint16_t >(eng);
78+ data.i32 = random<int32_t >(eng);
79+ data.u32 = random<uint32_t >(eng);
80+ data.i64 = random<int64_t >(eng);
81+ data.u64 = random<uint64_t >(eng);
82+ data.f32 = random<float >(eng);
83+ data.f64 = random<double >(eng);
84+ data.str = make_string ();
85+ std::vector<uint8_t > buf;
86+
87+ ankerl::nanobench::Bench ().minEpochTime (100ms).maxEpochTime (1s).run (" serialize" , [&] {
88+ auto out = sink (buf);
89+ serialize (out, data);
90+ // ankerl::nanobench::doNotOptimizeAway(d);
91+ });
92+ }
0 commit comments