给出一个 Gearman 的 PHP 编程实例

学习笔记 马富天 2017-11-22 09:50:56 82 0

【摘要】本文给出一个在 gearman 分布式集群下 PHP 的一个编程实例,供后续学习使用,并给出一个对比实验。

实验是计算累加和,在分布式和单服务器下做同一计算量的程序,然后给出实验结果。

在单服务器下,1.php 文件中:

  1. $start = get_microtime();
  2. for($i = 1;$i <= 30;$i ++)
  3. {
  4. 	echo $i.'-1-'.title_function();
  5. 	echo '<br />';
  6. }
  7. function title_function()
  8. {
  9. 	$sum = 0;
  10. 	for($i = 1;$i <= 10000;$i++)
  11. 	{
  12. 	    for($j = 1;$j <= 100;$j++)
  13. 	    {
  14. 			$sum ++;
  15. 	    }       
  16. 	}
  17. 	return $sum;
  18. }
  19. $second = get_microtime() - $start;
  20. echo $second;
  21. function get_microtime()
  22. {
  23. 	list($usec,$sec) = explode(" ",microtime());
  24. 	return ((float)$sec + (float)$usec);
  25. }

在 gearman 环境下:client.php 代码:

  1. $start = get_microtime();
  2. $client= new GearmanClient();
  3. $client->addServer("172.18.130.11", 4730);
  4. $client->addServer("172.18.130.12", 4730);
  5. $client->addServer("172.18.130.13", 4730);
  6. for($i=1;$i<=30;$i++)
  7. {
  8.     print $client->do("title",$i);
  9.     echo "\n";
  10. }
  11. $second = get_microtime() - $start;
  12. echo $second."\n";
  13. function get_microtime()
  14. {
  15. 	list($usec,$sec) = explode(" ",microtime());
  16. 	return ((float)$sec + (float)$usec);
  17. }

worker.php 代码:

请输入图片名称请输入图片名称

在单服务器下执行的时间是 9.07 秒,而在三台服务器分布式下执行时间是:7.51 秒,时间上确实是减少了,但减少的量并不多,可能是因为三台服务器它们的内存都是 512M,而单台服务器暂用的内存是 4G,这个影响可能比较大。

版权归 马富天个人博客 所有

本文标题:《给出一个 Gearman 的 PHP 编程实例》

本文链接地址:http://www.mafutian.com/349.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

0

0

上一篇《 CentOS 6.3 报错:Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName 的解决办法 》 下一篇《 在 CentOS 系统中遇到:E325: ATTENTION Found a swap file by the name 的问题的解决办法 》

暂无评论

评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多