public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                ISubscribeInfo subscribeInfo1 = env.Nodes[0].Subscribe ();
                ISubscribeInfo subscribeInfo2 = env.Nodes[1].Subscribe ();

                while (true) {
                    if (subscribeInfo1.Status == SubscribeRouteStatus.Stable && subscribeInfo2.Status == SubscribeRouteStatus.Stable)
                        break;
                    Thread.Sleep (10);
                }

                bool routeEstablished = false;
                IAnonymousSocket sock1 = null, sock2 = null;
                StreamSocket strm1 = null, strm2 = null;
                do {
                    int datasize = 1000 * 1000;
                    IntervalInterrupter timeoutChecker = new IntervalInterrupter (TimeSpan.FromMilliseconds (100), "StreamSocket TimeoutChecker");
                    timeoutChecker.Start ();
                    try {
                        IAsyncResult ar = env.Nodes[0].AnonymousRouter.BeginConnect (subscribeInfo1.Key, subscribeInfo2.Key, AnonymousConnectionType.HighThroughput, null, null, null);
                        sock1 = env.Nodes[0].AnonymousRouter.EndConnect (ar);
                        if (env.Nodes[1].AnonymousSocketInfoList.Count == 0)
                            throw new System.Net.Sockets.SocketException ();
                        routeEstablished = true;
                        sock2 = env.Nodes[1].AnonymousSocketInfoList[0].BaseSocket;
                        strm1 = new StreamSocket (sock1, AnonymousRouter.DummyEndPoint, 500, timeoutChecker);
                        strm2 = new StreamSocket (sock2, AnonymousRouter.DummyEndPoint, 500, timeoutChecker);
                        sock1.InitializedEventHandlers ();
                        sock2.InitializedEventHandlers ();
                        Stopwatch sw = Stopwatch.StartNew ();
                        byte[] data = new byte[datasize];
                        strm1.Send (data, 0, data.Length);
                        strm1.Shutdown ();
                        strm2.Shutdown ();
                        Logger.Log (LogLevel.Info, this, "{0:f1}sec, {1:f2}Mbps", sw.Elapsed.TotalSeconds, datasize * 8 / sw.Elapsed.TotalSeconds / 1000.0 / 1000.0);
                    } catch {
                    } finally {
                        timeoutChecker.Dispose ();
                        if (sock1 != null) sock1.Dispose ();
                        if (sock2 != null) sock2.Dispose ();
                        if (strm1 != null) strm1.Dispose ();
                        if (strm2 != null) strm2.Dispose ();
                    }
                } while (!routeEstablished);
            }
        }
예제 #2
0
파일: KBREval1.cs 프로젝트: kazuki/p2pncs
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                VirtualNode testNode = env.Nodes[0];

                int returned = 0;
                int[] candidates = new int[3];
                ManualResetEvent reqDone = new ManualResetEvent (false);
                for (int i = 0; i < opt.Tests; i++) {
                    Key target = Key.CreateRandom (testNode.NodeID.KeyBytes);
                    testNode.KeyBasedRouter.BeginRoute (target, null, candidates.Length, 4, delegate (IAsyncResult ar) {
                        Key target2 = ar.AsyncState as Key;
                        RoutingResult result = testNode.KeyBasedRouter.EndRoute (ar);
                        if (result != null && result.RootCandidates != null && result.RootCandidates.Length > 0) {
                            VirtualNode[] nodes;
                            lock (env.Nodes) {
                                nodes = env.Nodes.ToArray ();
                            }
                            Array.Sort<VirtualNode> (nodes, delegate (VirtualNode x, VirtualNode y) {
                                Key diff1 = testNode.KeyBasedRouter.RoutingAlgorithm.ComputeDistance (target2, x.NodeID);
                                Key diff2 = testNode.KeyBasedRouter.RoutingAlgorithm.ComputeDistance (target2, y.NodeID);
                                return diff1.CompareTo (diff2);
                            });
                            for (int k = 0; k < Math.Min (candidates.Length, result.RootCandidates.Length); k ++) {
                                if (nodes[k].NodeID.Equals (result.RootCandidates[k].NodeID))
                                    candidates[k] ++;
                            }
                        }
                        int ret = Interlocked.Increment (ref returned);
                        if (ret == opt.Tests)
                            reqDone.Set ();
                        Console.Write ("*");
                    }, target);
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }
                reqDone.WaitOne ();
                for (int i = 0; i < candidates.Length; i ++)
                    Console.Write ("{0}/", candidates[i]);
                Console.WriteLine ("{0}", returned);
            }
        }
