Monitoring batch requests per second on SQL Server through WMI
I need to programmatically (.NET 3.5, C#) monitor a SQL Server 2008 machine through WMI. I want to measure the number of batch requests per second that the server is receiving; this is what the Windows 7 Performance Monitor tool will show you under the SQL Server:SQL Statistics
category, Batch Requests/sec
counter. If I monitor the server using this tool, I observe a baseline of zero when the server is idle, going up to 100 or 200 when I hit it with some queries from my application.
Now, when I try to use the WMI classes in .NET to obtain the same data, I only read zeros, no matter how much I hit the server with queries. From what I have read on MSDN, some performance counters are not supposed to be retrieved instantaneously, but a sampling process is suggested. I开发者_JS百科 could not find any information on which counters fall into this category, i.e. should I use sampling to measure batch requests per second? Also, what kind of sampling? I imagine that the Windows Performance Monitor tool is doing some calculations behind the scenes to show the graphs that it shows; I would like to obtain similar results. Anyone has gone through a similar experience and solved it successfully? Thanks.
Yes, that's accurate. It is up to your code to do the same thing that Perfmon does: query the performance counter once a second to get a meaningful value. You'll need a timer or (ugh) call Sleep(). Using the PerformanceCounter class is the better way to do this btw, WMI is not cheap. That also gives you access to the PerformanceCounter property which tells you what kind of counter it is. Doesn't really matter, you know you need the timer anyway.
精彩评论