summaryrefslogtreecommitdiff
path: root/examples/cxx/parser/performance/driver.cxx
blob: ed56f6321f1afabb3b7fcb75354e8c75d55c9827 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// file      : examples/cxx/parser/performance/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <string>
#include <memory>  // std::auto_ptr
#include <cstddef> // std::size_t
#include <fstream>
#include <sstream>
#include <iostream>

#include "time.hxx"
#include "test-pskel.hxx"

#ifdef _XERCES_VERSION
#  include <xercesc/sax2/SAX2XMLReader.hpp>
#  include <xercesc/sax2/XMLReaderFactory.hpp>
#  include <xercesc/framework/MemBufInputSource.hpp>
#  include <xercesc/validators/common/Grammar.hpp>
#  include <xercesc/util/PlatformUtils.hpp>
#  include <xercesc/util/XMLUni.hpp>

#  include <xsd/cxx/xml/sax/bits/error-handler-proxy.hxx>
#  include <xsd/cxx/parser/error-handler.hxx>
#endif

// No-op parser implementation.
//
namespace test
{
  struct enum_pimpl: enum_pskel, xml_schema::string_pimpl
  {
    virtual void
    post_enum ()
    {
    }
  };

  struct record_pimpl: record_pskel
  {
    virtual void
    int_ (unsigned int)
    {
    }

    virtual void
    double_ (double)
    {
    }

    virtual void
    name (const std::string&)
    {
    }

    virtual void
    string (const std::string&)
    {
    }

    virtual void
    choice1 (const std::string&)
    {
    }

    virtual void
    choice2 (const std::string&)
    {
    }

    virtual void
    choice3 (const std::string&)
    {
    }

    virtual void
    choice4 (const std::string&)
    {
    }

    virtual void
    apple (bool)
    {
    }

    virtual void
    orange (unsigned long long)
    {
    }
  };

  struct root_pimpl: root_pskel
  {
  };
}

using namespace std;

