views:

32

answers:

2

What exactly are performance counters and how do they work? Are they specific to Windows or they are an operating system level concept and available on Linux etc as well?

How can I use them in my .NET application to measure execution times of various parts of my application?

I googled but surprisingly didn't find any great references.

A: 

Performance counters, as a concept, are measurable, reportable metrics that the kernel, or an application, can report to the operating system. Thinks like CPU load, memory usage, network bandwidth, etc are examples of performance counters. It's not just hardware-based; a webserver can report how many sessions it's handling, or a DB server can report the number of connections and queries/sec. In Windows at least, you can create your own for your application; if you have a server application that processes input files into a database, you could have it report its throughput in files/sec or KB/sec. You can report server uptime in consecutive hours or as a percentage of the time since it was installed.

How you go about accessing or creating performance counters will vary pretty widely depending on your development environment (language, framework, libraries) and target OS, but I can't think of a modern kernel that can't at least report hardware performance numbers.

KeithS
A: 

Performance counters in .Net are specific to Windows. I don't think using performance counters to measure execution times is a good way to go, it would be making things more complicated than they have to be I suspect. Visual Studio contains a performance tool, if you go to View - OtherWindows - Performance Explorer menu the performance explorer window comes up and you can click on the Launch Performance Wizard button. Another possibility is to use the Stopwatch class which will use the high resolution timer if your computer has one:

        Int64 nanoSecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency;
        Stopwatch timer = Stopwatch.StartNew();
        // execute the code that you want to time
        timer.Stop();
        Int64 nanoSec = timer.ElapsedTicks / nanoSecPerTick;
        // or if you don't need such fine grained timing:
        Int64 milliSec = timer.ElapsedMilliseconds;
        Console.Write("Elapsed time: " + nanoSec + "ns");
Steve Ellinger
The performance tool you mentioned is only in the Premium or Ultimate editions.
adrianbanks