예제 #3
0
파일: DHTEval1.cs 프로젝트: kazuki/p2pncs
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);
                env.StartChurn ();

                VirtualNode testNode = env.Nodes[0];
                List<Key> list = new List<Key> ();
                for (int i = 0; i < opt.Tests; i++) {
                    Key key = Key.CreateRandom (testNode.NodeID.KeyBytes);
                    list.Add (key);
                    testNode.DistributedHashTable.BeginPut (key, TimeSpan.FromHours (1), Convert.ToBase64String (key.GetByteArray ()), null, null);
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }

                int returned = 0, successed = 0;
                ManualResetEvent getDone = new ManualResetEvent (false);
                for (int i = 0; i < opt.Tests; i++) {
                    testNode.DistributedHashTable.BeginGet (list[i], typeof (string), delegate (IAsyncResult ar) {
                        GetResult result = testNode.DistributedHashTable.EndGet (ar);
                        string expected = ar.AsyncState as string;
                        if (result != null && result.Values != null && result.Values.Length > 0) {
                            if (expected.Equals (result.Values[0] as string)) {
                                Interlocked.Increment (ref successed);
                                Console.Write ("*");
                            } else {
                                Console.Write ("=");
                            }
                        } else {
                            Console.Write ("?");
                        }
                        if (Interlocked.Increment (ref returned) == opt.Tests)
                            getDone.Set ();
                    }, Convert.ToBase64String (list[i].GetByteArray ()));
                    Thread.Sleep (TimeSpan.FromSeconds (0.2));
                }
                getDone.WaitOne ();
                Console.WriteLine ("{0}/{1}", successed, returned);
            }
        }
예제 #4
0
        public void Evaluate(EvalOptionSet opt)
        {
            using (EvalEnvironment env = new EvalEnvironment (opt)) {
                env.AddNodes (opt.NumberOfNodes, true);

                ISubscribeInfo subscribeInfo1 = env.Nodes[0].Subscribe ();
                ISubscribeInfo subscribeInfo2 = env.Nodes[1].Subscribe ();
                string strKey1 = subscribeInfo1.Key.ToString ();
                string strKey2 = subscribeInfo2.Key.ToString ();

                IMessagingSocket msock1 = null, msock2 = null;

                while (true) {
                    if (subscribeInfo1.Status == SubscribeRouteStatus.Stable && subscribeInfo2.Status == SubscribeRouteStatus.Stable)
                        break;
                    Thread.Sleep (10);
                }

                env.StartChurn ();
                bool routeEstablished = false;
                do {
                    try {
                        IAsyncResult ar = env.Nodes[0].AnonymousRouter.BeginConnect (subscribeInfo1.Key, subscribeInfo2.Key, AnonymousConnectionType.LowLatency, null, null, null);
                        Stopwatch sw = Stopwatch.StartNew ();
                        IAnonymousSocket sock1 = env.Nodes[0].AnonymousRouter.EndConnect (ar);
                        sw.Stop ();
                        if (env.Nodes[1].AnonymousSocketInfoList.Count == 0) {
                            throw new System.Net.Sockets.SocketException ();
                        }
                        Logger.Log (LogLevel.Info, this, "Connected: {0}ms", sw.ElapsedMilliseconds);

                        msock1 = env.Nodes[0].CreateAnonymousSocket (sock1);
                        msock2 = env.Nodes[1].AnonymousSocketInfoList[env.Nodes[1].AnonymousSocketInfoList.Count - 1].MessagingSocket;
                        routeEstablished = true;

                        DateTime dt = DateTime.Now;
                        StandardDeviation rtt_sd = new StandardDeviation (false);
                        int tests = 0, success = 0;
                        for (int i = 0; i < opt.Tests; i ++) {
                            string ret;

                            tests ++;
                            string msg = "Hello-" + strKey1 + "-" + i.ToString ();
                            sw.Reset (); sw.Start ();
                            ar = msock1.BeginInquire (msg, AnonymousRouter.DummyEndPoint, null, null);
                            ret = msock1.EndInquire (ar) as string;
                            sw.Stop ();
                            if (ret == null) {
                                Console.Write ("?");
                            } else if (ret != msg + "-" + strKey2 + "-ok") {
                                Console.Write ("@");
                            } else {
                                rtt_sd.AddSample ((float)sw.Elapsed.TotalMilliseconds);
                                Console.Write ("*");
                                success ++;
                            }

                            tests++;
                            msg = "Hello-" + strKey1 + "-" + i.ToString ();
                            sw.Reset (); sw.Start ();
                            ar = msock2.BeginInquire (msg, AnonymousRouter.DummyEndPoint, null, null);
                            ret = msock2.EndInquire (ar) as string;
                            sw.Stop ();
                            if (ret == null) {
                                Console.Write ("?");
                            } else if (ret != msg + "-" + strKey1 + "-ok") {
                                Console.Write ("@");
                            } else {
                                rtt_sd.AddSample ((float)sw.Elapsed.TotalMilliseconds);
                                Console.Write ("=");
                                success++;
                            }
                        }
                        Console.WriteLine ();
                        int minJitter, maxJitter;
                        double avgJitter, sdJitter;
                        env.Network.GetAndResetJitterHistory (out minJitter, out avgJitter, out sdJitter, out maxJitter);
                        Logger.Log (LogLevel.Info, this, "Time: {0:f2}sec, Jitter: {1}/{2:f1}({3:f1})/{4}, DeliverSuccess={5:p}, RTT: Avg={6:f1}({7:f1})",
                            DateTime.Now.Subtract (dt).TotalSeconds, minJitter, avgJitter, sdJitter, maxJitter, (double)success / (double)tests, rtt_sd.Average, rtt_sd.ComputeStandardDeviation ());
                    } catch {
                        Logger.Log (LogLevel.Info, this, "Establish Failed. Retry...");
                        return;
                    } finally {
                        if (msock1 != null) msock1.Dispose ();
                        if (msock2 != null) msock2.Dispose ();
                    }
                } while (!routeEstablished);
            }
        }