static void Main(string[] args) { args = new string[] { "127.0.0.1:10080" , "127.0.0.1:10081" }; _bufferManager = new BufferManager ( 64 * 1024 * 1024 , 64 * 1024 ); _bufferManager.InitBuffer(); _socketAsyncEventArgsPool = new SocketAsyncEventArgsPool(100); var performanceCountersCategoryName = "Microshaoft EasyPerformanceCounters Category"; EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , "Hander1::Sended" ); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , "Hander2::Sended" ); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , "Hander1::Received" ); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , "Hander2::Received" ); Console.WriteLine(@"Press any key to Send! Press ""q"" to release resource"); string s = string.Empty; while ((s = Console.ReadLine().ToLower()) != "q") { Run(args); } }
public static void AttachPerformanceCounters ( HttpConfiguration httpConfiguration , string controllerCategoryNamePrefix = null , string actionInstanceNamePrefix = null ) { IApiExplorer apiExplorer = httpConfiguration .Services .GetApiExplorer(); var apiDescriptions = apiExplorer .ApiDescriptions; var groups = apiDescriptions .ToLookup ( (x) => { return (x .ActionDescriptor .ControllerDescriptor .ControllerName); } ); var processName = Process .GetCurrentProcess() .ProcessName; groups .AsParallel() .WithDegreeOfParallelism(groups.Count) .ForAll ( (x) => { bool controllerPerformanceCounterProcessed = false; foreach (var xx in x) { var performanceCounterCategoryName = string .Format ( "{1}{0}{2}" , "-" , controllerCategoryNamePrefix , x.Key ); var performanceCounterInstanceName = string.Empty; CommonPerformanceCountersContainer commonPerformanceCountersContainer = null; if (!controllerPerformanceCounterProcessed) { performanceCounterInstanceName = "*"; var controllerAttribute = xx .ActionDescriptor .ControllerDescriptor .GetCustomAttributes <CommonPerformanceCounterAttribute>() .FirstOrDefault(); if (controllerAttribute != null) { if (!string.IsNullOrEmpty(controllerAttribute.PerformanceCounterCategoryName)) { performanceCounterCategoryName = controllerAttribute.PerformanceCounterCategoryName; } if (!string.IsNullOrEmpty(controllerAttribute.PerformanceCounterInstanceName)) { performanceCounterInstanceName = controllerAttribute.PerformanceCounterInstanceName; } } EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCounterCategoryName , performanceCounterInstanceName , out commonPerformanceCountersContainer ); controllerPerformanceCounterProcessed = true; } performanceCounterCategoryName = string .Format ( "{1}{0}{2}" , "-" , controllerCategoryNamePrefix , x.Key ); performanceCounterInstanceName = string .Format ( "{1}{0}{2}{0}{3}{0}{4}{0}{5}" , "-" , actionInstanceNamePrefix , xx .ActionDescriptor .ControllerDescriptor .ControllerName , xx .ActionDescriptor .ActionName , xx .HttpMethod .Method , processName ); var actionAttribute = xx .ActionDescriptor .GetCustomAttributes <CommonPerformanceCounterAttribute>() .FirstOrDefault(); if (actionAttribute != null) { if (!string.IsNullOrEmpty(actionAttribute.PerformanceCounterCategoryName)) { performanceCounterCategoryName = actionAttribute.PerformanceCounterCategoryName; } if (!string.IsNullOrEmpty(actionAttribute.PerformanceCounterInstanceName)) { performanceCounterInstanceName = actionAttribute.PerformanceCounterInstanceName; } } EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCounterCategoryName , performanceCounterInstanceName , out commonPerformanceCountersContainer ); } } ); }
static void Run(string[] args) { var performanceCountersCategoryName = "Microshaoft EasyPerformanceCounters Category"; var enableCounters = MultiPerformanceCountersTypeFlags.ProcessCounter | MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter | MultiPerformanceCountersTypeFlags.ProcessedCounter | MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter | MultiPerformanceCountersTypeFlags.ProcessingCounter; var sendEncoding = Encoding.Default; var receiveEncoding = Encoding.Default; //byte[] data = new byte[1024]; string[] a = args[0].Split(new char[] { ':' }); string ip = a[0]; int port = int.Parse(a[1]); IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse(ip), port); Console.WriteLine("ipep1 {0}", ipep1.ToString()); a = args[1].Split(new char[] { ':' }); ip = a[0]; port = int.Parse(a[1]); IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse(ip), port); Console.WriteLine("ipep2 {0}", ipep2.ToString()); var remoteAnyIPEP = new IPEndPoint(IPAddress.Any, 0); Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket1.Bind(ipep1); SocketAsyncDataHandler <string> handler1 = new SocketAsyncDataHandler <string>(socket1, 1); var receiveSocketAsyncEventArgs1 = _socketAsyncEventArgsPool.Pop(); _bufferManager.SetBuffer(receiveSocketAsyncEventArgs1); handler1.StartReceiveDataFrom ( remoteAnyIPEP , receiveSocketAsyncEventArgs1 , (x, y, z, w) => { Console.WriteLine("次数: {0}", x.ReceivedCount); Console.WriteLine("字节: {0}", z.Length); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformance ( enableCounters , performanceCountersCategoryName , "Hander1::Received" , null , () => { var ss = receiveEncoding.GetString(z); //Console.Write(s); Console.WriteLine ( "from {0} , to {1}, data {2}" , x.WorkingSocket.LocalEndPoint , w.RemoteEndPoint , ss ); } , null , null , null ); return(false); } , (x, y, z) => { Console.WriteLine(z.ToString()); return(true); } ); Socket socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket2.Bind(ipep2); SocketAsyncDataHandler <string> handler2 = new SocketAsyncDataHandler <string>(socket2, 2); var receiveSocketAsyncEventArgs2 = _socketAsyncEventArgsPool.Pop(); _bufferManager.SetBuffer(receiveSocketAsyncEventArgs2); handler2.StartReceiveDataFrom ( remoteAnyIPEP , receiveSocketAsyncEventArgs2 , (x, y, z, w) => { Console.WriteLine("次数: {0}", x.ReceivedCount); Console.WriteLine("字节: {0}", z.Length); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformance ( enableCounters , performanceCountersCategoryName , "Hander2::Received" , null , () => { var ss = receiveEncoding.GetString(z); //Console.Write(s); Console.WriteLine ( "from {0} , to {1}, data {2}" , x.WorkingSocket.LocalEndPoint , w.RemoteEndPoint , ss ); } , null , null , null ); return(false); } , (x, y, z) => { Console.WriteLine(z.ToString()); return(true); } ); string s = string.Empty; Console.WriteLine("Send ..."); while ((s = Console.ReadLine().ToLower()) != "q") { var buffer = sendEncoding.GetBytes(s); Parallel.For ( 0 , 1000 , new ParallelOptions() { MaxDegreeOfParallelism = 1 // Environment.ProcessorCount //, TaskScheduler = null } , i => { Thread.Sleep(5); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformance ( enableCounters , performanceCountersCategoryName , "Hander1::Sended" , null , () => { handler1.SendDataToSync(buffer, ipep2); } , null , null , null ); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformance ( enableCounters , performanceCountersCategoryName , "Hander2::Sended" , null , () => { handler2.SendDataToSync(buffer, ipep1); } , null , null , null ); } ); } var e = handler1.ReceiveSocketAsyncEventArgs; //_bufferManager.FreeBuffer(e); _socketAsyncEventArgsPool.Push(e); e = handler2.ReceiveSocketAsyncEventArgs; //_bufferManager.FreeBuffer(e); _socketAsyncEventArgsPool.Push(e); handler1.DestoryWorkingSocket(); handler2.DestoryWorkingSocket(); Console.WriteLine("Send quit"); }
static void Main1(string[] args) { Console.WriteLine("Begin ..."); Random r = new Random(); int sleep = 2; int iterations = 10; int maxDegreeOfParallelism = 8; // Environment.ProcessorCount; var performanceCountersCategoryName = "Microshaoft EasyPerformanceCounters Category"; var performanceCountersCategoryInstanceName = string.Format ( "{2}{0}{3}{1}{4}" , ": " , " @ " , "" , "" , Process.GetCurrentProcess().ProcessName ); //EasyPerformanceCountersHelper 调用示例 EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , performanceCountersCategoryInstanceName + "-1" ); EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance ( performanceCountersCategoryName , performanceCountersCategoryInstanceName + "-2" ); var enableCounters = MultiPerformanceCountersTypeFlags.ProcessCounter | MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter | MultiPerformanceCountersTypeFlags.ProcessedCounter | MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter | MultiPerformanceCountersTypeFlags.ProcessingCounter; Parallel.For ( 0 , iterations , new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism } , (x) => { EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformance ( enableCounters , performanceCountersCategoryName , performanceCountersCategoryInstanceName + "-1" , null , () => { sleep = r.Next(0, 5) * 1000; //Thread.Sleep(sleep); throw new Exception("sadsad"); } , null , (xx) => { //Console.WriteLine("Exception {0}", xx.ToString()); return(false); } , null ); } ); Parallel.For ( 0 , iterations , new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism } , (x) => { Stopwatch stopwatch = null; try { stopwatch = EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformanceBegin ( enableCounters , performanceCountersCategoryName , performanceCountersCategoryInstanceName + "-2" ); sleep = r.Next(0, 5) * 1000; //Thread.Sleep(sleep); throw new Exception("Test"); } catch { } finally { EasyPerformanceCountersHelper <CommonPerformanceCountersContainer> .CountPerformanceEnd ( enableCounters , performanceCountersCategoryName , performanceCountersCategoryInstanceName + "-2" , stopwatch ); } } ); Console.WriteLine("End ..."); Console.ReadLine(); }