Ticket #1418: measure-archive-cast.cpp.boost_1_42_0

File measure-archive-cast.cpp.boost_1_42_0, 4.4 KB (added by kab@…, 13 years ago)

update test program for boost 1.42

Line 
1// (C) Copyright 2007 Kim Barrett - kab@irobot.com
2// Use, modification and distribution is subject to the Boost Software
3// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5//
6// Example code for measuring the impact of archive_cast patch.
7// Compile, link, and run this program against release-mode
8// multi-threaded shared-libs builds of boost, with and without the
9// archive_cast patch, and compare the timings.
10//
11// Using gcc 4.3.2, T(patched) is about 3/4 T(unpatched) in my tests.
12// Replace "<LOCATION>" with patched / unpatched boost root
13// bjam toolset=gcc
14// variant=release
15// threading=multi
16// link=shared
17// --prefix=<LOCATION>
18// --build-dir=<LOCATION>/build
19// --layout=system
20// install
21// export LD_LIBRARY_PATH=<LOCATION>/lib
22// g++ -Wall -O3 -fPIC -pthread -DNDEBUG
23// -I<LOCATION>/include -L<LOCATION>/lib
24// -o <LOCATION>/measure-archive-cast measure-archive-cast.cpp
25// -lboost_serialization-mt -lpthread
26
27#include <iostream>
28#include <iomanip>
29#include <boost/archive/polymorphic_oarchive.hpp>
30#include <boost/archive/polymorphic_binary_oarchive.hpp>
31#include <boost/serialization/nvp.hpp>
32#include <boost/iostreams/stream.hpp>
33#include <boost/timer.hpp>
34
35namespace {
36
37struct Limits {
38 float minV;
39 float maxV;
40 float minA;
41 float maxA;
42 float minT;
43 float maxT;
44 double minAP;
45 double maxAP;
46
47 Limits() : minV(0.0), maxV(0.0), minA(0.0), maxA(0.0),
48 minT(0.0), maxT(0.0), minAP(0.0), maxAP(0.0) {
49 }
50
51 template<typename Archive>
52 void serialize(Archive& ar, unsigned int /* version */) {
53 ar & BOOST_SERIALIZATION_NVP(minV);
54 ar & BOOST_SERIALIZATION_NVP(maxV);
55 ar & BOOST_SERIALIZATION_NVP(minA);
56 ar & BOOST_SERIALIZATION_NVP(maxA);
57 ar & BOOST_SERIALIZATION_NVP(minT);
58 ar & BOOST_SERIALIZATION_NVP(maxT);
59 ar & BOOST_SERIALIZATION_NVP(minAP);
60 ar & BOOST_SERIALIZATION_NVP(maxAP);
61 }
62};
63
64struct AState {
65 double p;
66 float v;
67 float a;
68 float t;
69 double ap;
70 double psd;
71 double vsd;
72 double asd;
73 unsigned long tsh;
74 unsigned long tsl;
75 Limits limits;
76
77 bool enabled;
78
79 AState() : p(0.0), v(0.0), a(0.0), t(0.0), ap(0.0),
80 psd(0.0), vsd(0.0), asd(0.0), limits(), enabled(false) {
81 }
82
83 template<typename Archive>
84 void serialize(Archive& ar, unsigned int /* version */) {
85 ar & BOOST_SERIALIZATION_NVP(p);
86 ar & BOOST_SERIALIZATION_NVP(v);
87 ar & BOOST_SERIALIZATION_NVP(a);
88 ar & BOOST_SERIALIZATION_NVP(t);
89 ar & BOOST_SERIALIZATION_NVP(ap);
90 ar & BOOST_SERIALIZATION_NVP(psd);
91 ar & BOOST_SERIALIZATION_NVP(vsd);
92 ar & BOOST_SERIALIZATION_NVP(asd);
93 ar & BOOST_SERIALIZATION_NVP(tsh);
94 ar & BOOST_SERIALIZATION_NVP(tsl);
95 ar & BOOST_SERIALIZATION_NVP(limits);
96 ar & BOOST_SERIALIZATION_NVP(enabled);
97 }
98};
99
100struct MState {
101 AState state;
102 float v;
103 float c;
104 float ct;
105 float at;
106 float dt;
107
108 MState() : state(), v(0.0), c(0.0), ct(0.0), at(0.0), dt(0.0) { }
109
110 template<typename Archive>
111 void serialize(Archive& ar, unsigned int /* version */) {
112 ar & BOOST_SERIALIZATION_NVP(state);
113 ar & BOOST_SERIALIZATION_NVP(v);
114 ar & BOOST_SERIALIZATION_NVP(c);
115 ar & BOOST_SERIALIZATION_NVP(ct);
116 ar & BOOST_SERIALIZATION_NVP(at);
117 ar & BOOST_SERIALIZATION_NVP(dt);
118 }
119};
120
121}
122
123static const size_t nstates = 1000000;
124static const size_t bufferSize = 1000 + nstates * sizeof(MState);
125static char buffer[bufferSize];
126
127int main(int argc, char* argv[]) {
128 MState data;
129 boost::iostreams::stream<boost::iostreams::array_sink> outs(
130 buffer,
131 bufferSize);
132 boost::timer t;
133 {
134 boost::archive::polymorphic_binary_oarchive bar(
135 outs,
136 boost::archive::no_header | boost::archive::no_codecvt);
137 boost::archive::polymorphic_oarchive& ar = bar;
138 t.restart();
139 for (size_t i = 0; i < nstates; ++i) {
140 ar << boost::serialization::make_nvp("value", data);
141 }
142 } // run archive destructor to finish archive
143 outs.flush();
144 double dt = t.elapsed();
145 std::cout << "Wrote "
146 << nstates << " values of size " << sizeof(MState) << ", "
147 << outs.tellp() << " bytes, "
148 << dt << " seconds"
149 << std::endl;
150 return 0;
151}
152