Ticket #9116: main.cpp

File main.cpp, 3.1 KB (added by Louis Zanella <louis.zanella@…>, 9 years ago)

Program to demonstrate the flaw

Line 
1#define ENABLE_BITWISE_SERIALIZATION
2#include "BigStruct.h"
3
4#pragma warning(disable:4244)
5#pragma warning(disable:4996)
6#include <boost/archive/binary_oarchive.hpp>
7#include <boost/serialization/vector.hpp>
8
9#include <sstream>
10
11int numObjectsToSerialize = 10000;
12
13void serializeBigStruct();
14void serializeBigStructArray();
15void serializeBigStructVector();
16
17int main(int argc, char* argv[])
18{
19 { // The following is to perform type registration of BigStruct within boost serialization...
20 std::ostringstream outStream;
21 boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);
22
23 BigStruct bigStruct;
24 memset(&bigStruct, 0, sizeof(bigStruct));
25 binOutArchive << bigStruct;
26 }
27
28 // Different test cases
29 serializeBigStructArray();
30 serializeBigStruct();
31 serializeBigStructVector();
32
33 return 0;
34}
35
36void serializeBigStruct()
37{
38 std::ostringstream outStream;
39 boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);
40
41 BigStruct bigStruct;
42 memset(&bigStruct, 0, sizeof(bigStruct));
43
44 std::cout << "\n*** Serializing 1 BigStruct object " << numObjectsToSerialize << " times ***\n";
45
46 std::clock_t c_start = std::clock();
47
48 // Serialize many times, to have a significant elapsed time
49 for (int i = 0; i < numObjectsToSerialize; ++i)
50 {
51 binOutArchive << bigStruct;
52 }
53
54 std::clock_t c_end = std::clock();
55
56 double elapsed_time_in_ms = 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC;
57
58 std::cout << "CPU time used: " << elapsed_time_in_ms << " ms" << std::endl;
59}
60
61void serializeBigStructArray()
62{
63 std::ostringstream outStream;
64 boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);
65
66 BigStruct bigStructArray[1];
67 memset(&bigStructArray, 0, sizeof(bigStructArray));
68
69 std::cout << "\n*** Serializing an array of 1 BigStruct object " << numObjectsToSerialize << " times ***\n";
70
71 std::clock_t c_start = std::clock();
72
73 // Serialize many times, to have a significant elapsed time
74 for (int i = 0; i < numObjectsToSerialize; ++i)
75 {
76 binOutArchive << bigStructArray;
77 }
78
79 std::clock_t c_end = std::clock();
80
81 double elapsed_time_in_ms = 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC;
82
83 std::cout << "CPU time used: " << elapsed_time_in_ms << " ms" << std::endl;
84}
85
86void serializeBigStructVector()
87{
88 std::ostringstream outStream;
89 boost::archive::binary_oarchive binOutArchive(outStream, boost::archive::no_header);
90
91 std::vector<BigStruct> bigStructVec;
92 for (int i = 0; i < numObjectsToSerialize; ++i)
93 {
94 BigStruct bigStruct;
95 memset(&bigStruct, 0, sizeof(bigStruct));
96 bigStructVec.push_back(bigStruct);
97 }
98
99 std::cout << "\n*** Serializing a vector of " << numObjectsToSerialize << " BigStruct objects once ***\n";
100
101 std::clock_t c_start = std::clock();
102
103 binOutArchive << bigStructVec;
104
105 std::clock_t c_end = std::clock();
106
107 double elapsed_time_in_ms = 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC;
108
109 std::cout << "CPU time used: " << elapsed_time_in_ms << " ms" << std::endl;
110}