博主辛苦了,我要打赏银两给博主,犒劳犒劳站长。
【摘要】本文给出一个在 gearman 分布式集群下 PHP 的一个编程实例,供后续学习使用,并给出一个对比实验。
实验是计算累加和,在分布式和单服务器下做同一计算量的程序,然后给出实验结果。
在单服务器下,1.php 文件中:
$start = get_microtime();
for($i = 1;$i <= 30;$i ++)
{
echo $i.'-1-'.title_function();
echo '<br />';
}
function title_function()
{
$sum = 0;
for($i = 1;$i <= 10000;$i++)
{
for($j = 1;$j <= 100;$j++)
{
$sum ++;
}
}
return $sum;
}
$second = get_microtime() - $start;
echo $second;
function get_microtime()
{
list($usec,$sec) = explode(" ",microtime());
return ((float)$sec + (float)$usec);
}
在 gearman 环境下:client.php 代码:
$start = get_microtime();
$client= new GearmanClient();
$client->addServer("172.18.130.11", 4730);
$client->addServer("172.18.130.12", 4730);
$client->addServer("172.18.130.13", 4730);
for($i=1;$i<=30;$i++)
{
print $client->do("title",$i);
echo "\n";
}
$second = get_microtime() - $start;
echo $second."\n";
function get_microtime()
{
list($usec,$sec) = explode(" ",microtime());
return ((float)$sec + (float)$usec);
}
worker.php 代码:
在单服务器下执行的时间是 9.07 秒,而在三台服务器分布式下执行时间是:7.51 秒,时间上确实是减少了,但减少的量并不多,可能是因为三台服务器它们的内存都是 512M,而单台服务器暂用的内存是 4G,这个影响可能比较大。
版权归 马富天个人博客 所有
本文标题:《给出一个 Gearman 的 PHP 编程实例》
本文链接地址:http://www.mafutian.com/349.html
转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^
顶0
踩0
评论审核未开启 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
||