日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
小記MySQL的mysql-udf-http效率測試

看到張宴的博客上關(guān)于"http/rest客戶端的文章",怎樣安裝啥的直接都跳過,下面直接進入測試階段,測試環(huán)境:虛擬機

 
 
 
  1. [root@localhost ~]# uname -a  
  2. Linux sunss 2.6.18-128.el5 #1 SMP Wed Jan 21 10:44:23 EST 2009 i686 i686 i386 GNU/Linux  

內(nèi)存和交換分區(qū):

 
 
 
  1. [root@localhost ~]# free -m  
  2.              total       used       free     shared    buffers     cached  
  3. Mem:           376        363         13          0         23        105  
  4. -/+ buffers/cache:        233        142  
  5. Swap:         1023        133        890  
  6. mysql:  
  7. [root@localhost ~]# mysql -u root -p  
  8. Enter password:   
  9. Welcome to the MySQL monitor. Commands end with ; or \g.  
  10. Your MySQL connection id is 57  
  11. Server version: 5.1.26-rc-log Source distribution  
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  14. mysql>  

使用的表結(jié)構(gòu):

 
 
 
  1. DROP TABLE IF EXISTS `mytable`;  
  2.  
  3. CREATE TABLE `mytable` (  
  4.  `id` int(10) NOT NULL AUTO_INCREMENT,  
  5.  `addtime` int(10) NOT NULL,  
  6.  `title` varchar(255) NOT NULL,  
  7.  PRIMARY KEY (`id`)  
  8. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

php操作
MySQL的程序:

 
 
 
  1.     $type = $_GET['type'];  
  2.     print_r($_GET);  
  3.     include_once("gettime.php");  
  4.     $btime = getmicrotime();  
  5.     $loop_cnt= 1000; //循環(huán)次數(shù)  
  6.     $db_host = '127.0.0.1'; //  
  7.     $db_user = 'sunss'; //  
  8.     $db_pass = '123456'; //  
  9.     $db_name = 'test'; //  
  10.     $db_link = mysql_connect($db_host, $db_user, $db_pass) or die("Connected failed: ".mysql_error()."\n");  
  11.     mysql_query('set names utf8');  
  12.     mysql_db_query($db_name, $db_link);  
  13.     if ("put" == $type) {//修改  
  14.     $i = 1;  
  15.     while ($i <= $loop_cnt) {  
  16.         $title = "jkjkjkjkjkjkjkjkjkjkjkjkjk";  
  17.         $tt    = time();  
  18.         $sql = "update mytable set addtime=".$tt.",title='".$title."' where id='".$i."'";  
  19.         $res = mysql_query($sql);  
  20.         if (FALSE == $res) {  
  21.             echo "update failed!\n";  
  22.         }  
  23.         $i++;  
  24.     }  
  25.     } else if ("delete" == $type) { //刪除  
  26.     $i = 1;  
  27.     while ($i <= $loop_cnt) {  
  28.         $sql = "delete from mytable where id='".$i."'";  
  29.         echo "delete sql: ".$sql."
    ";  
  30.         $res = mysql_query($sql);  
  31.         if (FALSE == $res) {  
  32.         echo "delete failed!\n";  
  33.         }  
  34.         $i++;  
  35.     }  
  36.       
  37.     } else if ("post" == $type) { //添加  
  38.     $i = 0;  
  39.     while ($i < $loop_cnt) {  
  40.         $title = "hahahahahahahahahahahahahahahahahaha";  
  41.         $tt    = time();  
  42.         $sql = "insert into mytable(addtime, title) values($tt, '".$title."')";  
  43.         //print "SQL: ".$sql."
    ";  
  44.         $res = mysql_query($sql);  
  45.         if (FALSE == $res) {  
  46.         echo "insert failed!\n";  
  47.         }  
  48.         $i++;  
  49.     }  
  50.     }  
  51.     mysql_close();  
  52.     $etime = getmicrotime();  
  53.     $runTime = round($etime - $btime, 4);  
  54.     echo "runTime: ".$runTime."\r\n
    ";  
  55. ?>  

單獨執(zhí)行php連接MySQL,單條連接添加1000條記錄需要:0.9s左右php操作memcache的程序:

 
 
 
  1.     include_once("gettime.php");  
  2.     $btime = getmicrotime();  
  3.     //   
  4.     $mem_host = "192.168.0.134";  
  5.     $mem_port = "11311";  
  6.     $timeout = 3600;  
  7.     $i = 0;  
  8.     $cnt = 1000;  
  9.     while ($i < $cnt) {  
  10.     $mem = new Memcache;  
  11.     $mem->connect($mem_host, $mem_port) or die("Could not connect!");  
  12.     $ret = $mem->set($i, "11111111111", 0, $timeout);  
  13.     if (false == $ret) {  
  14.         file_put_contents("insert_failed.log", "post failed!\n", FILE_APPEND);  
  15.     }  
  16.     $mem->close();  
  17.     $i++;  
  18.     }  
  19.  
  20.     //   
  21.     $etime = getmicrotime();  
  22.     $runTime = round($etime - $btime, 4);  
  23.     echo "runTime: ".$runTime."\r\n
    ";  
  24. ?>  

單條連接添加1000條記錄,需要0.8s左右,創(chuàng)建觸發(fā)器:

 
 
 
  1. DELIMITER $$  
  2.  
  3. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$  
  4.  
  5. CREATE 
  6.     /*!50017 DEFINER = 'root'@'localhost' */  
  7.     TRIGGER `mytable_insert` AFTER INSERT ON `mytable`   
  8.     FOR EACH ROW BEGIN   
  9.     SET @tt_resu = (SELECT http_put(CONCAT('http://192.168.0.134/mem_ss.php?type=post&id=', NEW.id, "&data=", NEW.addtime), 11));  
  10. END;  
  11. $$  

為觸發(fā)器寫個php更新memcache,代碼如下:

 
 
 
  1.     $id        = $_GET['id'];  
  2.     $type      = $_GET['type'];  
  3.     $json_data = $_GET['data'];  
  4.     var_dump($_GET);  
  5.     //  
  6.     $mem_host = "192.168.0.134";  
  7.     $mem_port = "11211";  
  8.     $timeout = 3600;  
  9.     $mem = new Memcache;  
  10.     $mem->connect($mem_host, $mem_port) or die("Could not connect!");  
  11.       
  12.     if ("get" == $type ) {  
  13.     $val = $mem->get($id);  
  14.     echo $val;  
  15.     //$arr = jsonDecode($val,'utf-8');  
  16.     //print_r($arr);  
  17.     } else if ("put" == $type) {  
  18.     $ret = $mem->replace($id, $json_data, 0, $timeout);  
  19.     if (false == $ret) {  
  20.         file_put_contents("replace_failed.log", "replace failed!\n", FILE_APPEND);  
  21.     }   
  22.     } else if ("delete" == $type) {   
  23.     $ret = $mem->delete($id);  
  24.     if (false == $ret) {  
  25.         file_put_contents("delete_failed.log", "delete failed!\n", FILE_APPEND);  
  26.     }  
  27.     } else if ("post" == $type) {  
  28.     $ret = $mem->set($id, $json_data, 0, $timeout);  
  29.     if (false == $ret) {  
  30.         file_put_contents("post_failed.log", "post failed!\n", FILE_APPEND);  
  31.     }  
  32.     }  
  33.  
  34.     $mem->close();  
  35. ?>  

使用php觸發(fā)MySQL添加1000條記錄,同時觸發(fā)器觸動php更新memcache,使用時間9s左右,因為每次都關(guān)閉鏈接memcache,看是不是關(guān)閉鏈接導(dǎo)致慢,又寫了一個程序:

 
 
 
  1.     include_once("gettime.php");  
  2.     $btime = getmicrotime();  
  3.     //連接  
  4.     $mem_host = "192.168.0.134";  
  5.     $mem_port = "11311";  
  6.     $timeout = 3600;  
  7.     $i = 0;  
  8.     $cnt = 1000;  
  9.     while ($i < $cnt) {  
  10.     $mem = new Memcache;  
  11.     $mem->connect($mem_host, $mem_port) or die("Could not connect!");  
  12.     $ret = $mem->set($i, "11111111111", 0, 3600);  
  13.     if (false == $ret) {  
  14.         file_put_contents("insert_failed.log", "post failed!\n", FILE_APPEND);  
  15.     }  
  16.     $mem->close();  
  17.     $i++;  
  18.     }  
  19.  
  20.     //關(guān)閉連接  
  21.     $etime = getmicrotime();  
  22.     $runTime = round($etime - $btime, 4);  
  23.     echo "runTime: ".$runTime."\r\n
    ";  
  24. ?>  

耗時0.9s左右,比一個連接慢不了多少。為了定位是觸發(fā)器慢還是http_put慢,創(chuàng)建一個臨時表tmp_mytable,表結(jié)構(gòu)如下:

 
 
 
  1. CREATE TABLE `mytable` (  
  2.  `id` int(10) NOT NULL AUTO_INCREMENT,  
  3.  `addtime` int(10) NOT NULL,  
  4.  `title` varchar(255) NOT NULL 
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

再次修改觸發(fā)器,如下:

 
 
 
  1. DELIMITER $$  
  2.  
  3. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$  
  4.  
  5. CREATE 
  6.     /*!50017 DEFINER = 'root'@'localhost' */  
  7.     TRIGGER `mytable_insert` AFTER INSERT ON `mytable`   
  8.     FOR EACH ROW BEGIN   
  9.      insert into tmp_mytable values(NEW.id,NEW.addtime,NEW.title);     
  10. END;  
  11. $$  

再次用php向MySQL中添加1000條記錄,消耗時間0.7s左右,證明效率消耗在http_put,也就是mysql-udf-http慢。不知道我的測試有錯沒?還請正在使用mysql-udf-http的高手,或者對mysql-udf-http有研究的高手指教。

原文鏈接:http://www.cnblogs.com/sunss/archive/2011/05/09/2041283.html


網(wǎng)站標題:小記MySQL的mysql-udf-http效率測試
分享網(wǎng)址:http://www.5511xx.com/article/dhhhiji.html