int
main (int argc, char* argv[])
{
  if (argc < 2)
  {
    cerr << "usage: " << argv[0] << " [-v] [-i <count>] test.xml" << endl
         << "\t -v turn on validation (default is off)" << endl
         << "\t -i number of iterations to perform (default is 1000)" << endl;
    return 1;
  }

  bool validate (false);
  unsigned long iter (1000);
  const char* file (0);

  // Parse command line arguments.
  //
  for (int i (1); i < argc; ++i)
  {
    string arg (argv[i]);

    if (arg == "-v")
    {
      validate = true;
    }
    else if (arg == "-i")
    {
      if (++i == argc)
      {
        cerr << "argument expected for the -i option" << endl;
        return 1;
      }

      iter = 0;
      istringstream is (argv[i]);
      is >> iter;

      if (iter == 0)
      {
        cerr << "invalid argument for the -i option" << endl;
        return 1;
      }
    }
    else
    {
      file = argv[i];
      break;
    }
  }

  if (file == 0)
  {
    cerr << "no input file specified" << endl;
    return 1;
  }

  try
  {
    // Instantiate and connect parsers.
    //
    xml_schema::unsigned_int_pimpl unsigned_int_p;
    xml_schema::double_pimpl double_p;
    xml_schema::ncname_pimpl ncname_p;
    xml_schema::string_pimpl string_p;
    xml_schema::boolean_pimpl boolean_p;
    xml_schema::unsigned_long_pimpl unsigned_long_p;

    test::enum_pimpl enum_p;
    test::record_pimpl record_p;
    test::root_pimpl root_p;

    record_p.parsers (unsigned_int_p,
                      double_p,
                      ncname_p,
                      string_p,
                      string_p,
                      string_p,
                      string_p,
                      string_p,
                      enum_p,
                      boolean_p,
                      unsigned_long_p);

    root_p.parsers (record_p);

    // Read the fine into in-memory buffer.
    //
    ifstream ifs;
    ifs.exceptions (ios_base::failbit);
    ifs.open (file, ios::in | ios::ate);

    size_t size (ifs.tellg ());
    ifs.seekg (0, ios::beg);

    char* buf = new char[size];
    ifs.read (buf, size);
    ifs.close ();

    cerr << "document size:  " << size << " bytes" << endl
         << "iterations:     " << iter << endl;

    os::time time (0);
    xml_schema::document doc (root_p, "test", "root");

#ifdef _XERCES_VERSION

    // Xerces-C++ as the underlying XML parser.
    //
    using namespace xercesc;

    namespace xml = xsd::cxx::xml;
    namespace parser = xsd::cxx::parser;

    XMLPlatformUtils::Initialize ();

    {
      MemBufInputSource is (
        reinterpret_cast<XMLByte*> (buf), size, file, false);
      is.setCopyBufToStream (false);

      auto_ptr<SAX2XMLReader> parser (XMLReaderFactory::createXMLReader ());

      parser->setFeature (XMLUni::fgSAX2CoreNameSpaces, true);
      parser->setFeature (XMLUni::fgSAX2CoreNameSpacePrefixes, true);
      parser->setFeature (XMLUni::fgXercesValidationErrorAsFatal, true);

      if (validate)
      {
        parser->setFeature (XMLUni::fgSAX2CoreValidation, true);
        parser->setFeature (XMLUni::fgXercesSchema, true);
        parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);

        // Xerces-C++ 3.1.0 is the first version with working multi import
        // support.
        //
#if _XERCES_VERSION >= 30100
        parser->setFeature (XMLUni::fgXercesHandleMultipleImports, true);
#endif

        // Initialize the schema cache. To detect schema errors we will
        // need an error handler.
        //
        parser::error_handler<char> eh;
        xml::sax::bits::error_handler_proxy<char> ehp (eh);
        parser->setErrorHandler (&ehp);

        if (!parser->loadGrammar ("test.xsd", Grammar::SchemaGrammarType, true))
        {
          // In Xerces-C++ grammar loading failure results in just a warning.
          // Make it a fatal error.
          //
          eh.handle ("test.xsd", 0, 0,
                     parser::error_handler<char>::severity::fatal,
                     "unable to load schema");
        }

        eh.throw_if_failed ();
        parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);

#if _XERCES_VERSION >= 30100
        parser->setFeature (XMLUni::fgXercesLoadSchema, false);
#endif
      }
      else
      {
        parser->setFeature (XMLUni::fgSAX2CoreValidation, false);
        parser->setFeature (XMLUni::fgXercesSchema, false);
        parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
      }

      os::time start;

      for (unsigned long i (0); i < iter; ++i)
      {
        root_p.pre ();
        doc.parse (is, *parser);
        root_p.post_root ();
      }

      os::time end;
      time = end - start;
    }

    XMLPlatformUtils::Terminate ();

#else

    // Expat as the underlying XML parser.
    //
    XML_Parser xml_parser (XML_ParserCreateNS (0, ' '));
    string public_id (file);

    os::time start;

    for (unsigned long i (0); i < iter; ++i)
    {
      // Using the low-level Expat-specific API to parse the memory
      // buffer.
      //
      root_p.pre ();
      doc.parse_begin (xml_parser, public_id);

      XML_Parse (xml_parser, buf, size, 1);

      doc.parse_end ();
      root_p.post_root ();

      XML_ParserReset (xml_parser, 0);
    }

    os::time end;
    time = end - start;

    XML_ParserFree (xml_parser);

#endif

    delete[] buf;

    cerr << "time:           " << time << " sec" << endl;

    double ms (time.sec () * 1000000ULL + time.nsec () / 1000ULL);

    // Calculate throughput in documents/sec.
    //
    double tpd ((iter / ms) * 1000000);
    cerr << "throughput:     " << tpd << " documents/sec" << endl;

    // Calculate throughput in MBytes/sec.
    //
    double tpb (((size * iter) / ms) * 1000000/(1024*1024));
    cerr << "throughput:     " << tpb << " MBytes/sec" << endl;
  }
  catch (const xml_schema::exception& e)
  {
    cerr << e << endl;
    return 1;
  }
  catch (std::ios_base::failure const&)
  {
    cerr << "io failure" << endl;
    return 1;
  }
}