• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php版权重轮询调度算法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

2013-09-25

<?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();
    }
}

测试结果:

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
wordpress教程之在非single.php页面调用文章的信息发布时间:2022-07-10
下一篇:
php变量类型分类发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap