Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 1 | /* /libs/serialization/xml_performance/harness.hpp ***************************** |
| 2 | |
| 3 | (C) Copyright 2010 Bryce Lelbach |
| 4 | |
| 5 | Use, modification and distribution is subject to the Boost Software License, |
| 6 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | http://www.boost.org/LICENSE_1_0.txt) |
| 8 | |
| 9 | *******************************************************************************/ |
| 10 | |
| 11 | #if !defined(BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP) |
| 12 | #define BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP |
| 13 | |
| 14 | // MS compatible compilers support #pragma once |
| 15 | #if defined(_MSC_VER) |
| 16 | # pragma once |
| 17 | #endif |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <cstdio> |
| 21 | |
| 22 | #include <string> |
| 23 | #include <fstream> |
| 24 | #include <iostream> |
| 25 | #include <utility> |
| 26 | |
| 27 | #include <boost/config.hpp> |
| 28 | |
| 29 | #if defined(BOOST_NO_STDC_NAMESPACE) |
| 30 | namespace std { |
| 31 | using ::remove; |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | #include <boost/uuid/uuid.hpp> |
| 36 | #include <boost/uuid/uuid_io.hpp> |
| 37 | #include <boost/uuid/random_generator.hpp> |
| 38 | |
| 39 | #include <boost/functional/hash.hpp> |
| 40 | |
| 41 | #include <boost/utility/enable_if.hpp> |
| 42 | |
| 43 | #include <boost/type_traits/is_integral.hpp> |
| 44 | |
| 45 | #include <boost/archive/xml_iarchive.hpp> |
| 46 | #include <boost/archive/xml_oarchive.hpp> |
| 47 | |
| 48 | #include <boost/archive/archive_exception.hpp> |
| 49 | |
| 50 | #include "high_resolution_timer.hpp" // from /libs/spirit/optimization |
| 51 | |
| 52 | #include "node.hpp" // includes macro.hpp |
| 53 | |
| 54 | namespace boost { |
| 55 | namespace archive { |
| 56 | namespace xml { |
| 57 | |
| 58 | template<typename T> T random (void); |
| 59 | |
| 60 | template<typename T> T |
| 61 | random (void) { |
| 62 | using namespace boost::uuids; |
| 63 | |
| 64 | hash<uuid> hash; |
| 65 | basic_random_generator<mt19937> gen; |
| 66 | |
| 67 | return hash(gen()); |
| 68 | } |
| 69 | |
| 70 | template<> std::string |
| 71 | random<std::string> (void) { |
| 72 | using namespace boost::uuids; |
| 73 | |
| 74 | basic_random_generator<mt19937> gen; |
| 75 | uuid u = gen(); |
| 76 | |
| 77 | return to_string(u); |
| 78 | } |
| 79 | |
| 80 | template<typename T> std::string |
| 81 | save_archive (T const& s) { |
| 82 | std::string fn = random<std::string>() + |
| 83 | "-" BOOST_PP_STRINGIZE(BSL_TYPE) |
| 84 | BOOST_PP_STRINGIZE(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH)) |
| 85 | ".xml" |
| 86 | ; |
| 87 | |
| 88 | std::ofstream ofs(fn.c_str()); |
| 89 | |
| 90 | assert(ofs.good()); |
| 91 | |
| 92 | xml_oarchive oa(ofs); |
| 93 | oa << BOOST_SERIALIZATION_NVP(s); |
| 94 | |
| 95 | ofs.close(); |
| 96 | return fn; |
| 97 | } |
| 98 | |
| 99 | template<typename T> std::pair<double, T> |
| 100 | restore_archive (std::string fn) { |
| 101 | std::ifstream ifs(fn.c_str()); |
| 102 | T s; |
| 103 | |
| 104 | assert(ifs.good()); |
| 105 | |
| 106 | high_resolution_timer u; |
| 107 | |
| 108 | xml_iarchive ia(ifs); |
| 109 | ia >> BOOST_SERIALIZATION_NVP(s); |
| 110 | |
| 111 | ifs.close(); |
| 112 | return std::pair<double, T>(u.elapsed(), s); |
| 113 | } |
| 114 | |
| 115 | class result_set_exception: public virtual archive_exception { |
| 116 | public: |
| 117 | enum exception_code { |
| 118 | invalid_archive_metadata |
| 119 | }; |
| 120 | |
| 121 | result_set_exception (exception_code c = invalid_archive_metadata){ } |
| 122 | |
| 123 | virtual const char* what() const throw() { |
| 124 | const char *msg = ""; |
| 125 | |
| 126 | switch (code) { |
| 127 | case invalid_archive_metadata: |
| 128 | msg = "result set was not created on this system"; |
| 129 | default: |
| 130 | archive_exception::what(); |
| 131 | } |
| 132 | |
| 133 | return msg; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | struct entry { |
| 138 | std::string type; |
| 139 | std::size_t size; |
| 140 | double data; |
| 141 | |
| 142 | template<class ARC> |
| 143 | void serialize (ARC& ar, const unsigned int) { |
| 144 | ar & BOOST_SERIALIZATION_NVP(type) |
| 145 | & BOOST_SERIALIZATION_NVP(size) |
| 146 | & BOOST_SERIALIZATION_NVP(data) |
| 147 | ; |
| 148 | } |
| 149 | |
| 150 | entry (void) { } |
| 151 | |
| 152 | entry (std::string type, std::size_t size, double data): |
| 153 | type(type), size(size), data(data) { } |
| 154 | }; |
| 155 | |
| 156 | struct result_set { |
| 157 | std::string compiler; |
| 158 | std::string platform; |
| 159 | std::list<entry> entries; |
| 160 | |
| 161 | template<class ARC> |
| 162 | void serialize (ARC& ar, const unsigned int) { |
| 163 | ar & BOOST_SERIALIZATION_NVP(compiler) |
| 164 | & BOOST_SERIALIZATION_NVP(platform) |
| 165 | & BOOST_SERIALIZATION_NVP(entries) |
| 166 | ; |
| 167 | |
| 168 | if ( (compiler != BOOST_COMPILER) |
| 169 | || (platform != BOOST_PLATFORM)) |
| 170 | throw result_set_exception(); |
| 171 | } |
| 172 | |
| 173 | result_set (void): |
| 174 | compiler(BOOST_COMPILER), |
| 175 | platform(BOOST_PLATFORM) { } |
| 176 | |
| 177 | result_set (std::list<entry> entries): |
| 178 | compiler(BOOST_COMPILER), |
| 179 | platform(BOOST_PLATFORM), |
| 180 | entries(entries) { } |
| 181 | }; |
| 182 | |
| 183 | } // xml |
| 184 | } // archive |
| 185 | } // boost |
| 186 | |
| 187 | #endif // BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP |
| 188 | |