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 | |
parent | 4b17a974da7adf42d30f0658e63955bfe3f161a2 (diff) | |
download | sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.tar.gz sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.tar.bz2 sandhi-66d14812e6deaedf151cdb619797f00b057a28c0.zip |
gras: use json for performance
-rw-r--r-- | lib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/top_block_query.cpp | 39 | ||||
-rw-r--r-- | python/gras/query/__init__.py | 7 | ||||
-rw-r--r-- | python/gras/query/chart_factory.js | 5 | ||||
-rw-r--r-- | python/gras/query/chart_total_io_counts.js | 11 | ||||
-rw-r--r-- | python/gras/query/main.js | 25 | ||||
-rw-r--r-- | python/gras/query/utils.js | 46 |
7 files changed, 73 insertions, 62 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); } diff --git a/python/gras/query/__init__.py b/python/gras/query/__init__.py index 5789fab..e25e22d 100644 --- a/python/gras/query/__init__.py +++ b/python/gras/query/__init__.py @@ -15,11 +15,12 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(s): """Respond to a GET request.""" - if s.path.endswith('.xml'): + args = server_registry[s.server] + if s.path.endswith('.json'): s.send_response(200) - s.send_header("Content-type", "text/xml") + s.send_header("Content-type", "application/json") s.end_headers() - s.wfile.write(server_registry[s.server]['top_block'].query(s.path)) + s.wfile.write(args['top_block'].query(s.path)) return path = s.path if path.startswith('/'): path = path[1:] diff --git a/python/gras/query/chart_factory.js b/python/gras/query/chart_factory.js index 3d2b0bc..682a968 100644 --- a/python/gras/query/chart_factory.js +++ b/python/gras/query/chart_factory.js @@ -16,12 +16,11 @@ var gras_chart_get_registry = function() **********************************************************************/ function gras_chart_factory_setup(registry, point) { - var id = $('gras_stats:first', point).attr('id'); + var id = point.id; registry.top_id = id; $('#top_name').append(' - ' + id); - $('block', point).each(function(index, block) + $.each(point.blocks, function(id, block) { - var id = $(block).attr('id'); registry.block_ids.push(id); var container = $('#chart_designer_blocks'); var div = $('<div />'); diff --git a/python/gras/query/chart_total_io_counts.js b/python/gras/query/chart_total_io_counts.js index 5aaaf24..2f9ced3 100644 --- a/python/gras/query/chart_total_io_counts.js +++ b/python/gras/query/chart_total_io_counts.js @@ -16,15 +16,15 @@ function GrasChartTotalIoCounts(args) GrasChartTotalIoCounts.prototype.update = function(point) { - var block_data = $('block[id="' + this.block_id + '"]', point); + var block_data = point.blocks[this.block_id]; var ul = $('<ul />'); $('ul', this.div).remove(); //clear old lists this.div.append(ul); { - var init_time = parseInt($('init_time', block_data).text()); - var stats_time = parseInt($('stats_time', block_data).text()); - var tps = parseInt($('tps', block_data).text()); + var init_time = parseInt(block_data.init_time); + var stats_time = parseInt(block_data.stats_time); + var tps = parseInt(block_data.tps); var duration = (stats_time - init_time)/tps; var li = $('<li />'); var strong = $('<strong />').text('Elapsed' + ': '); @@ -48,9 +48,8 @@ GrasChartTotalIoCounts.prototype.update = function(point) var dir = contents[0]; var units = contents[1]; var key = contents[2]; - $(key, block_data).each(function(index, elem) + $.each(block_data[key], function(index, count) { - var count = parseInt($(elem).text()); var li = $('<li />'); var strong = $('<strong />').text(dir + index.toString() + ': '); var span = $('<span />').text(count.toString() + ' ' + units); diff --git a/python/gras/query/main.js b/python/gras/query/main.js index a27c0b7..80791b9 100644 --- a/python/gras/query/main.js +++ b/python/gras/query/main.js @@ -25,27 +25,24 @@ var gras_query_stats = function(registry) $.ajax({ type: "GET", async: true, - url: "/stats.xml", - dataType: "xml", - success: function(xml) + url: "/stats.json", + dataType: "json", + success: function(response) { registry.online = true; gras_chart_factory_online(registry); if (registry.overall_active) { - if ($(xml, "gras_stats") !== undefined) + if (!registry.init) { - if (!registry.init) - { - gras_chart_factory_setup(registry, xml); - try{gras_chart_load(registry);}catch(e){} - registry.init = true; - } - $.each(registry.active_charts, function(index, chart_info) - { - chart_info.chart.update(xml); - }); + gras_chart_factory_setup(registry, response); + try{gras_chart_load(registry);}catch(e){} + registry.init = true; } + $.each(registry.active_charts, function(index, chart_info) + { + chart_info.chart.update(response); + }); registry.timeout_handle = window.setTimeout(function() { diff --git a/python/gras/query/utils.js b/python/gras/query/utils.js index f5e5ee8..3e4fe4b 100644 --- a/python/gras/query/utils.js +++ b/python/gras/query/utils.js @@ -3,22 +3,26 @@ **********************************************************************/ var gras_extract_total_items = function(point, id) { - var block_data = $('block[id="' + id + '"]', point); + var block_data = point.blocks[id]; var total_items = 0; - $('items_consumed,items_produced', block_data).each(function() + $.each(block_data.items_produced, function(index, value) { - total_items += parseInt($(this).text()); + total_items += value; + }); + $.each(block_data.items_consumed, function(index, value) + { + total_items += value; }); return total_items; } var gras_extract_throughput_delta = function(p0, p1, id) { - var d0 = $('block[id="' + id + '"]', p0); - var d1 = $('block[id="' + id + '"]', p1); - var t0 = parseInt($('stats_time', d0).text()); - var t1 = parseInt($('stats_time', d1).text()); - var tps = parseInt($('tps', d0).text()); + var d0 = p0.blocks[id]; + var d1 = p1.blocks[id]; + var t0 = d0.stats_time; + var t1 = d1.stats_time; + var tps = d0.tps; var items0 = gras_extract_total_items(p0, id); var items1 = gras_extract_total_items(p1, id); return ((items1-items0)*tps)/(t1-t0); @@ -26,31 +30,31 @@ var gras_extract_throughput_delta = function(p0, p1, id) var gras_extract_throughput = function(point, id) { - var block_data = $('block[id="' + id + '"]', point); - var start_time = parseInt($('start_time', block_data).text()); - var stats_time = parseInt($('stats_time', block_data).text()); - var tps = parseInt($('tps', block_data).text()); + var block_data = point.blocks[id]; + var start_time = block_data.start_time; + var stats_time = block_data.stats_time; + var tps = block_data.tps; var total_items = gras_extract_total_items(point, id); return (total_items*tps)/(stats_time-start_time); } var gras_extract_stat_time_delta = function(p0, p1) { - var t0 = parseInt($('now', p0).text()); - var t1 = parseInt($('now', p1).text()); - var tps = parseInt($('tps', p0).first().text()); + var t0 = p0.now; + var t1 = p1.now; + var tps = p0.tps; return (t1-t0)/(tps); } var gras_extract_percent_times = function(point, id) { - var block_data = $('block[id="' + id + '"]', point); + var block_data = point.blocks[id]; var data = { - prep: parseInt($('total_time_prep', block_data).text()), - work: parseInt($('total_time_work', block_data).text()), - post: parseInt($('total_time_post', block_data).text()), - input: parseInt($('total_time_input', block_data).text()), - output: parseInt($('total_time_output', block_data).text()), + prep: block_data.total_time_prep, + work: block_data.total_time_work, + post: block_data.total_time_post, + input: block_data.total_time_input, + output: block_data.total_time_output, }; var total = 0; $.each(data, function(key, val) |