Discuz教程网

mysql下mysql-udf-http效率测试小记

[复制链接]
authicon dly 发表于 2011-9-11 15:01:24 | 显示全部楼层 |阅读模式
看到张宴的博客上关于"http/rest客户端的文章",怎样安装啥的直接都跳过,下面直接进入测试阶段,测试环境:虚拟机
  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
复制代码

内存和交换分区:
  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. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  13. mysql>
复制代码

使用的表结构:
  1. DROP TABLE IF EXISTS `mytable`;
  2. CREATE TABLE `mytable` (
  3. `id` int(10) NOT NULL AUTO_INCREMENT,
  4. `addtime` int(10) NOT NULL,
  5. `title` varchar(255) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
复制代码

php操作MySQL的程序:
  1. <?php
  2. $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) or die("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. } else if ("delete" == $type) { //删除
  27. $i = 1;
  28. while ($i <= $loop_cnt) {
  29. $sql = "delete from mytable where id='".$i."'";
  30. echo "delete sql: ".$sql."<br>";
  31. $res = mysql_query($sql);
  32. if (FALSE == $res) {
  33. echo "delete failed!\n";
  34. }
  35. $i++;
  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."<br>";
  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<br>";
  55. ?>
复制代码

单独执行php连接MySQL,单条连接添加1000条记录需要:0.9s左右
php操作memcache的程序:
  1. <?php
  2. 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 = new Memcache;
  12. $mem->connect($mem_host, $mem_port) or die("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. $etime = getmicrotime();
  22. $runTime = round($etime - $btime, 4);
  23. echo "runTime: ".$runTime."\r\n<br>";
  24. ?>
复制代码

单条连接添加1000条记录,需要0.8s左右,
创建触发器:
  1. DELIMITER $$
  2. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$
  3. CREATE
  4. /*!50017 DEFINER = 'root'@'localhost' */
  5. TRIGGER `mytable_insert` AFTER INSERT ON `mytable`
  6. FOR EACH ROW BEGIN
  7. SET @tt_resu = (SELECT http_put(CONCAT('http://192.168.0.134/mem_ss.php?type=post&id=', NEW.id, "&data=", NEW.addtime), 11));
  8. END;
  9. $$
复制代码

为触发器写个php更新memcache,代码如下:
  1. <?php
  2. $id = $_GET['id'];
  3. $type = $_GET['type'];
  4. $json_data = $_GET['data'];
  5. var_dump($_GET);
  6. //杩炴帴
  7. $mem_host = "192.168.0.134";
  8. $mem_port = "11211";
  9. $timeout = 3600;
  10. $mem = new Memcache;
  11. $mem->connect($mem_host, $mem_port) or die("Could not connect!");
  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. $mem->close();
  34. ?>
复制代码

使用php触发MySQL添加1000条记录,同时触发器触动php更新memcache,使用时间9s左右,
因为每次都关闭链接memcache,看是不是关闭链接导致慢,又写了一个程序:
  1. <?php
  2. 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 = new Memcache;
  12. $mem->connect($mem_host, $mem_port) or die("Could not connect!");
  13. $ret = $mem->set($i, "11111111111", 0, 3600);
  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. $etime = getmicrotime();
  22. $runTime = round($etime - $btime, 4);
  23. echo "runTime: ".$runTime."\r\n<br>";
  24. ?>
复制代码

耗时0.9s左右,比一个连接慢不了多少。
为了定位是触发器慢还是http_put慢,创建一个临时表
tmp_mytable,表结构如下:
  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;
复制代码

再次修改触发器,如下:
  1. DELIMITER $$
  2. DROP TRIGGER /*!50032 IF EXISTS */ `test`.`mytable_insert`$$
  3. CREATE
  4. /*!50017 DEFINER = 'root'@'localhost' */
  5. TRIGGER `mytable_insert` AFTER INSERT ON `mytable`
  6. FOR EACH ROW BEGIN
  7. insert into tmp_mytable values(NEW.id,NEW.addtime,NEW.title);
  8. END;
  9. $$
复制代码

再次用php向MySQL中添加1000条记录,消耗时间0.7s左右,证明效率消耗在http_put,也就是mysql-udf-http慢。
不知道我的测试有错没?还请正在使用mysql-udf-http的高手,或者对mysql-udf-http有研究的高手指教。



上一篇:mysql odbc字符集设置(中文显示乱码)
下一篇:MySQL数据库varchar的限制规则说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 16:40

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表