Exemple #1
0
        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
                    );
                }
            }
            );
        }
Exemple #3
0
        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();
        }