I have a method in a web service I am writing that compiles a list of Performance Counter Categories,Instances, and Counters on that System. It builds a xml document and returns that. The first time the method is called after being published to the server, it takes about 17 seconds to get a result. Each time it is called after that, it takes 38 seconds. If I republish, the first time after that takes 17 seconds and then I am back at 38 seconds per a call.
[WebMethod(Description="Returns the CounterList in XML Format")]
public XmlDocument GetCounters()
{
XmlDocument CounterList = new XmlDocument();
XmlElement root = CounterList.CreateElement("CounterList");
PerformanceCounterCategory[] pcc = PerformanceCounterCategory.GetCategories();
foreach (PerformanceCounterCategory cat in pcc)
{
XmlElement Category = CounterList.CreateElement("Category");
XmlAttribute CatName = CounterList.CreateAttribute("Name");
CatName.Value = cat.CategoryName;
Category.Attributes.Append(CatName);
String[] instances = cat.GetInstanceNames();
PerformanceCounter[] pc;
if (instances.Length > 0)
{
pc = cat.GetCounters(instances[0]);
XmlElement Instances = CounterList.CreateElement("Instances");
foreach (String instance in instances)
{
XmlElement Instance = CounterList.CreateElement("Instance");
Instance.AppendChild(CounterList.CreateTextNode(instance));
Instances.AppendChild(Instance);
}
Category.AppendChild(Instances);
}
else
pc = cat.GetCounters();
XmlElement Counters = CounterList.CreateElement("Counters");
foreach (PerformanceCounter counter in pc)
{
XmlElement Counter = CounterList.CreateElement("Counter");
Counter.AppendChild(CounterList.CreateTextNode(counter.CounterName));
Counters.AppendChild(Counter);
}
Category.AppendChild(Counters);
root.AppendChild(Category);
}
CounterList.AppendChild(root);
return CounterList;
}
I would comment out this line:
and see if the symptom persists.
Note, I tried the code as a regular function and the first one took about 4 seconds, subsequent executions took about .2 seconds.