Skip to content

Instantly share code, notes, and snippets.

@gruzovator
Last active August 29, 2015 14:17
Show Gist options
  • Save gruzovator/2e2a274dc2a282df922e to your computer and use it in GitHub Desktop.
Save gruzovator/2e2a274dc2a282df922e to your computer and use it in GitHub Desktop.
test xml via boost ptree generation
// cxx="g++" cxxflags="-O2"
/**
Test boost ptree perfomance.
2015-03-27
*/
#include <iostream>
#include <vector>
#include <sstream>
#include <chrono>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
//------------------------------------------------------------------------------
// CODE UNDER TEST
//------------------------------------------------------------------------------
/** typical web service reply data */
struct WebServiceDocument
{
//root
std::string rootNodeName;
size_t rootNodeId;
// children
std::vector<std::pair<std::string /*name*/, size_t/*id*/>> children;
};
/** typical way of making reply via ptree */
std::string makeServiceReplyPTree(const WebServiceDocument& doc)
{
using namespace boost::property_tree;
ptree docTree;
docTree.put("doc.name", doc.rootNodeName);
docTree.put("doc.id", doc.rootNodeId);
ptree docChild;
for(const auto& child: doc.children)
{
docChild.put("name", child.first);
docChild.put("id", child.second);
docTree.add_child("doc.children.child", docChild);
}
std::ostringstream oss;
xml_parser::write_xml(oss, docTree, xml_writer_make_settings(' ', 4));
return oss.str();
}
/** manual xml creation */
std::string makeServiceReplyManual(const WebServiceDocument& doc)
{
std::ostringstream oss;
oss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<< "<doc>\n"
<< "\t<name>" << doc.rootNodeName << "</name>\n"
<< "\t<id>" << doc.rootNodeId << "</id>\n"
<< "\t<children>\n";
for(const auto& child: doc.children)
{
oss << "\t\t<child>\n"
<< "\t\t\t<name>" << child.first << "</name>\n"
<< "\t\t\t<id>" << child.second << "</id>\n"
<< "\t\t</child>\n";
}
oss << "\t</children>\n"
<< "</doc>\n";
return oss.str();
}
//------------------------------------------------------------------------------
// TESTING
//------------------------------------------------------------------------------
using namespace std;
struct TestTimer
{
const string info;
const std::chrono::system_clock::time_point start;
TestTimer(const string& info): info(info), start(std::chrono::system_clock::now()) {}
~TestTimer() {
const std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
cout << "Test \"" << info << "\" duration " << std::chrono::duration<float>(end - start).count() << " s" << endl;
}
};
int main(int argc, char const *argv[])
try
{
using namespace std;
size_t docChildrenN = 500;
size_t repeatN = 100;
if(argc>1)
docChildrenN = boost::lexical_cast<size_t>(argv[1]);
if(argc>2)
repeatN = boost::lexical_cast<size_t>(argv[2]);
WebServiceDocument doc;
doc.rootNodeName = "root_node";
doc.rootNodeId = 42;
for(size_t i=0; i<docChildrenN; ++i)
{
string name = to_string(i) + " a tail";
size_t id = i;
doc.children.emplace_back(make_pair(name, id));
}
cout << "* Tests params: " << "doc children nodes = " << docChildrenN
<< "; repeat count = " << repeatN << endl;
cout << "* Running..." << endl;
// cout << makeServiceReplyPTree(doc).substr(0,500) << endl;
// cout << makeServiceReplyManual(doc).substr(0,500) << endl;
{
TestTimer timer("MANUAL XML REPLY");
string res;
for(size_t i=0; i<repeatN; ++i)
{
res = makeServiceReplyManual(doc);
}
}
{
TestTimer timer("PTREE XML REPLY");
string res;
for(size_t i=0; i<repeatN; ++i)
{
res = makeServiceReplyPTree(doc);
}
}
return 0;
}
catch(const exception& ex) {
cerr << "Exception: " << ex.what() << endl;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment