1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
namespace TimeMeasuring
{
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
// MethodStopwatch is a easy to use static class for measuring the time
// of a method (i.e. by simple calling "Start" at the begin of a method and "Stop" or "StopAndPublish" at the end
// by codeteq, 2011
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
public static class MethodStopwatch
{
#region private classes
private class _MethodStopwatch : Stopwatch
{
#region consts, members, properties
public System.Reflection.MethodBase Method
{
get
{
try
{
StackFrame sf = new StackFrame(4);
return sf.GetMethod();
}
catch { return null; }
}
}
#endregion
#region .ctor
public _MethodStopwatch(bool autostart = true)
{
if (autostart)
this.Start();
}
#endregion
#region methods
public string StopAndPublish(Stream stream, string suffix = null)
{
this.Stop();
return Publish(stream, suffix);
}
public string Publish(Stream stream, string suffix)
{
string output = String.Format("Method: \"{0}\" lasted: {1}ms {2}", (this.Method != null) ? this.Method.Name : "Couldn not resolve method", this.ElapsedMilliseconds.ToString(), suffix);
if (stream == null)
{
if (Debugger.IsAttached)
Debug.WriteLine(output);
}
else if(stream.CanWrite)
{
using (StreamWriter writer = new StreamWriter(stream))
writer.WriteLine(output);
}
return output;
}
#endregion
}
#endregion
#region consts, members, properties
private static Stack<_MethodStopwatch> stopwatches;
#endregion
#region methods
public static void Start()
{
(stopwatches = (stopwatches = new Stack<_MethodStopwatch>())).Push(new _MethodStopwatch());
}
public static void Stop()
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().Stop();
}
public static void Publish(string suffix = null)
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().Publish(null, suffix);
}
public static void StopAndPublish(string suffix = null)
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().StopAndPublish(null, suffix);
}
public static void PublishToStream(Stream stream, string suffix = null)
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().Publish(stream, suffix);
}
public static void StopAndPublishToStream(Stream stream, string suffix = null)
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().StopAndPublish(stream, suffix);
}
public static void Restart()
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().Restart();
}
public static void Reset()
{
if (stopwatches != null && stopwatches.Count > 0)
stopwatches.Pop().Reset();
}
#endregion
}
}
|