Sunday 18 June 2017

Performance counter

Performance counters can be used to show an overview of performance on your machine.

Lets start by creating our Performance counter; in visual studio expand your Server Explorer, expand your computer, right click on Performance Counters and Create a new Category


next your your going to make your Category, give it a name and description.




Now create counters



With your counters ready, let's create application to increment them

using System;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

namespace PerformanceCounterExample
{
    class Person
    {
        static PerformanceCounter TotalPeople = 
               new PerformanceCounter("PersonCounter", "Person Counter", false);

        string FirstName { get; set; }
        string LastName { get; set; }

        public Person(string FirstName, string LastName, int rnd)
        {
            Thread.Sleep(rnd * 100);
            this.FirstName = FirstName;
            this.LastName = LastName;
            TotalPeople.Increment();
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    }

    class Program
    {
        static string[] FirstNames = { "Mike", "Marin", "Pavel", "Davor",
            "Tomek", "Izabella", "Ivan", "Ted" };
        static string[] LastNames = { "Chooch", "Doe", "Smith", "Johnson",
            "Nelly", "Flanders", "Simpson", "Mozbie" };

        static void Main(string[] args)
        {
            Random rand = new Random();
            var people = new List<Person>();

            for (int i = 0; i < 1000; i++)
            {
                string fn = FirstNames[rand.Next(FirstNames.Length)];
                string ln = LastNames[rand.Next(LastNames.Length)];
                var p = new Person(fn, ln, rand.Next(3));
                people.Add(p);
                Console.WriteLine(p.ToString());
            }
        }
    }

}

the above code creates a thousand random people and adds them to a list, taking a slightly different random time to create each record.

next open up your performance monitor.


with that open you're going to have to add your person counters, In the above screen I've already added mine, but your going to have to hit the Green Plus sign



with the Add Counters screen open, you're going to add the counters you created earlier. next at the bottom of your graph screen make sure you have the counters you created checked off.


with that done run your program and watch your graph