Open Source Web Development Tutorials - Dev Shed
ベンチマークツールでファイルとデータベースを比較
(2009/01/29公開)
サンプルデータベーステーブルからデータを読み出す
引き続きPHPのベンチマーク機能をテストするために、まず、簡単な比較を実験してみます。すなわち、第2回と同じサンプルUSERSデータベーステーブルから行をフェッチする場合と、同じデータをフラットなテキストファイルから読み出す場合の比較です。
これで、ベンチマークの条件(実践例は、すべて、ローカル・サーバーを使ってテストしていることを思い出してください)下で、より短時間で実行される方法を特定できます。では、上述のデータベースから行をフェッチするスクリプトを見てください。また、コードスニペット(コード断片)が使用する対応クラスを、以下に列挙しました。
// 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(){
return microtime(true);
}
}
// 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 form 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');
}
}
}
try{
// instantiate 'Timer' class
$timer=new Timer();
// start timer
$timer->start();
// connect to MySQL
$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password','database'=>'database'));
$result=$db->query('SELECT * FROM users');
while($row=$result->fetchRow()){
echo 'ID: '.$row['id'].' Name: '.$row['name'].' Email: '.$row['email'].'<br />';
}
// stop timer
$elapsedTime=$timer->stop();
// display elapsed time
echo 'Time spent in fetching database rows was '.$elapsedTime.' seconds';
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
/* displays the following:
ID: 1 Name: user1 Email: user1@domain.com
ID: 2 Name: user2 Email: user2@domain.com
ID: 3 Name: user3 Email: user3@domain.com
ID: 4 Name: user4 Email: user4@domain.com
ID: 5 Name: user5 Email: user5@domain.com
ID: 6 Name: user6 Email: user6@domain.com
ID: 7 Name: user7 Email: user7@domain.com
ID: 8 Name: user8 Email: user8@domain.com
ID: 9 Name: user9 Email: user9@domain.com
ID: 10 Name: user10 Email: user10@domain.com
Time spent in fetching database rows was 0.00544 seconds
ご覧のとおり、上述のUSERSデータベース・テーブルに格納された10行の読み出しには、約0.0054秒を要しました。上に一覧表示したタイマークラスを使ってスクリプトをとても簡単にベンチマークできることが、このプログラムで実証されていますね。
では、各データベーステーブルからこれらの行を読み出すのに要した時間が判明したところで、同じ記録をフラットファイルから検索したら性能テストの結果は向上するでしょうか?面白そうですね。調べてみましょう。
さあ、このベンチマークテストの結果を知りたい人は次ページに進んでください。
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








