Open Source Web Development Tutorials - Dev Shed
タイマーを利用したPHPアプリのベンチマーク
(2009/01/22公開)
作動クラスの定義
今回作る最初のサンプルでは、第1回で定義したタイマーラスを利用して、データの少ない単純なデータベース行のコンテンツをフェッチし、画面に表示するPHPアプリケーションの性能をベンチマークします。
最初の例では、HTTP圧縮をまったく使わないで、データをクライアントに送信します。2番目の例では、すべてのデータを圧縮して、ネットワークへ転送します。
当然、タイマークラスを使って、この各アプリケーションの時間測定を行い、どちらの性能が優れているかを示します。なおテストはすべてローカルホストを使って行う点に留意してください。したがって、リモートサーバーで実行した場合、2つの例で現れる最終的な差異はさらに拡大する可能性があります。
以上を理解したところで、以下に、最初の例で使用するすべてのクラスを列挙します。確認してください。
// define 'MySQL' class
class MySQL{
private $conId;
private $host;
private $user;
private $password;
private $database;
private $result;
const OPTIONS=4;
public function __construct($options=array()){
if(count($options)!=self::OPTIONS){
throw new Exception('Invalid number of connection parameters');
}
foreach($options as $parameter=>$value){
if(!$value){
throw new Exception('Invalid parameter '.$parameter);
}
$this->{$parameter}=$value;
}
$this->connectDB();
}
// connect to MySQL
private function connectDB(){
if(!$this->conId=mysql_connect($this->host,$this->user,$this->password)){
throw new Exception('Error connecting to the server');
}
if(!mysql_select_db($this->database,$this->conId)){
throw new Exception('Error selecting database');
}
}
// run query
public function query($query){
if(!$this->result=mysql_query($query,$this->conId)){
throw new Exception('Error performing query '.$query);
}
return new Result($this,$this->result);
}
}
// define 'Result' class
class Result {
private $mysql;
private $result;
public function __construct($mysql,$result){
$this->mysql=$mysql;
$this->result=$result;
}
// fetch row
public function fetchRow(){
return mysql_fetch_assoc($this->result);
}
// count rows
public function countRows(){
if(!$rows=mysql_num_rows($this->result)){
throw new Exception('Error counting rows');
}
return $rows;
}
// count affected rows
public function countAffectedRows(){
if(!$rows=mysql_affected_rows($this->mysql->conId)){
throw new Exception('Error counting affected rows');
}
return $rows;
}
// get ID from last-inserted row
public function getInsertID(){
if(!$id=mysql_insert_id($this->mysql->conId)){
throw new Exception('Error getting ID');
}
return $id;
}
// seek row
public function seekRow($row=0){
if(!is_int($row)||$row<0){
throw new Exception('Invalid result set offset');
}
if(!mysql_data_seek($this->result,$row)){
throw new Exception('Error seeking data');
}
}
}
// define 'Timer' class
class Timer{
private $elapsedTime;
// start timer
public function start(){
if(!$this->elapsedTime=$this->getMicrotime()){
throw new Exception('Error obtaining start time!');
}
}
// stop timer
public function stop(){
if(!$this->elapsedTime=round($this->getMicrotime()-$this->elapsedTime,5)){
throw new Exception('Error obtaining stop time!');
}
return $this->elapsedTime;
}
//define private 'getMicrotime()' method
private function getMicrotime(){
list($useg,$seg)=explode(' ',microtime());
return ((float)$useg+(float)$seg);
}
}
以上です。ご覧のとおり、3つの簡単なクラスを定義しています。最初の2つは、MySQLに接続してさまざまな結果セットをフェッチするために使います。3つ目(すなわち、「タイマー」クラス)は、サンプルデータベーステーブルから複数行を読み出すプロセスの大まかな時間計測に使います。とても簡単でしょう?
これら複数のクラスを1つの例で使用する方法は分かりましたね。それでは、次に進みましょう。データベース行の読み出しをベンチマークする方法を紹介します。
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








