harness.hpp 3.9 KB

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