Esempio n. 1
0
        static void Test()
        {
            List <Server> servers = new List <Server>();

            for (int i = 0; i < 1000; i++)
            {
                servers.Add(new Server("192.168.0." + i));
            }

            ConsistentHash <Server> ch = new ConsistentHash <Server>();

            ch.Init(servers);

            int search = 100000;

            DateTime start = DateTime.Now;
            SortedList <int, string> ay1 = new SortedList <int, string>();

            for (int i = 0; i < search; i++)
            {
                string temp = ch.GetNode(i.ToString()).IP;

                ay1[i] = temp;
            }
            TimeSpan ts = DateTime.Now - start;

            Console.WriteLine(search + " each use seconds: " + (double)(ts.TotalSeconds / search));

            //ch.Add(new Server(1000));
            ch.Remove(servers[1]);
            SortedList <int, string> ay2 = new SortedList <int, string>();

            for (int i = 0; i < search; i++)
            {
                string temp = ch.GetNode(i.ToString()).IP;

                ay2[i] = temp;
            }

            int diff = 0;

            for (int i = 0; i < search; i++)
            {
                if (ay1[i] != ay2[i])
                {
                    diff++;
                }
            }

            Console.WriteLine("diff: " + diff);
        }
Esempio n. 2
0
        static void TestHashKeyMethod()
        {
            // 假设我们有三个组,可以来存放人。也可以理解成,我们要把我们的学生分成3个组,均匀分布。
            List <StoreList> storeLists = new List <StoreList>();

            storeLists.Add(new StoreList(1)); //第一组
            storeLists.Add(new StoreList(2)); //第二组
            storeLists.Add(new StoreList(3)); //第三组

            //假设我们有10万学生
            int         num   = 10;
            List <User> users = new List <User>();

            for (int i = 0; i < 10000 * num; i++)
            {
                users.Add(new User {
                    UserId = i + 1, UserName = "******" + (i + 1)
                });
            }

            //将组按hash计算的形式分布到环上
            ConsistentHash <StoreList> ch = new ConsistentHash <StoreList>();

            ch.Init(storeLists);

            //每个学生找到自己应该放的组
            foreach (var user in users)
            {
                //学生id是唯一的,我们那就将学生id作为key来用
                StoreList storeList = ch.GetNode(user.UserId.ToString());
                storeList.Welcome(user);
                Console.WriteLine($"{user.UserName} 在 {storeList.Id} 组");
            }

            //看下每组人数(这是分布是否均匀的检测指标)
            foreach (var item in storeLists)
            {
                Console.WriteLine($"{item.Id} 组 {item.list.Count} 人");
            }

            //查找学生id是6000的学生
            var startTime = DateTime.Now;
            var userid    = 6000;
            var node      = ch.GetNode(userid.ToString());
            var student   = node.list.FirstOrDefault(m => m.UserId == userid);
            var elpased   = (DateTime.Now - startTime).TotalMilliseconds;

            Console.WriteLine("用时:" + elpased + " " + student.UserName);
        }