ベンチマーク結果をグラフで取得する
ベンチマーク結果の生の数値も役に立ちますが、棒グラフや円グラフのような形式で目にした方が役に立つ場合が少なくありません。fibonacci_iterative_timer.phpアプリケーションを拡張すると、Benchmark_Timerクラスを使ったベンチマークスクリプトにグラフ機能を追加できます。ここではSVGを使用して、ループの各反復に要した時間を視覚的に示す棒グラフと円グラフの画像を取得します。そのためには、測定結果の最後の列をグラフに描き、「ex time」列を視覚的に表現します。
<?php require_once 'Benchmark/Timer.php'; function fibonacci(){ //create an instance of Benchmark_Timer class $timer = new Benchmark_Timer(); //Set "Start" marker $timer->start(); $a=0;$b=1; for ($i = 0; $i < 15; $i++) { $s=$a+$b; //Set the markers $timer->setMarker('f'.$i); $a=$b; $b=$s; } //Set "Stop" marker $timer->stop(); //Returns formatted informations echo '<table>'; echo '<tr>'; echo '<td>'; $timer->display(); echo '</td>'; echo '<td>'; //Get the profiler info as an associative array $profiling = $timer->getProfiling(); //serialize the $profiling $ser = serialize($profiling); // Display all the information: name, time, // difference between two consecutive // markers, and total time echo "<embed src='DiagramSvg.php?profile=".$ser."' width='900' height='500' type='image/svg+xml' pluginspage='http://www.adobe.com/svg/viewer/install/' />"; echo '</td>'; echo '</tr>'; echo '</table>'; } //start the process fibonacci(); ?>
このコードは、生の測定結果を示すテーブルを含んだHTMLページを出力します。このデータからSVG形式のグラフを2つ生成し(リスト4を参照)、テーブルの右側の列にAdobe SVGプレーヤを使って表示します。
図1に、測定結果のテーブルとグラフを示します。Fibonacciメソッドの15回の反復結果を視覚的に示すことができます。
<?php //include Graph.php and Canvas.php require_once 'Image/Graph.php'; require_once 'Image/Canvas.php'; $getser = $_GET['profile']; $results = array(); $results = unserialize($getser); //create a canvas $Canvas = & Image_Canvas::factory( 'svg', array('width' => 800, 'height' => 440)); //create the graph $Graph =& Image_Graph::factory('graph', $Canvas); //set Helvetica font $Font =& $Graph->addNew('font', 'Helvetica'); //font size 10 pixels $Font->setSize(10); //set the font to the graph $Graph->setFont($Font); //create the plotarea $Graph->add( Image_Graph::vertical( Image_Graph::vertical( $Title = Image_Graph::factory( 'title', array('Timer (the last column from the table) ', 15)), $SubTitle = Image_Graph::factory( 'title', array('Bar and pie representation', 11)), 100), $Plotarea = Image_Graph::factory('plotarea'), 8)); //set the title and subtitle at the right of the graphic $Title->setAlignment(IMAGE_GRAPH_ALIGN_RIGHT); $SubTitle->setAlignment(IMAGE_GRAPH_ALIGN_RIGHT); //add a bar grid $Grid =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_X); //add a gradient background $Grid->setFillStyle(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_RADIAL, 'white', 'lightgrey'))); //add a bar graph $dataset = & Image_Graph::factory('dataset'); for($i=0;$i<sizeof($results);$i++) { //computing the graphic's values $dataset->addPoint($i,(($results[$i][diff]*100.0) / ($results[sizeof($results)-1][total]))); } $Plot =& $Plotarea->addNew('Image_Graph_Plot_Bar', &$dataset); $Plot->setLineColor('white'); $Plot->setFillColor('#cc0000'); $Marker =& Image_Graph::factory( 'Image_Graph_Marker_Value', IMAGE_GRAPH_VALUE_Y); $Plot->setMarker($Marker); $Marker->setFillColor('pink'); $Marker->setBorderColor('white'); $Plot =& $Plotarea->addNew('Image_Graph_Plot_Pie', &$dataset); $Plot->setLineColor('pink'); $Plot->setFillColor('#cc0000'); $Marker =& Image_Graph::factory( 'Image_Graph_Marker_Value', IMAGE_GRAPH_VALUE_Y); $Plot->setMarker($Marker); $Marker->setFillColor('pink'); $Marker->setBorderColor('white'); //show axis $AxisY = $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); $AxisY->showArrow(); $Array = array(); for($i=0;$i<sizeof($results);$i++) { $Array[$i] = $results[$i][name]; } $AxisX = $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); $AxisX -> setDataPreprocessor( Image_Graph::factory( 'Image_Graph_DataPreprocessor_Array', array($Array))); //display the graph $Graph->done(); ?>
ここまで見てきたとおり、このPEARパッケージを使ってPHPスクリプトにベンチマーク処理を追加するのは簡単で単純な作業です。この方法は、新しくコードを書くときだけでなく、既存のPHPコードのベンチマークが必要なときにも簡単に適用できます。しかも、ベンチマークの結果はわかりやすく、処理も簡単です。こうなると、PHPアプリケーションの最適化をサボる口実はほとんどなくなります。今は必要のないステップだと思っているものでも、後で大きな違いとなって現れてくることでしょう。

