public static int DoTestUdp(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 8, 1, "TestAdd", TestAdd); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 8, 1, "TestMultiply", TestMultiply); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 8, 1, "TestText", TestText); Thread.Sleep(5000); for (int k = 0; k < 4; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), 4096, "TestAdd", TestAdd); } Thread.Sleep(5000); for (int k = 0; k < 4; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), 4096, "TestMultiply", TestMultiply); } Thread.Sleep(5000); for (int k = 0; k < 4; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), 4096, "TestText", TestText); } return(0); }
public static int DoTestUdp(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType) { //如果在Windows测试提示“由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。”(WSAENOBUFS 10055),表示系统的UDP端口数不足 //可以使用下述命令查看动态端口范围 //netsh int ipv4 show dynamicport udp //可以在管理员权限下使用下述命令设置 //netsh int ipv4 set dynamicport udp start=16384 num=49152 //参见:http://support.microsoft.com/kb/929851/en-us TestUdpForNumUser(RemoteEndPoint, ProtocolType, 64, "TestQuit", TestQuit); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 64, "TestAdd", TestAdd); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 64, "TestMultiply", TestMultiply); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 64, "TestText", TestText); Thread.Sleep(1000); TestUdpForNumUser(RemoteEndPoint, ProtocolType, 64, "TestMessage", TestMessage, TestMessageInitializeClientContext, TestMessageFinalCheck); Thread.Sleep(5000); for (int k = 0; k < 8; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), "TestQuit", TestQuit); } Thread.Sleep(5000); for (int k = 0; k < 8; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), "TestAdd", TestAdd); } Thread.Sleep(5000); for (int k = 0; k < 7; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), "TestMultiply", TestMultiply); } Thread.Sleep(5000); for (int k = 0; k < 7; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), "TestText", TestText); } Thread.Sleep(10000); for (int k = 0; k < 6; k += 1) { TestUdpForNumUser(RemoteEndPoint, ProtocolType, 1 << (2 * k), "TestMessage", TestMessage, TestMessageInitializeClientContext, TestMessageFinalCheck); Thread.Sleep(1000); } return(0); }
public static void RunUdp(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, Boolean UseOld) { if (!(ProtocolType == SerializationProtocolType.Binary || ProtocolType == SerializationProtocolType.Json)) { Console.WriteLine("协议不能识别:" + ProtocolType.ToString()); } IApplicationClient ac; IStreamedVirtualTransportClient vtc; var bt = new Rc4PacketClientTransformer(); if (ProtocolType == SerializationProtocolType.Binary) { var a = new BinarySerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new BinaryCountPacketClient(a, bt); } else if (ProtocolType == SerializationProtocolType.Json) { var a = new JsonSerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new JsonLinePacketClient(a, bt); } else { throw new InvalidOperationException(); } using (var bc = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem)) { bc.Connect(); Console.WriteLine("输入login登录,输入secure启用安全连接。"); var Lockee = new Object(); Action <Action> DoHandle = a => { lock (Lockee) { a(); } }; bc.ReceiveAsync(DoHandle, ex => Console.WriteLine(ex.Message)); Action <SecureContext> SetSecureContext = c => { bt.SetSecureContext(c); bc.SecureContext = c; }; ReadLineAndSendLoop(ac, SetSecureContext, UseOld, Lockee); } }
public static void TestUdpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, 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 <UdpClient>(); var ccl = new List <ClientContext>(); var tmrl = new List <Timer>(); var vConnected = new LockedVariable <int>(0); var vCompleted = new LockedVariable <int>(0); var Check = new AutoResetEvent(false); var bAbondon = new LockedVariable <Boolean>(false); var vError = new LockedVariable <int>(0); for (int k = 0; k < NumUser; k += 1) { var n = k; var Lockee = new Object(); IApplicationClient ac; IStreamedVirtualTransportClient vtc; if (ProtocolType == SerializationProtocolType.Binary) { var a = new BinarySerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new BinaryCountPacketClient(a); } else if (ProtocolType == SerializationProtocolType.Json) { var a = new JsonSerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new JsonLinePacketClient(a); } else { throw new InvalidOperationException(); } var bc = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem); var cc = new ClientContext(); var bCompleted = new LockedVariable <Boolean>(false); Action Completed; Action FaultedCompleted; if (Test == TestQuit) { Completed = () => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }; FaultedCompleted = Completed; } else { Completed = () => { ac.Quit(new QuitRequest { }).ContinueWith(tt => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }); }; FaultedCompleted = () => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }; } ac.Error += e => { var m = e.Message; Console.WriteLine(m); }; if (InitializeClientContext != null) { InitializeClientContext(NumUser, n, cc, ac, Completed); } bc.Connect(); Action <Exception> UnknownFaulted = ex => { int OldValue = 0; vError.Update(v => { OldValue = v; return(v + 1); }); if (OldValue <= 10) { Console.WriteLine(String.Format("{0}:{1}", n, ex.Message)); } FaultedCompleted(); }; bc.ReceiveAsync ( a => { a(); }, UnknownFaulted ); ac.ServerTime(new ServerTimeRequest { }).ContinueWith(tt => { vConnected.Update(i => i + 1); Check.Set(); }); var t = new Task ( () => { Test(NumUser, n, cc, ac, Completed); } ); var tmr = new Timer ( o => { if (!bAbondon.Check(b => b)) { return; } if (bCompleted.Check(b => b)) { return; } int OldValue = 0; vError.Update(v => { OldValue = v; return(v + 1); }); if (OldValue <= 10) { Console.WriteLine(String.Format("{0}:{1}", n, "Timedout")); } FaultedCompleted(); }, null, 10000, 10000 ); tl.Add(t); bcl.Add(bc); ccl.Add(cc); tmrl.Add(tmr); } 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)) { if (!Check.WaitOne(10000)) { if (vCompleted.Check(i => i > 0)) { bAbondon.Update(b => true); break; } } } var NumMutualWaiting = NumUser - vCompleted.Check(i => i); while (vCompleted.Check(i => i != NumUser)) { Check.WaitOne(); } foreach (var tmr in tmrl) { tmr.Dispose(); } 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); } if (NumMutualWaiting > 0) { Console.WriteLine("{0} Users, {1} ms, {2} MutualWaiting", NumUser, TimeDiff, NumMutualWaiting); } else { Console.WriteLine("{0} Users, {1} ms", NumUser, TimeDiff); } }
public static int DoTestUdp(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType) { TestUdpForNumUser(RemoteEndPoint, 4096, "TestHalfConnection"); return(0); }
public static void TestTcpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, 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 <TcpClient>(); 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(); IApplicationClient ac; IStreamedVirtualTransportClient vtc; if (ProtocolType == SerializationProtocolType.Binary) { var a = new BinarySerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new BinaryCountPacketClient(a); } else if (ProtocolType == SerializationProtocolType.Json) { var a = new JsonSerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new JsonLinePacketClient(a); } else { throw new InvalidOperationException(); } var bc = new TcpClient(RemoteEndPoint, vtc, QueueUserWorkItem); var cc = new ClientContext(); ac.Error += e => { var m = e.Message; Console.WriteLine(m); }; bc.Connect(); Action <Exception> UnknownFaulted = 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(); }; bc.ReceiveAsync ( a => { a(); }, UnknownFaulted ); ac.ServerTime(new ServerTimeRequest { }).ContinueWith(tt => { vConnected.Update(i => i + 1); Check.Set(); }); Action f = () => { Test(NumUser, n, cc, ac, Completed); }; 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; } 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); }
public static int MainInner() { DisplayTitle(); var CmdLine = CommandLine.GetCmdLine(); var UseOld = false; var UseLoadTest = false; var UsePerformanceTest = false; var UseStableTest = false; foreach (var opt in CmdLine.Options) { if ((opt.Name.ToLower() == "?") || (opt.Name.ToLower() == "help")) { DisplayInfo(); return(0); } else if (opt.Name.ToLower() == "old") { UseOld = true; } else if (opt.Name.ToLower() == "load") { UseLoadTest = true; } else if (opt.Name.ToLower() == "perf") { UsePerformanceTest = true; } else if (opt.Name.ToLower() == "stable") { UseStableTest = true; } } var argv = CmdLine.Arguments; TransportProtocolType TransportProtocolType = TransportProtocolType.Tcp; SerializationProtocolType SerializationProtocolType = SerializationProtocolType.Binary; int DefaultPort = 8001; if (argv.Length >= 1) { TransportProtocolType = (TransportProtocolType)Enum.Parse(typeof(TransportProtocolType), argv[0], true); } if (TransportProtocolType == TransportProtocolType.Tcp) { if (argv.Length >= 2) { SerializationProtocolType = (SerializationProtocolType)Enum.Parse(typeof(SerializationProtocolType), argv[1], true); } if (SerializationProtocolType == SerializationProtocolType.Binary) { DefaultPort = 8001; } else if (SerializationProtocolType == SerializationProtocolType.Json) { DefaultPort = 8002; } } else if (TransportProtocolType == TransportProtocolType.Udp) { if (argv.Length >= 2) { SerializationProtocolType = (SerializationProtocolType)Enum.Parse(typeof(SerializationProtocolType), argv[1], true); } if (SerializationProtocolType == SerializationProtocolType.Binary) { DefaultPort = 8001; } else if (SerializationProtocolType == SerializationProtocolType.Json) { DefaultPort = 8002; } } if (TransportProtocolType == TransportProtocolType.Tcp) { IPEndPoint RemoteEndPoint; if (argv.Length == 4) { RemoteEndPoint = new IPEndPoint(IPAddress.Parse(argv[2]), int.Parse(argv[3])); } else if (argv.Length == 2) { RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), DefaultPort); } else if (argv.Length == 0) { RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), DefaultPort); } else { DisplayInfo(); return(-1); } if (UseLoadTest) { LoadTest.DoTestTcp(RemoteEndPoint, SerializationProtocolType); } else if (UsePerformanceTest) { PerformanceTest.DoTestTcp(RemoteEndPoint, SerializationProtocolType); } else if (UseStableTest) { StableTest.DoTestTcp(RemoteEndPoint, SerializationProtocolType); } else { RunTcp(RemoteEndPoint, SerializationProtocolType, UseOld); } } else if (TransportProtocolType == TransportProtocolType.Udp) { IPEndPoint RemoteEndPoint; if (argv.Length == 4) { RemoteEndPoint = new IPEndPoint(IPAddress.Parse(argv[2]), int.Parse(argv[3])); } else if (argv.Length == 2) { RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), DefaultPort); } else { DisplayInfo(); return(-1); } if (UseLoadTest) { LoadTest.DoTestUdp(RemoteEndPoint, SerializationProtocolType); } else if (UsePerformanceTest) { PerformanceTest.DoTestUdp(RemoteEndPoint, SerializationProtocolType); } else if (UseStableTest) { StableTest.DoTestTcp(RemoteEndPoint, SerializationProtocolType); } else { RunUdp(RemoteEndPoint, SerializationProtocolType, UseOld); } } else if (TransportProtocolType == TransportProtocolType.Http) { String UrlPrefix = "http://localhost:8003/api/"; String ServiceVirtualPath = "q"; if (argv.Length == 3) { UrlPrefix = argv[1]; ServiceVirtualPath = argv[2]; } else if (argv.Length == 1) { } else { DisplayInfo(); return(-1); } if (UseLoadTest) { LoadTest.DoTestHttp(UrlPrefix, ServiceVirtualPath); } else if (UsePerformanceTest) { PerformanceTest.DoTestHttp(UrlPrefix, ServiceVirtualPath); } else if (UseStableTest) { StableTest.DoTestHttp(UrlPrefix, ServiceVirtualPath); } else { RunHttp(UrlPrefix, ServiceVirtualPath, UseOld); } } else { DisplayInfo(); return(-1); } return(0); }