1// MessagePack for C++ example
2//
3// Copyright (C) 2013-2015 KONDO Takatoshi
4//
5//    Distributed under the Boost Software License, Version 1.0.
6//    (See accompanying file LICENSE_1_0.txt or copy at
7//    http://www.boost.org/LICENSE_1_0.txt)
8//
9
10// g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
11// export LD_LIBRARY_PATH=path_to_boost_lib
12
13#include <msgpack.hpp>
14#include <string>
15#include <iostream>
16#include <sstream>
17#include <map>
18#include <boost/timer/timer.hpp>
19
20void test_map_pack_unpack() {
21    std::cout << "[TEST][map_pack_unpack]" << std::endl;
22    // setup
23    std::cout << "Setting up map data..." << std::endl;
24    std::map<int, int> m1;
25    int const num = 30000000L;
26    for (int i = 0; i < num; ++i) m1[i] = i;
27    std::cout << "Start packing..." << std::endl;
28    std::stringstream buffer;
29    {
30        boost::timer::cpu_timer timer;
31        msgpack::pack(buffer, m1);
32        std::string result = timer.format();
33        std::cout << result << std::endl;
34    }
35    std::cout << "Pack finished..." << std::endl;
36
37    buffer.seekg(0);
38    std::string str(buffer.str());
39
40    msgpack::unpacked unpacked;
41    std::cout << "Start unpacking...by void unpack(unpacked& result, const char* data, size_t len)" << std::endl;
42    {
43        boost::timer::cpu_timer timer;
44        msgpack::unpack(unpacked, str.data(), str.size());
45        std::string result = timer.format();
46        std::cout << result << std::endl;
47    }
48    std::cout << "Unpack finished..." << std::endl;
49    std::map<int, int> m2;
50    std::cout << "Start converting..." << std::endl;
51    {
52        boost::timer::cpu_timer timer;
53        unpacked.get().convert(m2);
54        std::string result = timer.format();
55        std::cout << result << std::endl;
56    }
57    std::cout << "Convert finished..." << std::endl;
58}
59
60int main(void)
61{
62    test_map_pack_unpack();
63}
64