Skip to content

Commit 8a39ff1

Browse files
committed
added msgpack test
1 parent 70b676d commit 8a39ff1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

msgpack/record.hpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __MSGPACK_RECORD_HPP_INCLUDED__
2+
#define __MSGPACK_RECORD_HPP_INCLUDED__
3+
4+
#include <vector>
5+
#include <string>
6+
7+
#include <stdint.h>
8+
9+
#include <msgpack.hpp>
10+
11+
namespace msgpack_test {
12+
13+
typedef std::vector<int64_t> Integers;
14+
typedef std::vector<std::string> Strings;
15+
16+
class Record {
17+
public:
18+
19+
Integers ids;
20+
Strings strings;
21+
22+
bool operator==(const Record &other) {
23+
return (ids == other.ids && strings == other.strings);
24+
}
25+
26+
bool operator!=(const Record &other) {
27+
return !(*this == other);
28+
}
29+
30+
MSGPACK_DEFINE(ids, strings);
31+
};
32+
33+
} // namespace
34+
35+
#endif

test.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "boost/record.hpp"
1919

20+
#include "msgpack/record.hpp"
21+
2022
const size_t kItegersCount = 1000;
2123
const size_t kStringsCount = 100;
2224
const int64_t kIntegerValue = 26354;
@@ -155,6 +157,55 @@ boost_serialization_test(size_t iterations)
155157
std::cout << "boost: time = " << duration << " milliseconds" << std::endl << std::endl;
156158
}
157159

160+
void
161+
msgpack_serialization_test(size_t iterations)
162+
{
163+
using namespace msgpack_test;
164+
165+
Record r1, r2;
166+
167+
for (size_t i = 0; i < kItegersCount; i++) {
168+
r1.ids.push_back(kIntegerValue);
169+
}
170+
171+
for (size_t i = 0; i < kStringsCount; i++) {
172+
r1.strings.push_back(kStringValue);
173+
}
174+
175+
msgpack::sbuffer sbuf;
176+
177+
msgpack::pack(sbuf, r1);
178+
179+
std::string serialized(sbuf.data(), sbuf.size());
180+
181+
msgpack::unpacked msg;
182+
msgpack::unpack(&msg, serialized.data(), serialized.size());
183+
184+
msgpack::object obj = msg.get();
185+
186+
obj.convert(&r2);
187+
188+
if (r1 != r2) {
189+
throw std::logic_error("msgpack's case: deserialization failed");
190+
}
191+
192+
std::cout << "msgpack: size = " << serialized.size() << " bytes" << std::endl;
193+
194+
auto start = std::chrono::high_resolution_clock::now();
195+
for (size_t i = 0; i < iterations; i++) {
196+
sbuf.clear();
197+
msgpack::pack(sbuf, r1);
198+
msgpack::unpacked msg;
199+
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
200+
msgpack::object obj = msg.get();
201+
obj.convert(&r2);
202+
}
203+
auto finish = std::chrono::high_resolution_clock::now();
204+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count();
205+
206+
std::cout << "msgpack: time = " << duration << " milliseconds" << std::endl << std::endl;
207+
}
208+
158209
int
159210
main(int argc, char **argv)
160211
{
@@ -175,6 +226,7 @@ main(int argc, char **argv)
175226
thrift_serialization_test(iterations);
176227
protobuf_serialization_test(iterations);
177228
boost_serialization_test(iterations);
229+
msgpack_serialization_test(iterations);
178230
} catch (std::exception &exc) {
179231
std::cerr << "Error: " << exc.what() << std::endl;
180232
return EXIT_FAILURE;

0 commit comments

Comments
 (0)