mysql udf http_mysql的mysql-udf-http效率测试小记

看到张宴的博客上关于"http/rest客户端的文章",怎样安装啥的直接都跳过,下面直接进入测试阶段,测试环境:虚拟机

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

内存和交换分区:

[root@localhost ~]#free -mtotal used free shared buffers cached

Mem:37636313023105-/+buffers/cache:233142Swap:1023133890

mysql:

[root@localhost ~]#mysql -u root -pEnter password:

Welcome to the MySQL monitor. Commandsendwith ; or \g.

Your MySQL connection id is57Server version:5.1.26-rc-log Source distribution

Type 'help;' or '\h'forhelp. Type '\c' to clear the buffer.

mysql>

使用的表结构:

DROPTABLEIFEXISTS`mytable`;CREATETABLE`mytable` (

`id`int(10)NOTNULLAUTO_INCREMENT,

`addtime`int(10)NOTNULL,

`title`varchar(255)NOTNULL,PRIMARYKEY(`id`)

) ENGINE=MyISAMDEFAULTCHARSET=utf8;

php操作MySQL的程序:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

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

单独执行php连接MySQL,单条连接添加1000条记录需要:0.9s左右

php操作memcache的程序:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

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

单条连接添加1000条记录,需要0.8s左右,

创建触发器:

DELIMITER $$DROPTRIGGER/*!50032 IF EXISTS*/`test`.`mytable_insert`$$CREATE/*!50017 DEFINER = 'root'@'localhost'*/TRIGGER`mytable_insert` AFTERINSERTON`mytable`FOREACH ROWBEGINSET@tt_resu=(SELECThttp_put(CONCAT('http://192.168.0.134/mem_ss.php?type=post&id=', NEW.id, "&data=", NEW.addtime),11));END;

$$

为触发器写个php更新memcache,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

<?php $id=$_GET['id'];$type=$_GET['type'];$json_data=$_GET['data'];var_dump($_GET);//杩炴帴$mem_host="192.168.0.134";$mem_port="11211";$timeout=3600;$mem=newMemcache;$mem->connect($mem_host,$mem_port) ordie("Could not connect!");if("get"==$type) {$val=$mem->get($id);echo$val;//$arr = jsonDecode($val,'utf-8');

//print_r($arr);}elseif("put"==$type) {$ret=$mem->replace($id,$json_data,0,$timeout);if(false==$ret) {file_put_contents("replace_failed.log","replace failed!\n",FILE_APPEND);

}

}elseif("delete"==$type) {$ret=$mem->delete($id);if(false==$ret) {file_put_contents("delete_failed.log","delete failed!\n",FILE_APPEND);

}

}elseif("post"==$type) {$ret=$mem->set($id,$json_data,0,$timeout);if(false==$ret) {file_put_contents("post_failed.log","post failed!\n",FILE_APPEND);

}

}$mem->close();?>

使用php触发MySQL添加1000条记录,同时触发器触动php更新memcache,使用时间9s左右,

因为每次都关闭链接memcache,看是不是关闭链接导致慢,又写了一个程序:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

<?phpinclude_once ("gettime.php");$btime=getmicrotime();//连接$mem_host="192.168.0.134";$mem_port="11311";$timeout=3600;$i=0;$cnt=1000;while($i<$cnt) {$mem=newMemcache;$mem->connect($mem_host,$mem_port) ordie("Could not connect!");$ret=$mem->set($i,"11111111111",0,3600);if(false==$ret) {file_put_contents("insert_failed.log","post failed!\n",FILE_APPEND);

}$mem->close();$i++;

}//关闭连接$etime=getmicrotime();$runTime=round($etime-$btime,4);echo"runTime:".$runTime."\r\n
";?>

耗时0.9s左右,比一个连接慢不了多少。

为了定位是触发器慢还是http_put慢,创建一个临时表

tmp_mytable,表结构如下:

CREATETABLE`mytable` (

`id`int(10)NOTNULLAUTO_INCREMENT,

`addtime`int(10)NOTNULL,

`title`varchar(255)NOTNULL) ENGINE=MyISAMDEFAULTCHARSET=utf8;

再次修改触发器,如下:

DELIMITER $$DROPTRIGGER/*!50032 IF EXISTS*/`test`.`mytable_insert`$$CREATE/*!50017 DEFINER = 'root'@'localhost'*/TRIGGER`mytable_insert` AFTERINSERTON`mytable`FOREACH ROWBEGINinsertintotmp_mytablevalues(NEW.id,NEW.addtime,NEW.title);END;

$$

再次用php向MySQL中添加1000条记录,消耗时间0.7s左右,证明效率消耗在http_put,也就是mysql-udf-http慢。

不知道我的测试有错没?还请正在使用mysql-udf-http的高手,或者对mysql-udf-http有研究的高手指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值