Esempio n. 1
0
		void InitNodes()
		{
			nodes.Clear();
			var document = new XPathDocument(file);
			XPathNavigator navigator = document.CreateNavigator();
			XPathNodeIterator it = navigator.Select("Rpc/Providers/Provider");
			
			foreach (XPathNavigator navi in it) {
                string nodeName = navi.GetAttribute("Name", "");
				Boolean available = Boolean.Parse(navi.GetAttribute("Available", ""));
				Int32 weight = Int32.Parse(navi.GetAttribute("Weight", ""));
                string ip = navi.GetAttribute("Ip", "");
				String url = navi.Value;

                var node = new Node(nodeName, url, weight, available, ip);
				
				nodes.Add(node);
			}
		}
		public void RandomBalanceSelectTest()
		{
			var node1 = new Node("a","http://www.baidu.com?node=1&weight=5", 100, true,"192.168.1.10");
			var node2 = new Node("a","http://www.baidu.com?node=2&weight=10", 500, true, "192.168.1.10");
            var node3 = new Node("a", "http://www.baidu.com?node=3&weight=20", 1000, true, "192.168.1.10");
			
			var list = new List<Node>();
			list.Add(node1);
			list.Add(node2);
			list.Add(node3);
			
			int node1Count = 0;
			int node2Count = 0;
			int node3Count = 0;
			
			var randomBalance = new RandomLoadBalance();
			
			for (var i = 0; i < 100000; i++) {
				Node node = randomBalance.Select(list, string.Empty);
				
				if (node == node3) {
					node3Count++;
				}
				
				if (node == node2) {
					node2Count++;
				}
				
				if (node == node1) {
					node1Count++;
				}
			}
			
			int totalCount = node1Count + node2Count + node3Count;

            var result1 = (node1Count * 1.0 / totalCount);
            var result2 = (100 * 1.0 / 1600);

            Assert.IsTrue((result1 - result2) < 0.01, result1 + "," + result2);
		}
        public void RoundRobinLoadBalanceSelectTest()
		{
			var node1 = new Node("a","http://www.baidu.com?node=1&weight=100", 5, true,"192.168.1.1");
			var node2 = new Node("a","http://www.baidu.com?node=2&weight=100", 10, true, "192.168.1.1");
			var node3 = new Node("a","http://www.baidu.com?node=3&weight=200", 5, true, "192.168.1.1");
			var node4 = new Node("a","http://www.baidu.com?node=4&weight=200", 30, true, "192.168.1.1");
			var node5 = new Node("a","http://www.baidu.com?node=5&weight=200", 100, true, "192.168.1.1");
			
			var list = new List<Node>();
			list.Add(node1);
			list.Add(node2);
			list.Add(node3);
			list.Add(node4);
			list.Add(node5);
			
			var roundRobinBalance = new RoundRobinLoadBalance();
			
			ConcurrentDictionary<String,AtomicPositiveInteger> conDic = new ConcurrentDictionary<String,AtomicPositiveInteger>();
			
			conDic.TryAdd(node1.Url, new AtomicPositiveInteger());
			conDic.TryAdd(node2.Url, new AtomicPositiveInteger());
			conDic.TryAdd(node3.Url, new AtomicPositiveInteger());
			conDic.TryAdd(node4.Url, new AtomicPositiveInteger());
			conDic.TryAdd(node5.Url, new AtomicPositiveInteger());
			
			for (var i = 0; i < 100000; i++) 
			{
				Node node = roundRobinBalance.Select(list, "test1");
				conDic[node.Url].GetAndIncrement();
			}

            int totalCount = conDic[node1.Url].Value() + conDic[node2.Url].Value() + conDic[node3.Url].Value() + conDic[node4.Url].Value() + conDic[node4.Url].Value();

            var result1 = (conDic[node4.Url].Value() * 1.0 / totalCount);
            var result2 = (100 * 1.0 / 150);

            Assert.IsTrue((result1 - result2) < 0.01, result1 + "," + result2);
		}