Home | History | Annotate | Download | only in deep_memory_profiler
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2013 The Chromium Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 import json
      8 import sys
      9 from string import Template
     10 
     11 
     12 _HTML_TEMPLATE = """
     13 <html>
     14   <head>
     15     <script type='text/javascript' src='https://www.google.com/jsapi'></script>
     16     <script type='text/javascript'>
     17       google.load('visualization', '1', {packages:['corechart', 'table']});
     18       google.setOnLoadCallback(drawVisualization);
     19       function drawVisualization() {
     20         var data = google.visualization.arrayToDataTable(
     21           $JSON_ARRAY
     22         );
     23 
     24         var charOptions = {
     25           title: 'DMP Graph',
     26           vAxis: {title: 'Timestamp',  titleTextStyle: {color: 'red'}},
     27           isStacked : true
     28         };
     29 
     30         var chart = new google.visualization.BarChart(
     31             document.getElementById('chart_div'));
     32         chart.draw(data, charOptions);
     33 
     34         var table = new google.visualization.Table(
     35             document.getElementById('table_div'));
     36         table.draw(data);
     37       }
     38     </script>
     39   </head>
     40   <body>
     41     <div id='chart_div' style="width: 1024px; height: 800px;"></div>
     42     <div id='table_div' style="width: 1024px; height: 640px;"></div>
     43   </body>
     44 </html>
     45 """
     46 
     47 def _GenerateGraph(json_data, policy):
     48   legends = list(json_data['policies'][policy]['legends'])
     49   legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1:
     50                                  legends.index('UNTIL_HERE_FOR_TOTAL')]
     51   data = []
     52   for snapshot in json_data['policies'][policy]['snapshots']:
     53     data.append([0] * len(legends))
     54     for k, v in snapshot.iteritems():
     55       if k in legends:
     56         data[-1][legends.index(k)] = v
     57   print Template(_HTML_TEMPLATE).safe_substitute(
     58       {'JSON_ARRAY': json.dumps([legends] + data)})
     59 
     60 
     61 def main(argv):
     62   _GenerateGraph(json.load(file(argv[1], 'r')), argv[2])
     63 
     64 
     65 if __name__ == '__main__':
     66   sys.exit(main(sys.argv))
     67 
     68 
     69