2

I have a few xaml files that I use throughout my application. I wish to write unit tests to test the performance of each xaml file, in other words, how long would it take to load/parse and render one of the windows (which use certain xaml files).

I have found XamlReader class which (I think) does exactly this. However, I know that Application.LoadComponent() (in System.Windows) also can be used for similar things. If you had to test how long does it take for an entire xaml document to parse, what would you use? XamlReader.Load() / XamlReader.Parse() or Application.LoadComponent(), or something entirely different? What would be the best course of action in this scenario?

Also, since I have little experience when it comes to performance testing programmatically, what is a good way of measuring load time, would a stopwatch class from System.Diagnostics be appropriate?

Mefhisto1
  • 2,188
  • 7
  • 34
  • 73
  • You mess up many things in your question. To create and test window you use baml (xaml+cs). `XmlReader` does only part of job, majority of which is creating objects and wiring event. You do not unit test performance, but you use profiler on existing application. `StopWatch` is perfect to measure many iteration of performance-critical peace of code. – Sinatr Jan 12 '15 at 09:29
  • Thank you for the comment. So, if I only want to test how fast xaml objects are created from xaml code, XamlReader class and its methods should be sufficient ? – Mefhisto1 Jan 12 '15 at 10:29
  • See [loose xaml](http://wpf.2000things.com/2010/10/22/102-using-xamlreader-to-load-a-loose-xaml-file/) and [baml](http://stackoverflow.com/q/634121/1997232) (as opposed). I don't really understand what performance you are measuring. It's architectural problem if your xamls are loading slowly (perhaps you put too many resources to be load or doing to much when creating instance on window). – Sinatr Jan 12 '15 at 10:57

1 Answers1

0

Try System.Diagnostics.Stopwatch. You can start this stopwatch before your code is executed and stop it when finished.

You code would look like this:

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Your code here!!!
stopwatch.Stop();
Console.WriteLine("This took " + stopwatch.ElapsedMilliseconds + " ms.");
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54