Skip to content

Commit ed8cc0f

Browse files
committed
Added graph and updated text on index
1 parent ff8ea6c commit ed8cc0f

8 files changed

Lines changed: 397 additions & 35 deletions

File tree

graph/Charts.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
4+
class Charts{
5+
6+
private static $charts_count = 0;
7+
private static $colors = "['#3366cc','#dc3912','#ff9900','#109618','#990099','#0099c6','#dd4477']";
8+
9+
/**
10+
* Load data from CSV file
11+
*
12+
*/
13+
function load_csv( $file ){
14+
$file = file_get_contents( $file );
15+
$rows = explode("\n",$file);
16+
for( $i=0;$i<count($rows);$i++ )
17+
$this->data[$i] = explode( ",", $rows[$i] );
18+
}
19+
20+
/**
21+
* Load data from array
22+
*
23+
*/
24+
function set_data($array){
25+
$this->data = $array;
26+
}
27+
28+
29+
30+
/**
31+
* Draw line
32+
*
33+
*/
34+
function draw_line(){
35+
$this->_draw('line');
36+
}
37+
38+
39+
40+
/**
41+
* Draw pie
42+
*
43+
*/
44+
function draw_pie(){
45+
$this->_draw('pie');
46+
}
47+
48+
49+
50+
51+
52+
53+
private function _draw( $chart = 'line'){
54+
55+
$id = 'name' . self::$charts_count;
56+
57+
if( !self::$charts_count )
58+
$this->_init_script();
59+
self::$charts_count++;
60+
61+
$js = '<script>' . "\n";
62+
$js .= ' google.load("visualization", "1", {packages:["corechart"]});' . "\n";
63+
$js .= ' google.setOnLoadCallback(drawChart);' . "\n";
64+
$js .= ' function drawChart() {' . "\n";
65+
$js .= ' var data = new google.visualization.DataTable();' . "\n";
66+
67+
$data = $this->data;
68+
69+
$rows = count($data);
70+
$ncol = count($data[0]);
71+
72+
for( $i=0;$i<$ncol;$i++ )
73+
if($i==0)
74+
$js .= " data.addColumn('string', '{$data[0][$i]}' );" . "\n";
75+
else
76+
$js .= " data.addColumn('number', '{$data[0][$i]}' );" . "\n";
77+
78+
$js .= ' data.addRows('.($rows-1).');' . "\n";
79+
80+
81+
for($i=1;$i<$rows;$i++)
82+
for( $j=0;$j<$ncol;$j++)
83+
if($j==0)
84+
$js .= " data.setValue(".($i-1).", $j, '{$data[$i][$j]}' )". "\n";
85+
else
86+
$js .= " data.setValue(".($i-1).", $j, {$data[$i][$j]} )". "\n";
87+
88+
89+
if( $chart == 'line' ){
90+
$js .= " var chart = new google.visualization.LineChart(document.getElementById('$id'));" . "\n";
91+
$js .= " chart.draw(data, {lineType: 'function', colors:".self::$colors.", width:750, height: 300, vAxis: {maxValue: 10}} );" . "\n";
92+
}
93+
else{
94+
$js .= " var chart = new google.visualization.PieChart(document.getElementById('$id'));";
95+
$js .= " chart.draw(data, {width: 430, height: 350, colors:['#3366cc','#dc3912','#ff9900','#109618'], is3D:true });";
96+
}
97+
98+
$js .= ' }' . "\n";
99+
$js .= '</script>' . "\n";
100+
101+
$html = '<div id="'.$id.'"></div>';
102+
103+
echo $js;
104+
echo $html;
105+
106+
}
107+
108+
function _init_script(){
109+
$js = '<script type="text/javascript" src="https://www.google.com/jsapi"></script>' . "\n";
110+
echo $js;
111+
}
112+
113+
}
114+
115+
116+
?>
117+
118+
119+

graph/example_line.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require_once "../library/mysql.class.php";
4+
require_once "../library/functions.php";
5+
require_once "../library/Charts.php";
6+
7+
8+
$test = get('test')=='loop'?'loop':'assign';
9+
$type = get('type')=='memory'?'memory':'execution_time';
10+
11+
$charts = new Charts();
12+
$charts->load_csv( "../csv/{$test}_{$type}.csv" );
13+
$charts->draw_line();
14+
15+
?>

graph/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>403 Forbidden</title>
4+
</head>
5+
<body>
6+
7+
<p>Forbidden Access.</p>
8+
9+
</body>
10+
</html>

