コード例 #1
0
        static void Main(string[] args)
        {
            var counterFactory = PerfCountersInstantiationFactory.CreateCounterFactoryFromAppConfig("PerfCountersConfigurationSection");

            PerfCounters.Init(counterFactory.CreateRootWrapper());
            counterFactory.InitAll();

            var momentTimer = PerfCounters.TestSingle.MomentTime.StartNew();

            for (int i = 0; i < 100; i++)
            {
                PerfCounters.TestSingle.Delta.Increment();
                Thread.Sleep(50);

                if (i % 10 == 9)
                {
                    Console.WriteLine("Delta Current = " + PerfCounters.TestSingle.Delta.CurrentValue.ToString());
                    Console.WriteLine("Delta Measure = " + PerfCounters.TestSingle.Delta.Measure().ToString());
                }
            }
            Console.WriteLine("Delta Last = " + PerfCounters.TestSingle.Delta.CurrentValue.ToString());



            for (int i = 0; i < 1000; i++)
            {
                PerfCounters.TestSingle.Count.Increment();
            }
            Console.WriteLine("Count = " + PerfCounters.TestSingle.Count.CurrentValue.ToString());



            for (int i = 0; i < 1000; i++)
            {
                Thread.Sleep(5);
                PerfCounters.TestSingle.OperationPerSec.OperationFinished();
            }
            Console.WriteLine("OperationPerSec = " + PerfCounters.TestSingle.OperationPerSec.CurrentValue.ToString());



            Random rnd = new Random();

            for (int i = 0; i < 1000; i++)
            {
                PerfCounters.TestSingle.Avg.RegisterValue(rnd.Next(0, 100));
            }
            Console.WriteLine("Avg = " + PerfCounters.TestSingle.Avg.CurrentValue.ToString());



            for (int i = 0; i < 1000; i++)
            {
                var timer = PerfCounters.TestSingle.AvgTime.StartNew();
                Thread.Sleep(rnd.Next(0, 10));
                timer.Complete();
            }
            for (int i = 0; i < 1000; i++)
            {
                using (PerfCounters.TestSingle.AvgTime.StartNew())
                {
                    Thread.Sleep(rnd.Next(0, 10));
                }
            }
            Console.WriteLine("AvgTime = " + PerfCounters.TestSingle.AvgTime.CurrentValue.ToString());


            momentTimer.Complete();
            Console.WriteLine("MomentTime = " + PerfCounters.TestSingle.MomentTime.CurrentValue.ToString());
            Console.WriteLine("Elapsed = " + PerfCounters.TestSingle.Elapsed.CurrentValue.ToString());



            for (int i = 0; i < 100000; i++)
            {
                PerfCounters.TestMulti[rnd.Next(0, 10).ToString()].Count.Increment();
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Instance #" + i.ToString() + ": " + PerfCounters.TestMulti[i.ToString()].Count.CurrentValue.ToString());
                PerfCounters.TestMulti[i.ToString()].Remove();
            }


            counterFactory.Dispose();

            Console.WriteLine("========= Completed =========");
            Console.ReadLine();
        }
