예제 #1
0
        public static void RunHttp(String UrlPrefix, String ServiceVirtualPath, Boolean UseOld)
        {
            IApplicationClient ac;

            Http.IHttpVirtualTransportClient vtc;
            var a = new JsonSerializationClientAdapter();

            ac  = a.GetApplicationClient();
            vtc = new Http.JsonHttpPacketClient(a);
            using (var bc = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc))
            {
                Console.WriteLine("输入login登录。");

                var Lockee = new Object();
                Action <SecureContext> SetSecureContext = c =>
                {
                    throw new InvalidOperationException();
                };
                ReadLineAndSendLoop(ac, SetSecureContext, UseOld, Lockee);
            }
        }
예제 #2
0
        public static void TestHttpForNumUser(String UrlPrefix, String ServiceVirtualPath, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test, Action <int, int, ClientContext, IApplicationClient, Action> InitializeClientContext = null, Action <ClientContext[]> FinalCheck = null)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tl         = new List <Task>();
            var bcl        = new List <Http.HttpClient>();
            var ccl        = new List <ClientContext>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                var n      = k;
                var Lockee = new Object();
                var a      = new JsonSerializationClientAdapter();
                var ac     = a.GetApplicationClient();
                var vtc    = new Http.JsonHttpPacketClient(a);
                var bc     = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc);
                var cc     = new ClientContext();
                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                Action Completed;
                if (Test == TestQuit)
                {
                    Completed = () =>
                    {
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    };
                }
                else
                {
                    Completed = () =>
                    {
                        ac.Quit(new QuitRequest {
                        }).ContinueWith(tt =>
                        {
                            vCompleted.Update(i => i + 1);
                            Check.Set();
                        });
                    };
                }
                if (InitializeClientContext != null)
                {
                    InitializeClientContext(NumUser, n, cc, ac, Completed);
                }
                try
                {
                    ac.ServerTime(new ServerTimeRequest {
                    }).ContinueWith(tt =>
                    {
                        vConnected.Update(i => i + 1);
                        Check.Set();
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    Completed();
                }
                var t = new Task
                        (
                    () =>
                {
                    try
                    {
                        Test(NumUser, n, cc, ac, Completed);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                        Completed();
                    }
                }
                        );
                tl.Add(t);
                bcl.Add(bc);
                ccl.Add(cc);
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            foreach (var t in tl)
            {
                t.Start();
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            if (FinalCheck != null)
            {
                FinalCheck(ccl.ToArray());
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            Console.WriteLine("{0} Users, {1} ms", NumUser, TimeDiff);
        }
예제 #3
0
        public static void TestHttpForNumUser(String UrlPrefix, String ServiceVirtualPath, int NumRequestPerUser, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tll        = new Object();
            var tl         = new List <Task>();
            var bcl        = new List <Http.HttpClient>();
            var ccl        = new List <ClientContext>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                Action Completed = null;

                var n      = k;
                var Lockee = new Object();
                var a      = new JsonSerializationClientAdapter();
                var ac     = a.GetApplicationClient();
                var vtc    = new Http.JsonHttpPacketClient(a);
                var bc     = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc);
                var cc     = new ClientContext();
                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                Action <Exception> HandleError = ex =>
                {
                    int OldValue = 0;
                    vError.Update(v =>
                    {
                        OldValue = v;
                        return(v + 1);
                    });
                    if (OldValue <= 10)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    }
                    vCompleted.Update(i => i + 1);
                    Check.Set();
                };
                try
                {
                    ac.ServerTime(new ServerTimeRequest {
                    }).ContinueWith(tt =>
                    {
                        vConnected.Update(i => i + 1);
                        Check.Set();
                    });
                }
                catch (Exception ex)
                {
                    HandleError(ex);
                }
                Action f = () =>
                {
                    try
                    {
                        Test(NumUser, n, cc, ac, Completed);
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex);
                    }
                };
                var t = new Task(f);
                lock (tll)
                {
                    tl.Add(t);
                }
                bcl.Add(bc);
                ccl.Add(cc);

                int RequestCount = NumRequestPerUser;
                Completed = () =>
                {
                    if (RequestCount > 0)
                    {
                        RequestCount -= 1;
                        var tt = new Task(f);
                        lock (tll)
                        {
                            tl.Add(t);
                        }
                        tt.Start();
                        return;
                    }
                    ac.Quit(new QuitRequest {
                    }).ContinueWith(tt =>
                    {
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    });
                };
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            lock (tll)
            {
                foreach (var t in tl)
                {
                    t.Start();
                }
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            Console.WriteLine("{0} Users, {1} Request/User, {2} ms", NumUser, NumRequestPerUser, TimeDiff);
        }