graph/line.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
1-
The charts library is part of the <a href="http://www.rainframework.com">RainFramework</a>
1+
<?php
2+
3+
require_once "../library/mysql.class.php";
4+
require_once "../library/functions.php";
5+
6+
7+
$db = new mysql;
8+
$db->connect() or die("db connection error");
9+
10+
$test = get('test')=='loop'?'loop':'assign';
11+
$type = get('type')=='memory'?'memory':'execution_time';
12+
$template_tested = $db->get_list( "SELECT template_engine FROM template_benchmark WHERE test='$test' GROUP BY template_engine ORDER BY template_engine", "template_engine", "template_engine" );
13+
14+
if( $template_selected = get( 'template' ) ){
15+
$where = "WHERE test='$test' AND (";
16+
$i=0;
17+
foreach( $template_selected as $tpl => $on ){
18+
$where .= $i==0?" template_engine='$tpl'":" OR template_engine='$tpl'";
19+
$i=1;
20+
}
21+
$where .= ")";
22+
}
23+
else
24+
$where = "WHERE test='$test'";
25+
26+
$rows = $db->get_list( "SELECT template_engine, n, avg(execution_time) AS execution_time, round(avg(memory)/1024) AS memory FROM template_benchmark $where GROUP BY template_engine, n ORDER BY n, template_engine" );
27+
$template_show = $db->get_list( "SELECT template_engine, avg(execution_time) AS execution_time FROM template_benchmark $where GROUP BY template_engine ORDER BY n, template_engine", "template_engine", "template_engine" );
28+
$nrows = $db->get_list( "SELECT n FROM template_benchmark $where GROUP BY n" );
29+
30+
$color = array('#3366cc','#dc3912','#ff9900','#109618','#990099','#0099c6','#dd4477' );
31+
$nc = sizeof($color);
32+
$color_sel = "colors:[";
33+
34+
$i=0;$cs=false;
35+
foreach( $template_tested as $template ){
36+
if( isset($template_show[ $template ] )){
37+
$color_sel .= $cs ? ",'".$color[$i%$nc] . "'" : "'".$color[$i%$nc] . "'" ;
38+
$cs=true;
39+
}
40+
$i++;
41+
}
42+
$color_sel .= "],";
43+
44+
45+
// create the execution time array
46+
$tpl_array = array();
47+
for( $i=0;$i<count($rows);$i++){
48+
if( !isset( $tpl_array[ $rows[$i]['n'] ] ) )
49+
$tpl_array[ $rows[$i]['n'] ][] = $rows[$i]['n'];
50+
$tpl_array[ $rows[$i]['n'] ][] = round( $rows[$i][$type] );
51+
}
52+
53+
$html = "data.addRows(".(count($nrows)).");\n";
54+
55+
// add column
56+
foreach( $template_show as $tpl )
57+
$html .= "data.addColumn('number', '$tpl');\n";
58+
59+
$row=0;
60+
foreach( $tpl_array as $num => $array ){
61+
for( $col=0;$col< count($array); $col++)
62+
$html .= $col==0 ? 'data.setValue('.$row.','.$col.','. "'" . $num. "'" . ");\n" : 'data.setValue('.$row.','.$col.','. ( $array[$col] ) . ");\n";
63+
$row++;
64+
}
65+
66+
?>
67+
68+
69+
<html>
70+
<head>
71+
<link href="../graph/style.css" type="text/css" rel="stylesheet"/>
72+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
73+
<script type="text/javascript">
74+
google.load("visualization", "1", {packages:["corechart"]});
75+
google.setOnLoadCallback(drawChart);
76+
function drawChart() {
77+
// Create and populate the data table.
78+
var data = new google.visualization.DataTable();
79+
data.addColumn('string', '<?php echo $type; ?>' );
80+
81+
<?php echo $html; ?>
82+
83+
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
84+
chart.draw(data, {lineType: "function", <?php echo $color_sel; ?> width:750, height: 300, vAxis: {maxValue: 10}} );
85+
}
86+
</script>
87+
</head>
88+
<body>
89+
<?php
90+
if( $type == 'memory' )
91+
echo "Memory (KB)";
92+
else
93+
echo "Execution Time (µs)"
94+
?>
95+
<div id="line_chart"></div>
96+
97+
<div style="margin-left:600px;">
98+
<?php
99+
if( $test == 'assign' )
100+
echo "Assigned variables";
101+
else
102+
echo "Array elements"
103+
?>
104+
</div>
105+
106+
</body>
107+
</html>

