diff options
author | Josh Blum | 2013-03-26 03:51:45 -0500 |
---|---|---|
committer | Josh Blum | 2013-03-26 03:51:45 -0500 |
commit | 66d14812e6deaedf151cdb619797f00b057a28c0 (patch) | |
tree | 3f8009612305ed7a7fa590cbcdfa5e99e9245e75 /lib | |
parent | 4b17a974da7adf42d30f0658e63955bfe3f161a2 (diff) | |
download | sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.tar.gz sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.tar.bz2 sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.zip |
gras: use json for performance
Diffstat (limited to 'lib')
-rw-r--r-- | lib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/top_block_query.cpp | 39 |
2 files changed, 26 insertions, 15 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index fa15efa..ff36a5a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -7,7 +7,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) ######################################################################## # Setup Boost ######################################################################## -find_package(Boost COMPONENTS thread date_time) +find_package(Boost COMPONENTS thread date_time regex system) include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) list(APPEND GRAS_LIBRARIES ${Boost_LIBRARIES}) diff --git a/lib/top_block_query.cpp b/lib/top_block_query.cpp index a52d89b..a8091d2 100644 --- a/lib/top_block_query.cpp +++ b/lib/top_block_query.cpp @@ -4,7 +4,8 @@ #include <gras/top_block.hpp> #include <boost/foreach.hpp> #include <boost/property_tree/ptree.hpp> -#include <boost/property_tree/xml_parser.hpp> +#include <boost/property_tree/json_parser.hpp> +#include <boost/regex.hpp> #include <sstream> using namespace gras; @@ -24,6 +25,17 @@ struct GetStatsReceiver : Theron::Receiver std::vector<GetStatsMessage> messages; }; +//http://stackoverflow.com/questions/13464383/boost-property-write-json-incorrect-behaviour +static std::string my_write_json(const boost::property_tree::ptree &pt) +{ + boost::regex exp("\"(null|true|false|[0-9]+(\\.[0-9]+)?)\""); + std::stringstream ss; + boost::property_tree::json_parser::write_json(ss, pt); + std::string rv = boost::regex_replace(ss.str(), exp, "$1"); + + return rv; +} + std::string TopBlock::query(const std::string &) { //get stats with custom receiver and set high prio @@ -39,16 +51,16 @@ std::string TopBlock::query(const std::string &) //create root level node boost::property_tree::ptree root; + root.put("id", this->to_string()); root.put("now", time_now()); root.put("tps", time_tps()); //iterate through blocks + boost::property_tree::ptree blocks; BOOST_FOREACH(const GetStatsMessage &message, receiver.messages) { const BlockStats &stats = message.stats; boost::property_tree::ptree block; - block.put("<xmlattr>.id", message.block_id); - block.put("id", message.block_id); block.put("tps", time_tps()); block.put("stats_time", message.stats_time); block.put("init_time", stats.init_time); @@ -61,10 +73,13 @@ std::string TopBlock::query(const std::string &) block.put("total_time_post", stats.total_time_post); block.put("total_time_input", stats.total_time_input); block.put("total_time_output", stats.total_time_output); - #define my_block_ptree_append(l) \ - for (size_t i = 0; i < stats.l.size(); i++) { \ - boost::property_tree::ptree t; t.put_value(stats.l[i]); \ - block.push_back(std::make_pair(#l, t)); \ + #define my_block_ptree_append(l) { \ + boost::property_tree::ptree e; \ + for (size_t i = 0; i < stats.l.size(); i++) { \ + boost::property_tree::ptree t; t.put_value(stats.l[i]); \ + e.push_back(std::make_pair("", t)); \ + } \ + block.push_back(std::make_pair(#l, e)); \ } my_block_ptree_append(items_consumed); my_block_ptree_append(tags_consumed); @@ -72,13 +87,9 @@ std::string TopBlock::query(const std::string &) my_block_ptree_append(items_produced); my_block_ptree_append(tags_produced); my_block_ptree_append(msgs_produced); - root.push_back(std::make_pair("block", block)); + blocks.push_back(std::make_pair(message.block_id, block)); } + root.push_back(std::make_pair("blocks", blocks)); - //create top tag and write xml - boost::property_tree::ptree top; - top.put_child("gras_stats", root); - std::stringstream ss; - write_xml(ss, top); - return ss.str(); + return my_write_json(root); } |