From 5e527213a2430bb3018e5eebd909aef294edf9b5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- .../cxx/tree/performance/serialization.cxx | 132 +++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 xsd-examples/cxx/tree/performance/serialization.cxx (limited to 'xsd-examples/cxx/tree/performance/serialization.cxx') diff --git a/xsd-examples/cxx/tree/performance/serialization.cxx b/xsd-examples/cxx/tree/performance/serialization.cxx new file mode 100644 index 0000000..e81fcbd --- /dev/null +++ b/xsd-examples/cxx/tree/performance/serialization.cxx @@ -0,0 +1,132 @@ +// file : cxx/tree/performance/serialization.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include // std::size_t +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include + +#include "time.hxx" +#include "test.hxx" + +using namespace std; + +bool +serialization (const char* file, unsigned long iter) +{ + try + { + using namespace xercesc; + + cerr << "serialization:" << endl; + + // Get the object model using the standard parsing function. + // + unique_ptr r ( + test::root_ (file, + xml_schema::flags::dont_initialize | + xml_schema::flags::dont_validate)); + + // Serialize it to the in-memory buffer. This makes sure the buffer + // pre-allocates enough memory. + // + xml_schema::namespace_infomap map; + map["t"].name = "test"; + map["t"].schema = "test.xsd"; + + MemBufFormatTarget ft (10240); + test::root_ (ft, *r, map, "UTF-8", + xml_schema::flags::dont_initialize | + xml_schema::flags::dont_pretty_print | + xml_schema::flags::no_xml_declaration); + + size_t size (ft.getLen ()); + cerr << " document size: " << size << " bytes" << endl + << " iterations: " << iter << endl; + + // Create XML serializer that we are going to use in all iterations. + // + const XMLCh ls_id[] = + {xercesc::chLatin_L, xercesc::chLatin_S, xercesc::chNull}; + + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + + // Use the error handler implementation provided by the XSD runtime. + // + xsd::cxx::tree::error_handler eh; + xsd::cxx::xml::dom::bits::error_handler_proxy ehp (eh); + + xml_schema::dom::unique_ptr writer ( + impl->createLSSerializer ()); + + DOMConfiguration* conf (writer->getDomConfig ()); + + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + conf->setParameter (XMLUni::fgDOMXMLDeclaration, false); + + xml_schema::dom::unique_ptr out (impl->createLSOutput ()); + + out->setByteStream (&ft); + + // Serialization loop. + // + os::time start; + + for (unsigned long i (0); i < iter; ++i) + { + // First serialize the object model to DOM. + // + xml_schema::dom::unique_ptr doc (test::root_ (*r, map)); + + ft.reset (); + + // Then serialize DOM to XML reusing the serializer we created above. + // + writer->write (doc.get (), out.get ()); + eh.throw_if_failed (); + } + + os::time end; + os::time time (end - start); + + 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 false; + } + catch (std::ios_base::failure const&) + { + cerr << "io failure" << endl; + return false; + } + + return true; +} -- cgit v1.1