summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/serialization/performance/xml/harness.hpp
blob: 8955be216004302c2daeae954ff42f07230c5908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* /libs/serialization/xml_performance/harness.hpp *****************************

(C) Copyright 2010 Bryce Lelbach

Use, modification and distribution is subject to the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 
http://www.boost.org/LICENSE_1_0.txt)

*******************************************************************************/

#if !defined(BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP)
#define BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP

// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
  # pragma once
#endif

#include <cassert>
#include <cstdio> 

#include <string>
#include <fstream>
#include <iostream>
#include <utility>

#include <boost/config.hpp>

#if defined(BOOST_NO_STDC_NAMESPACE)
  namespace std { 
    using ::remove;
  }
#endif 

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/random_generator.hpp>

#include <boost/functional/hash.hpp>

#include <boost/utility/enable_if.hpp>

#include <boost/type_traits/is_integral.hpp>

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

#include <boost/archive/archive_exception.hpp>

#include "high_resolution_timer.hpp" // from /libs/spirit/optimization

#include "node.hpp" // includes macro.hpp

namespace boost {
namespace archive {
namespace xml {

template<typename T> T random (void);

template<typename T> T
random (void) {
  using namespace boost::uuids;

  hash<uuid> hash;
  basic_random_generator<mt19937> gen;

  return hash(gen());
}

template<> std::string
random<std::string> (void) {
  using namespace boost::uuids;

  basic_random_generator<mt19937> gen;
  uuid u = gen();

  return to_string(u);
}

template<typename T> std::string
save_archive (T const& s) {
  std::string fn = random<std::string>() +
    "-" BOOST_PP_STRINGIZE(BSL_TYPE)
    BOOST_PP_STRINGIZE(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH))
    ".xml"
  ;

  std::ofstream ofs(fn.c_str());
 
  assert(ofs.good());
  
  xml_oarchive oa(ofs);
  oa << BOOST_SERIALIZATION_NVP(s);

  ofs.close();
  return fn;
}

template<typename T> std::pair<double, T>
restore_archive (std::string fn) {
  std::ifstream ifs(fn.c_str());
  T s;

  assert(ifs.good());
  
  high_resolution_timer u;
  
  xml_iarchive ia(ifs);
  ia >> BOOST_SERIALIZATION_NVP(s);

  ifs.close();
  return std::pair<double, T>(u.elapsed(), s);
}

class result_set_exception: public virtual archive_exception {
 public:
  enum exception_code {
    invalid_archive_metadata
  };

  result_set_exception (exception_code c = invalid_archive_metadata){ }
  
  virtual const char* what() const throw() {
    const char *msg = "";
    
    switch (code) {
      case invalid_archive_metadata:
        msg = "result set was not created on this system";
      default:
        archive_exception::what();
    }
    
    return msg;
  }
};

struct entry {
  std::string type;
  std::size_t size;
  double      data;

  template<class ARC>
  void serialize (ARC& ar, const unsigned int) {
    ar & BOOST_SERIALIZATION_NVP(type)
       & BOOST_SERIALIZATION_NVP(size)
       & BOOST_SERIALIZATION_NVP(data)
    ;
  }

  entry (void) { }

  entry (std::string type, std::size_t size, double data):
    type(type), size(size), data(data) { }
};

struct result_set {
  std::string      compiler;
  std::string      platform;
  std::list<entry> entries;

  template<class ARC>
  void serialize (ARC& ar, const unsigned int) {
    ar & BOOST_SERIALIZATION_NVP(compiler)
       & BOOST_SERIALIZATION_NVP(platform)
       & BOOST_SERIALIZATION_NVP(entries)
    ;

    if (  (compiler != BOOST_COMPILER)
       || (platform != BOOST_PLATFORM))
         throw result_set_exception(); 
  }

  result_set (void):
    compiler(BOOST_COMPILER),
    platform(BOOST_PLATFORM) { }

  result_set (std::list<entry> entries):
    compiler(BOOST_COMPILER),
    platform(BOOST_PLATFORM),
    entries(entries) { }
};

} // xml
} // archive
} // boost

#endif // BOOST_SERIALIZATION_XML_PERFORMANCE_HARNESS_HPP