Level 8 速度の測定

作成したトランザクションの速度を測定する方法を提案し、実際に、速度を測定せよ。 最低でも数百トランザクションのテストが必要である。手動で測定することは出来ない。プログラムを作成せよ。 WWWベースであれば、このテストを書くには、HTTPを話すプログラムを書く必要がある。しかし、WWWベースである必要があるのだろうか?

Level7で作成したプログラムを変更し、処理速度を測定するプログラムを書き加える。
処理前と処理後の時間を比較して速度を測る。

プログラム

update2.php
/*時刻を取得する関数*/
function getmicrotime(){
    list($msec, $sec) = explode(" ", microtime());
    return ((float)$sec + (float)$msec);
}

$con=oci_connect(USER,PASS,DB);

//print "ID =>";
//$ID = chop(fgets(STDIN));

/*SQLよりIDを取得*/
$sql="select ITEM_ID from stock";
$ID=oci_parse($con,$sql); 
oci_execute($ID,OCI_DEFAULT); 

/*取得したIDを配列 id_data に格納*/
$i=0;
while(oci_fetch($ID)){
    $id_data[$i++]=oci_result($ID,"ITEM_ID");
}                                                                     

$MAX = 1000;    //測定の回数

for($i=0;$i<$MAX;$i++){        

$Stime = getmicrotime();   //処理前の時刻

$sql="lock table ".$tablename." in exclusive mode nowait";
$stmt=oci_parse($con,$sql);

    $sql="update stock set ITEM_STOCK = ITEM_STOCK -1  where ITEM_ID = ".$id_data[$i]."";
    $stmt=oci_parse($con,$sql);
    if(!oci_execute($stmt,OCI_DEFAULT)){
        oci_rollback($con);
    }else{
        oci_commit($con);
    }

$Etime = getmicrotime();  //処理後の時刻

$tm[$i] = $Etime - $Stime;  //処理時間
print"$tm[$i] s\n";
$stm = $stm + $tm[$i];  //測定した時間の合計
}
$atm = $stm/$MAX;  //測定した時間の平均
print"Sum = $stm s \n";
print"Ave = $atm s \n";
oci_close($con);   

時刻は microtime() を用いることで小数点以下の時間も取得した。

実行結果

result.txt
0.10905408859253 s
0.13218903541565 s
0.076097011566162 s
0.12702822685242 s
0.14478015899658 s
0.11919498443604 s
0.10693597793579 s
--省略--
0.13985991477966 s
0.11988806724548 s
0.069957971572876 s
0.15980696678162 s
0.069906949996948 s
0.11997413635254 s
Sum = 114.36270356178 s 
Ave = 0.11436270356178 s 

1000回出力した結果、データベースの書き換えの処理速度は1つのデータ当たり、約0.1秒程度だと分かる。

以上


Level7 index Level9