2

I am trying to figure out what the best way would be for me to find out what portions of my application are taking the longest time to run (Largest run cost). The application is not overly complex, but I wanted to make ensure that I have all of the pieces properly tuned so that I could potentially handle a greater load.

Application: Loads / shreds xml documents and dumps the contents into a DB. The application is using Linq to XML to parse the xml, and SQL Server TVPs to pass the data down to the DB. Because I am using TVPs I have one round trip to the DB even when there are collections of data the data is not big (XML files at most 1MB).

Any suggestions on how to isolate the bottlenecks would be greatly appreciated.

As always greatly appreciate the feedback.

scarpacci
  • 8,957
  • 16
  • 79
  • 144
  • Profiling is the answer, this question probably dupes a bunch of questions. – Chris Marisic Dec 07 '11 at 17:10
  • 1
    Use a profiler, or a poor man's profiler by timing how long parts of the process take to run. – Jon Dec 07 '11 at 17:10
  • Here's a pretty close duplicate: [Find performance bottleneck in a method](http://stackoverflow.com/questions/1300659/find-performance-bottleneck-in-a-method). Voting to close as duplicate. – Daniel Pryden Dec 07 '11 at 17:13
  • Don't forget to profile the database (using SQL Server Profiler), and watch out for deadlocks! – Polyfun Dec 07 '11 at 17:40

3 Answers3

5

You may want to check out the StopWatch class. You can sprinkle it into your code like this:

// load XML Method
var stopWatch = new Stopwatch();
stopWatch.Start();
// run XML parsing code
stopWatch.Stop();

var xmlTime = stopWatch.Elapsed;


// SQL Server dump Method
var stopWatch = new Stopwatch();
stopWatch.Start();
// dump to SQL Server
stopWatch.Stop();

var sqlTime = stopWatch.Elapsed;

This is a low-tech way to take general measurments. For a simple application this is probably more efficient than a profiler, since your application only has two real points for a bottle neck. That said, learning how to use a profiler may be worth your while.

Nate
  • 30,286
  • 23
  • 113
  • 184
2

Based on the answer of Nate you could make things easy and use a small helper method for this purpose.

public static Int64 MeasureTime(Action myAction)
{
    var stopWatch = new Stopwatch();
    stopWatch.Start();
    myAction();
    stopWatch.Stop();
    return stopWatch.ElapsedMilliseconds;
}

Sample usage:

StringBuilder result;
Console.WriteLine("Parse-Xml: {0}", MeasureTime(() => result = MyAction("Test.xml")));
Felix K.
  • 6,201
  • 2
  • 38
  • 71
0

The common way to do this is to use a profiling tool. I've used RedGate ANTS for profiling C# applications, and it works well, but there are plenty of alternatives.

Community
  • 1
  • 1
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135