Tuesday, December 29, 2009

Measure Execution Time

If there is a instrument that we can measure or monitor functions' excution time, then we got a clear insight of an application, it is like a dashboard on a vehicle, you know what's going on in the application.


The following is what I wrote just for that.

The idea

// Read the initial time.
DateTime startTime = DateTime.Now;

/* Do something that takes up some time. For example sleep for 1.7 seconds. */
Thread.Sleep(1700);

// Read the end time.
DateTime stopTime = DateTime.Now;
Console.WriteLine("Measure End:" + stopTime);




Wrap it up:
Measure measure = new Measure();
// the measureId parameter is optional, but it is useful for identifying the

measure
measure.start(measureId);
/*
the body to be measured
*/
measure.end(measureId);





class Measure
{
private static readonly string allMeasureId = "DefaultMeasure";
private static Hashtable map = new Hashtable();
public static void Clear(){
map.Reset();
}

public static void Start(){
if(map.Exist(Measure.allMeasureId )){
throw new Exception("there already an instance with default id.");
}else{
Measure m = new Measure();
map.add(Measure.allMeasureId, m);
m.Start();
}
}

public static void Stop(){
if(map[Measure.allMeasureId] != null){
Measure m = map[Measure.allMeasureId] as Measure;
m.Stop();
map.remove(map[Measure.allMeasureId]);
m.dispose();
}
}

public static void Start(string measureKey){
if(map.Exist(measureKey)){
throw new Exception("there already an instance with given id.");
}else{
Measure m = new Measure();
map.add(measureKey, m);
m.Start();
}
}

public static void Stop(string measureKey){
if(map[measureKey] != null){
Measure m = map[measureKey] as Measure;
m.Stop();
map.remove(map[measureKey]);
m.dispose();
}else{
throw new Exception("there is no measure instance having the

id.");
}
}


private string _measureId = Messure.allMeasureId;
private datetime _start;
private datetime _stop;

public Measure(string measureId){
this._measureId = measureId;
}
public Measure(){

}

public void Start(){
_start = DateTime.Now;
}

public void Stop(){
_stop = DateTime.Now;
this.LogIt();

}

private void LogIt(){
Console.WriteLine("Measure Id:" + _measureId);
Console.WriteLine("Measure Start:" + _start);
Console.WriteLine("Measure End:" + _stop);

TimeSpan duration = stopTime - startTime;
Console.WriteLine("hours:" + duration.Hours);
Console.WriteLine("minutes:" + duration.Minutes);
Console.WriteLine("seconds:" + duration.Seconds);
Console.WriteLine("milliseconds:" + duration.Milliseconds);

/*
Measure Start: 4/17/2005 4:17:28 PM
Measure End: 4/17/2005 4:17:30 PM
hours:0
minutes:0
seconds:1
milliseconds:712
*/
TimeSpan duration = stopTime - startTime;
Console.WriteLine("hours:" + duration.TotalHours);
Console.WriteLine("minutes:" + duration.TotalMinutes);
Console.WriteLine("seconds:" + duration.TotalSeconds);
Console.WriteLine("milliseconds:" + duration.TotalMilliseconds);

/*
Measure Start: 4/17/2005 4:23:27 PM
Measure End: 4/17/2005 4:23:29 PM
hours:0.000475684
minutes:0.02854104
seconds:1.7124624
milliseconds:1712.4624
*/

}
}


Think Broader

using(Measure m = new Measure(measureId)){
/*
the measure body
*/
}

No comments: