-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinotaurCommand.cs
186 lines (170 loc) · 5.04 KB
/
MinotaurCommand.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace Minotaur
{
public class MinotaurCommand : ICommand
{
private Action _execute;
public MinotaurCommand(Action execute)
{
if (execute == null)
throw new ArgumentNullException("Minotaur did not detect a method to execute.");
_stopwatch = new Stopwatch();
_execute = execute;
_outputFormat = OutputFormat.Off;
}
public MinotaurCommand(Action execute, OutputFormat outputFormat)
{
if (execute == null)
throw new ArgumentNullException("Minotaur did not detect a method to execute.");
_stopwatch = new Stopwatch();
_execute = execute;
_outputFormat = outputFormat;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
try
{
if (_stopwatch == null)
_stopwatch = new Stopwatch();
_parentClass = _execute.Method.DeclaringType.Name;
_methodName = _execute.Method.Name;
_startTime = DateTime.Now;
_stopwatch.Start();
_execute();
_stopwatch.Stop();
}
catch
{
}
finally
{
switch(_outputFormat)
{
case OutputFormat.Console:
WriteToOutputWindow();
break;
case OutputFormat.File:
LogPerformanceSnapshot();
break;
case OutputFormat.Both:
LogPerformanceSnapshot();
WriteToOutputWindow();
break;
default:
break;
}
Stopwatch.Reset();
}
}
private void LogPerformanceSnapshot()
{
try
{
using (CSVPrinter printer = new CSVPrinter())
{
printer.Print(PerformanceSnapshot);
}
}
catch
{
}
}
private void WriteToOutputWindow()
{
Console.WriteLine(string.Empty);
Console.WriteLine("-- Minotaur Performance Log --");
Console.WriteLine(string.Format("Parent Class: {0}", ParentClass));
Console.WriteLine(string.Format("Interaction: {0}", MethodName));
Console.WriteLine(string.Format("Start Time: {0}", StartTime.ToShortTimeString()));
Console.WriteLine(string.Format("Elapsed Time (h): {0}", Hours));
Console.WriteLine(string.Format("Elapsed Time (m): {0}", Minutes));
Console.WriteLine(string.Format("Elapsed Time (s): {0}", Seconds));
}
public DateTime StartTime
{
get
{
return _startTime;
}
}
public string ParentClass
{
get
{
return _parentClass;
}
}
public string MethodName
{
get
{
return _methodName;
}
}
public int Hours
{
get
{
if (Stopwatch == null)
return 0;
else
return Stopwatch.Elapsed.Hours;
}
}
public int Minutes
{
get
{
if (Stopwatch == null)
return 0;
else
return Stopwatch.Elapsed.Minutes;
}
}
public double Seconds
{
get
{
if (Stopwatch == null)
return 0;
else
return Stopwatch.Elapsed.TotalSeconds;
}
}
public Stopwatch Stopwatch
{
get
{
return _stopwatch;
}
}
public PerformanceSnapshot PerformanceSnapshot
{
get
{
return new PerformanceSnapshot()
{
StartDate = StartTime.ToShortDateString(),
StartTime = StartTime.ToShortTimeString(),
ParentClass = ParentClass,
Interaction = MethodName,
Hours = Stopwatch.Elapsed.Hours,
Minutes = Stopwatch.Elapsed.Minutes,
Seconds = Stopwatch.Elapsed.TotalSeconds,
};
}
}
private Stopwatch _stopwatch;
private DateTime _startTime;
private string _parentClass;
private string _methodName;
private OutputFormat _outputFormat;
}
}