php版权重轮询调度算法

2013-09-25网址:yii666.com

<?php
class WeightedRoundRobin
{
private static $_weightArray = array(); private static $_i = -1;//代表上一次选择的服务器
private static $_gcd;//表示集合S中所有服务器权值的最大公约数
private static $_cw = 0;//当前调度的权值
private static $_max;
private static $_n;//agent个数 public function __construct(array $weightArray)
{
self::$_weightArray = $weightArray;
self::$_gcd = self::getGcd(self::$_weightArray);
self::$_max = self::getMaxWeight(self::$_weightArray);
self::$_n = count($weightArray);
} private static function getGcd(array $weightArray)
{
$temp = array_shift($weightArray);
$min = $temp['weight'];
$status = false;
foreach ($weightArray as $val) {
$min = min($val['weight'],$min);
} if($min == 1){
return 1;
}else{
      			for ($i = $min; $i>1; $i--) {

      				foreach ($weightArray as $val) {
if (is_int($val['weight']/$i)) {
$status = true;
}else{
$status = false;
break;
}
}
if ($status) {
return $i;
}else {
return 1;
} }
} } private static function getMaxWeight(array $weightArray)
{
if(empty($weightArray)){
return false;
}
$temp = array_shift($weightArray);
$max = $temp['weight'];
foreach ($weightArray as $val) {
$max = max($val['weight'],$max);
}
return $max;
} public function getWeight()
{ while (true){ self::$_i = ((int)self::$_i+1) % (int)self::$_n; if (self::$_i == 0) { self::$_cw = (int)self::$_cw - (int)self::$_gcd;
if (self::$_cw <= 0) {
self::$_cw = (int)self::$_max; if (self::$_cw == 0) {
return null;
}
}
} if ((int)(self::$_weightArray[self::$_i]['weight']) >= self::$_cw) {
return self::$_weightArray[self::$_i];
}
}
} }
?>

  客户端代码:在thinkphp中实现

<?php
class IndexAction extends Action {
public function index(){ import("COM.WeightedRoundRobin"); $arr = array(
array('id' => 'A', 'weight' => 3),
array('id' => 'B', 'weight' => 3),
array('id' => 'C', 'weight' => 6),
array('id' => 'D', 'weight' => 4),
array('id' => 'E', 'weight' => 1), );
$weight = new WeightedRoundRobin($arr); $a=0;
$b=0;
$c=0;
$d=0;
$e=0;
for ($j = 0; $j < 100; $j++) {
$weightInfo = $weight->getWeight();
echo $weightInfo['id'].'----------------------weight:'.$weightInfo['weight'].'<br/>';
if($weightInfo['id'] == 'A'){
$a++;
}
if($weightInfo['id'] == 'B'){
$b++;
}
if($weightInfo['id'] == 'C'){
$c++;
}
if($weightInfo['id'] == 'D'){
$d++;
}
if($weightInfo['id'] == 'E'){
$e++;
}
}
echo 'A:'.$a.'<br/>';
echo 'B:'.$b.'<br/>';
echo 'C:'.$c.'<br/>';
echo 'D:'.$d.'<br/>';
echo 'E:'.$e.'<br/>';
exit;
$this->display();
}
}

测试结果:文章来源地址https://www.yii666.com/article/754052.html文章地址https://www.yii666.com/article/754052.html

php版权重轮询调度算法网址:yii666.com<文章来源地址:https://www.yii666.com/article/754052.html

php版权重轮询调度算法

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

php版权重轮询调度算法-相关文章

  1. 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现

  2. 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现3

  3. 权重轮询调度算法 java版本号

  4. 权重轮询调度算法(Weighted Round-Robin Scheduling)-C#实现

  5. golang实现权重轮询调度算法

  6. php版权重轮询调度算法

  7. loadbalance轮询算法 java实现

  8. 为什么JAVA要提供 wait/notify 机制?是为了避免轮询带来的性能损失

    wait/notify  机制是为了避免轮询带来的性能损失。为了说清道理,我们用“图书馆借书”这个经典例子来作解释。一本书同时只能借给一个人。现在有一本书,图书馆已经把这本书借了张三。在简单的synchrnozed 同步机制下,李四如果想借,先要去图书馆查看书有没有还回来。李

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png