The controller makes a few calls to the model, then it returns some data to the view. The view actually, sadly, (it's not my fault), contains a ton of inline queries and more calls to the Model, yeah I know. Anyways I am tasked with optimizing this really slow page and I am trying to figure out how I can tell which thing is taking the most time. I was just going to put a timer at the start and the end of each 'thing' that the page does and output them to a log with the line number or something. But not sure what the most accurate way开发者_JAVA百科 to do this is.
//in controller
StartTimer();
var something = model.something.getsomething(someID);
StopTimerAndLog(3); //line number
<!-- in view -->
<%StartTimer();
var something = model.somethingelse.getanotherthing(someotherID);
StopTimerAndLog(2);%>
so on and so forth...
Then the question remains about what timing mechanism to use, I'm sure there must be a question about this already. But I don't know if my situation makes anything unique or not... any ideas?
If you really want to measure like this, I would use the StopWatch class:
var watch = new StopWatch();
watch.Start();
var something = model.something.getSomething(someID);
watch.Stop();
var time = watch.Elapsed;
If you want something really detailed and without writing any extra code...I would suggest the use of a Profiler. It will give you details about exactly what is taking so long and why. My personal favorite is RedGate's ANTS Performance Profiler.
精彩评论