コード例 #2
0
        public static void RunTest()
        {
            //TestPerf();
            //CompareCounters2();

            // Console.ReadLine();

            //var f = PerformanceCounterCategory.GetCategories();

            //CounterCreationDataCollection f = new CounterCreationDataCollection();
            //f.Add(new CounterCreationData("RawFraction", "RawFraction", PerformanceCounterType.RawFraction));
            //f.Add(new CounterCreationData("RawFractionBase", "RawFractionBase", PerformanceCounterType.RawBase));
            //var cat = PerformanceCounterCategory.Create("testCat3", "testCat3", f);

            //var perfCnt = new PerformanceCounter("testCat3", "RawFraction", false);
            //var perfCntBase = new PerformanceCounter("testCat3", "RawFractionBase", false);

            //double valDbl = 51.4;


            //perfCnt.RawValue = (long)(valDbl * 10000);
            //perfCntBase.RawValue = 100 * 10000;

            //for (int i = 0; i < 1000; i++)
            //{
            //    perfCnt.IncrementBy(10);
            //    Thread.Sleep(100);
            //}

            //return;


            //var rootFact = new Qoollo.PerformanceCounters.WinCounters.WinCounterFactory(Qoollo.PerformanceCounters.WinCounters.WinCountersInstantiationMode.UseExistedIfPossible);
            //var rootFact = new Qoollo.PerformanceCounters.InternalCounters.InternalCounterFactory();
            //var rootFact = Qoollo.PerformanceCounters.NullCounters.NullCounterFactory.Default;
            //var rootFact = new Qoollo.PerformanceCounters.CompositeCounters.CompositeCounterFactory(new CounterFactory[]
            //    {
            //        new Qoollo.PerformanceCounters.InternalCounters.InternalCounterFactory(),
            //        new Qoollo.PerformanceCounters.WinCounters.WinCounterFactory(Qoollo.PerformanceCounters.WinCounters.WinCountersInstantiationMode.UseExistedIfPossible)
            //    });

            var rootFact = PerfCountersInstantiationFactory.CreateCounterFactoryFromAppConfig("PerfCountersConfigurationSection");

            //var si = rootFact.CreateSingleInstanceCategory("aaa", "aaa");
            //var cntr = si.CreateNumberOfItemsCounter("asd", "asd");
            //cntr.Increment();

            //rootFact.InitAll();
            //cntr.SetValue(1000);

            AttributeCounters.Init(rootFact.CreateRootWrapper());
            rootFact.InitAll();


            var tmr = AttributeCounters.Instance.MomentTime.StartNew();
            var avg = AttributeCounters.Instance.AverageTimer32.StartNew();

            Thread.Sleep(1000);
            tmr.Complete();
            avg.Complete();

            Console.ReadLine();

            AttributeCounters.Instance.ElapsedTime.Reset();
            Thread.Sleep(10000);
            var tm = AttributeCounters.Instance.ElapsedTime.CurrentValue;

            tm = AttributeCounters.Instance.ElapsedTime.CurrentValue;
            tm = AttributeCounters.Instance.ElapsedTime.CurrentValue;
            AttributeCounters.Instance.ElapsedTime.Reset();
            var mv = TimeSpan.MaxValue;

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < 10000000; i++)
            {
                AttributeCounters.Instance.NumericalCounter.Increment();
                //Thread.Sleep(20);

                //var val = AttributeCounters.Instance.OpPerSec.CurrentValue;
                //if (val == -1)
                //    throw new Exception();
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalMilliseconds);


            sw = Stopwatch.StartNew();
            for (int i = 0; i < 10000000; i++)
            {
                MultyCategory.Instance[(i % 10).ToString()].ItemCnt.IncrementBy(2);
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalMilliseconds);

            for (int i = 0; i < 1000; i++)
            {
                AttributeCounters.Instance.OpPerSec.OperationFinished();
                Thread.Sleep(20);

                var val = AttributeCounters.Instance.OpPerSec.CurrentValue;
                if (val == -1)
                {
                    throw new Exception();
                }
            }

            Random rnd111 = new Random();

            //for (int i = 0; i < 100; i++)
            //{
            //    var timer = AttributeCounters.Instance.AverageTimer32.StartNew();
            //    Thread.Sleep(rnd111.Next(1000));
            //    timer.TimePoint();

            //    var val = AttributeCounters.Instance.AverageTimer32.CurrentValue;
            //    if (val == TimeSpan.MinValue)
            //        throw new Exception();
            //}

            //for (int i = 0; i < 1000; i++)
            //{
            //    AttributeCounters.Instance.AvgCount.RegisterValue(rnd111.Next(1000));
            //    Thread.Sleep(50);

            //    var val = AttributeCounters.Instance.AvgCount.CurrentValue;
            //    if (val == long.MinValue)
            //        throw new Exception();
            //}


            for (int cnt = 0; cnt < 4; cnt++)
            {
                int vvv = cnt;
                MultyCategory.Instance[vvv.ToString()].ItemCnt.Reset();
                Task.Run(() =>
                {
                    Random rnd = new Random(vvv * 10);
                    for (int i = 0; i < 500; i++)
                    {
                        MultyCategory.Instance[vvv.ToString()].ItemCnt.IncrementBy(rnd.Next(20));
                        Thread.Sleep(rnd.Next(100));
                    }
                });
            }

            Thread.Sleep(1000);

            MultyCategory.Instance["0"].Remove();


            Console.ReadLine();

            //rootFact.Dispose();

            //  CounterCreationDataCollection cntCol = new CounterCreationDataCollection();
            //  cntCol.Add(new CounterCreationData("c1", "c1", PerformanceCounterType.NumberOfItems32));
            //  cntCol.Add(new CounterCreationData("c2", "c2", PerformanceCounterType.NumberOfItems32));
            ////  PerformanceCounterCategory.Create("TestCat", "TestCat", cntCol);

            //  PerformanceCounter c1_i1 = new PerformanceCounter("TestCat", "c1", false);
            //  PerformanceCounter c1_i2 = new PerformanceCounter("TestCat", "c1", "inst2", false);

            //  c1_i1.Increment();
            //  c1_i2.Increment();

            //  Console.ReadLine();

            //  Counters.Instance.Inicialize();

            //  //Task.Factory.StartNew(Logic, TaskCreationOptions.LongRunning);
            //  Task.Factory.StartNew(AttributeCounterTest, TaskCreationOptions.LongRunning);
            //  Task.Factory.StartNew(MultyInstanceTest, TaskCreationOptions.LongRunning);

            //  Console.ReadLine();
        }