Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / deep_memory_profiler / graph.py
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 = """<!DOCTYPE html>
13 <script src="https://www.google.com/jsapi"></script>
14 <script>
15 var all_data = $ALL_DATA;
16 google.load('visualization', '1', {packages:['corechart', 'table']});
17 google.setOnLoadCallback(drawVisualization);
18 function drawVisualization() {
19   // Apply policy 'l2' by default.
20   var default_policy = '$DEF_POLICY';
21   document.getElementById(default_policy).style.fontWeight = 'bold';
22   turnOn(default_policy);
23 }
24
25 function turnOn(policy) {
26   var data = google.visualization.arrayToDataTable(all_data[policy]);
27   var charOptions = {
28     title: 'DMP Graph (Policy: ' + policy + ')',
29     hAxis: {title: 'Timestamp',  titleTextStyle: {color: 'red'}},
30     isStacked : true
31   };
32   var chart = new google.visualization.AreaChart(
33       document.getElementById('chart_div'));
34   chart.draw(data, charOptions);
35   var table = new google.visualization.Table(
36       document.getElementById('table_div'));
37   table.draw(data);
38 }
39
40 window.onload = function() {
41   var ul = document.getElementById('policies');
42   for (var i = 0; i < ul.children.length; ++i) {
43     var li = ul.children[i];
44     li.onclick = function() {
45       for (var j = 0; j < ul.children.length; ++j) {
46         var my_li = ul.children[j];
47         my_li.style.fontWeight = 'normal';
48       }
49       this.style.fontWeight = 'bold';
50       turnOn(this.id);
51     }
52   }
53 };
54 </script>
55 <style>
56 #policies li {
57   display: inline-block;
58   padding: 5px 10px;
59 }
60 </style>
61 Click to change an applied policy.
62 <ul id="policies">$POLICIES</ul>
63 <div id="chart_div" style="width: 1024px; height: 640px;"></div>
64 <div id="table_div" style="width: 1024px; height: 640px;"></div>
65 """
66
67 def _GenerateGraph(json_data):
68   policies = list(json_data['policies'])
69
70   default_policy = "l2"
71   if default_policy not in policies:
72     default_policy = policies[0]
73
74   policies = "".join(map(lambda x: '<li id="'+x+'">'+x+'</li>', policies))
75
76   all_data = {}
77   for policy in json_data['policies']:
78     legends = list(json_data['policies'][policy]['legends'])
79     legends = ['second'] + legends[legends.index('FROM_HERE_FOR_TOTAL') + 1:
80                                      legends.index('UNTIL_HERE_FOR_TOTAL')]
81     data = []
82     for snapshot in json_data['policies'][policy]['snapshots']:
83       data.append([0] * len(legends))
84       for k, v in snapshot.iteritems():
85         if k in legends:
86           data[-1][legends.index(k)] = v
87     all_data[policy] = [legends] + data
88
89   print Template(_HTML_TEMPLATE).safe_substitute(
90       {'POLICIES': policies,
91        'DEF_POLICY': default_policy,
92        'ALL_DATA': json.dumps(all_data)})
93
94
95 def main(argv):
96   _GenerateGraph(json.load(file(argv[1], 'r')))
97
98
99 if __name__ == '__main__':
100   sys.exit(main(sys.argv))