graph/line_charts.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require_once "../library/mysql.class.php";
4+
require_once "../library/functions.php";
5+
require_once "Charts.php";
6+
7+
$test = get('test')=='loop'?'loop':'assign';
8+
$type = get('type')=='memory'?'memory':'execution_time';
9+
10+
11+
$chart = new Charts;
12+
$chart->load_csv( "../csv/{$test}_{$type}.csv" );
13+
$chart->draw();
14+
15+
?>

graph/pie.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,97 @@
1-
The charts library is part of the <a href="http://www.rainframework.com">RainFramework</a>!
1+
<?php
2+
3+
require_once "../library/mysql.class.php";
4+
require_once "../library/functions.php";
5+
6+
$db = new mysql;
7+
$db->connect() or die("db connection error");
8+
9+
$test = get('test')=='loop'?'loop':'assign';
10+
$type = get('type')=='memory'?'memory':'execution_time';
11+
$summary = $db->get_list( "SELECT template_engine AS name, avg(execution_time) AS execution_time, avg(memory) AS memory FROM template_benchmark WHERE test='$test' GROUP BY template_engine ORDER BY template_engine" );
12+
13+
$template_tested = $db->get_list( "SELECT template_engine FROM template_benchmark WHERE test='$test' GROUP BY template_engine ORDER BY template_engine", "template_engine", "template_engine" );
14+
15+
if( $template_selected = get( 'template' ) ){
16+
$where = "WHERE test='$test' AND (";
17+
$i=0;
18+
foreach( $template_selected as $tpl => $on ){
19+
$where .= $i==0?" template_engine='$tpl'":" OR template_engine='$tpl'";
20+
$i=1;
21+
}
22+
$where .= ")";
23+
}
24+
else
25+
$where = "WHERE test='$test'";
26+
27+
$rows = $db->get_list( "SELECT template_engine, n, avg(execution_time) AS execution_time, avg(memory) AS memory FROM template_benchmark $where GROUP BY template_engine, n ORDER BY n, template_engine" );
28+
$template_show = $db->get_list( "SELECT template_engine, avg(execution_time) AS execution_time FROM template_benchmark $where GROUP BY template_engine ORDER BY n, template_engine", "template_engine", "template_engine" );
29+
$nrows = $db->get_list( "SELECT n FROM template_benchmark $where GROUP BY n" );
30+
31+
$color = array('#3366cc','#dc3912','#ff9900','#109618','#990099','#0099c6','#dd4477' );
32+
$color_sel = "colors:[";
33+
$nc = sizeof($color);
34+
$i=0;$cs=false;
35+
foreach( $template_tested as $template ){
36+
if( isset($template_show[ $template ] )){
37+
$color_sel .= $cs ? ",'".$color[$i%$nc] . "'" : "'".$color[$i%$nc] . "'" ;
38+
$cs=true;
39+
}
40+
$i++;
41+
}
42+
$color_sel .= "],";
43+
44+
45+
$tot = 0;
46+
foreach( $summary as $name => $res )
47+
$tot += round($res[$type]);
48+
49+
$pie = array();
50+
foreach( $summary as $name => $res ){
51+
if( isset( $template_show[$res['name']] ) ){
52+
53+
if( $type=='memory')
54+
$pie[$res['name']] = round( $res[$type] / 1024 );
55+
else
56+
$pie[$res['name']] = round( $res[$type] );
57+
}
58+
}
59+
60+
$html = "data.addColumn('number', 'speed');";
61+
$html .= "data.addRows(".(count($summary)).");\n";
62+
63+
$i=0;
64+
foreach( $pie as $name => $ex ){
65+
$html .= "data.setValue($i, 0,'$name');";
66+
$html .= "data.setValue($i, 1, $ex);";
67+
$i++;
68+
}
69+
70+
71+
72+
?>
73+
74+
75+
<html>
76+
<head>
77+
<link href="../graph/style.css" type="text/css" rel="stylesheet"/>
78+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
79+
<script type="text/javascript">
80+
google.load("visualization", "1", {packages:["corechart"]});
81+
google.setOnLoadCallback(drawChart);
82+
function drawChart() {
83+
// Create and populate the data table.
84+
var data = new google.visualization.DataTable();
85+
data.addColumn('string', '<?php echo $type=='memory'?'Memory (KB)':'Execution Time (µs)'; ?>' );
86+
87+
<?php echo $html; ?>
88+
89+
var chart = new google.visualization.PieChart(document.getElementById('pie_chart'));
90+
chart.draw(data, {width: 430, height: 350, <?echo $color_sel; ?> is3D:true, title: '<?php echo $type; ?>'});
91+
}
92+
</script>
93+
</head>
94+
<body>
95+
<div id="pie_chart"></div>
96+
</body>
97+
</html>

0 commit comments

Comments
 (0)