標籤:
http://www.cnblogs.com/lh460795/archive/2013/04/06/3003105.html
在apache / bin/ab.exe 可以做壓力測試,該工具可以類比多人,並發訪問某個頁面.
基本的用法
ab.exe –n 10000 –c 10
-n 表示請求多少次
-c 表示多少人
如果要測試php自己的緩衝機制, 需要做配置.
php.ini 檔案
display_errors=On
output_buffering=Off
error_reproting= 設定錯誤層級
看一段代碼,使用緩衝時,在傳送檔案頭之前可以顯示文字.
<?php
echo“yyy”;
header(“content-type:text/htm;charset=utf-8”);
echo“hello”;
?>
PHP緩衝控制的幾個函數:
1 //PHP緩衝控制的幾個函數: 2 //開啟緩衝 [通過php.ini,也可以在頁面 ob_start()] 3 ob_start(); 4 echo "yyy"; 5 header("content-type:text/htm;charset=utf-8"); 6 echo "hello"; 7 //ob_clean函數可以清空 outputbuffer的內容. 8 //ob_clean(); 9 //ob_end_clean是關閉ob緩衝,同時清空.10 //ob_clean();11 //ob_end_flush() 函數是 把ob緩衝的記憶體輸出,並關閉ob12 //ob_end_flush();13 //ob_end_flush() 函數是 把ob緩衝的記憶體輸出,14 //ob_flush()函數是輸出ob內容,並清空,但不關閉.15 ob_flush();16 17 echo "kkk";//=>ob緩衝.18 19 //header("content-type:text/htm;charset=utf-8");20 21 //ob_get_contents() 可以擷取output_buffering的內容.22 //$contents=ob_get_contents();23 24 //file_put_contents("d:/log.text",$contents);
下面來看一個執行個體,用緩衝技術,"假如儲存的快取檔案未超過30秒,則直接取出快取檔案":
1 <?php 2 $id=$_GET[‘id‘]; 3 $filename="static_id_".$id.".html"; 4 $status=filemtime($filename)+30>time();//判斷檔案建立及修改時間距目前時間是否超過30秒 5 if(file_exists($filename)&&$status){ 6 $str=file_get_contents($filename); 7 echo $str; 8 }else{ 9 require_once "SqlHelper.class.php";10 $sqlHelper=new Sqlhelper();11 $arr=$sqlHelper->execute_dql2("SELECT * FROM news1 WHERE id=$id");12 if(empty($arr)){13 echo "資料為空白";14 }else{15 /***緩衝開始***/16 ob_start();//下面的內容將存到緩衝區中,顯示的內容都將存到緩衝區17 echo $arr[0][‘tile‘];18 echo "<br/>";19 echo $arr[0][‘content‘];20 $content= ob_get_contents();//從緩衝中擷取內容21 ob_end_clean();//關閉緩衝並清空22 /***緩衝結束***/23 file_put_contents($filename, $content);24 echo $content;25 }26 }27 28 29 ?>
PHP使用緩衝產生靜態頁面