Freigeben über


Profiling: Sampling doesn't report my functions wait time.

Working on performance of VSSConverter. I am using the new F1 profiler being shipped with VS 2005. I have a simple code sample like this:

  using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace perf_test{class Program{static bool exit = false;static object lockObject = 3;static void Main(string[] args){Thread threadWait = new Thread(new ThreadStart(WaitMethod));Thread threadProcess = new Thread(new ThreadStart(ProcessMethod));threadWait.Start();threadProcess.Start();threadWait.Join();threadProcess.Join();}static void WaitMethod(){lock (lockObject){Console.WriteLine("Starting wait");Monitor.Wait(lockObject, 5000);exit = true;Monitor.Wait(lockObject, 5000);Console.WriteLine("Ending wait");}}static void ProcessMethod(){while (!exit){Console.WriteLine("Processing");}}}} 

Which function do you think will take more time? Obviously WaitMethod(). Try a sampling profiling on that. Here are the results I got:

 

(the image is not showing properly ineed to figure out why. but it says 80% time in ProcessMethod() and 3% time in WaitMethod())

What do i get with Instrumentation profiling:

(It says 5003 ms spent in ProcessMethod() and 10028 ms spent in WaitMethod())

heres my explaination to what happened.

looking very simplistically sampling is just a statistical data about what your processor was busy doing most of the time. what is done is at regular intervals a snapshot of what is processing is taken. so it misses out if some thread is in wait or sleep.

Instrumentaion modifies your binary and adds code at each function entry and exit. this way yoy get the exact time that was taken by a peice of code to execute including the time spent in wait.

this becomes very evident if you are profiling an application that makes lots of web calls. All such calls go for wait in WebRequest.GetResponse() method and sampling doesn't report the proper time spent in such calls.