-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfilerWindowViewModel.cs
More file actions
366 lines (307 loc) · 11.7 KB
/
ProfilerWindowViewModel.cs
File metadata and controls
366 lines (307 loc) · 11.7 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using SkyLineSQL.Utility;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Input;
namespace SkyLineSQL
{
public class ProfileEventModel
{
public string TextData {get; set; }
public string LoginName {get; set;}
public int CPU {get; set;}
public long Reads {get; set;}
public long Writes {get; set;}
public long Duration {get; set;}
public int SPID {get; set;}
public DateTime StartTime {get; set;}
public DateTime EndTime {get; set;}
public string DatabaseName {get; set;}
public string ObjectName {get; set;}
public string ApplicationName {get; set;}
public string HostName { get; set; }
}
public class ProfilerWindowViewModel : INotifyPropertyChanged
{
private DataManager DM;
public ObservableCollection<ProfileEventModel> Events { get; set; }
private ProfileEventModel selectedEvent;
public ProfileEventModel SelectedEvent
{
get { return selectedEvent; }
set { selectedEvent = value; OnPropertyChanged(); }
}
public ICommand StartProfilerCommand { get; }
public ICommand PauseProfilerCommand { get; }
public ICommand StopProfilerCommand { get; }
public ICommand ClearCommand { get; }
public ICommand SearchCommand { get; }
public ProfilerWindowViewModel()
{
Events = new();
StartProfilerCommand = new RelayCommand(ExecuteStartProfilerCommand, CanExecuteStartProfilerCommand);
PauseProfilerCommand = new RelayCommand(ExecutePauseProfilerCommand, CanExecutePauseProfilerCommand);
StopProfilerCommand = new RelayCommand(ExecuteStopProfilerCommand, CanExecuteStopProfilerCommand);
ClearCommand = new RelayCommand(ExecuteClearCommand, CanExecuteClearCommand);
SearchCommand = new RelayCommand(ExecuteSearchCommand);
//ProfileEventModel eventModel = new()
//{
// TextData = "TextData",
// LoginName = "LoginName",
// CPU = 0,
// Reads =12,
// Writes = 15,
// Duration = 150,
// SPID = 52,
// StartTime = DateTime.Now,
// EndTime = DateTime.Now.AddSeconds(15),
// DatabaseName = "BalaniRebuild",
// ObjectName = "usp_Client_Get",
// ApplicationName = "SkyLineSQL",
// HostName = "Server"
//};
//Events.Add(eventModel);
//Events.Add(eventModel);
}
public void SetDataManager(DataManager DM)
{
this.DM = DM;
}
private bool CanExecuteStartProfilerCommand(object param)
{
return (m_ProfilingState == ProfilingStateEnum.psPaused || m_ProfilingState == ProfilingStateEnum.psStopped);
}
private void ExecuteStartProfilerCommand(object param)
{
StartProfiling();
}
private bool CanExecutePauseProfilerCommand(object param)
{
return (m_ProfilingState == ProfilingStateEnum.psProfiling);
}
private void ExecutePauseProfilerCommand(object param)
{
PauseProfiling();
}
private bool CanExecuteStopProfilerCommand(object param)
{
return (m_ProfilingState == ProfilingStateEnum.psProfiling);
}
private void ExecuteStopProfilerCommand(object param)
{
StopProfiling();
}
IDbConnection m_Conn;
RawTraceReader m_Rdr;
Thread m_Thr;
bool m_NeedStop = true;
private ProfilingStateEnum m_ProfilingState = ProfilingStateEnum.psStopped;
private Exception m_profilerexception;
private readonly ProfilerEvent m_EventStarted = new ProfilerEvent();
private readonly ProfilerEvent m_EventStopped = new ProfilerEvent();
private readonly ProfilerEvent m_EventPaused = new ProfilerEvent();
private void StartProfiling()
{
if (m_ProfilingState == ProfilingStateEnum.psPaused)
{
ResumeProfiling();
return;
}
if (m_Conn != null && m_Conn.State == ConnectionState.Open)
{
m_Conn.Close();
}
m_Conn = DM.GetProfilerConnection();
m_Conn.Open();
m_Rdr = new RawTraceReader(m_Conn);
m_Rdr.CreateTrace();
m_Rdr.SetEvent(ProfilerEvents.TSQL.SQLBatchCompleted,
ProfilerEventColumns.TextData,
ProfilerEventColumns.LoginName,
ProfilerEventColumns.CPU,
ProfilerEventColumns.Reads,
ProfilerEventColumns.Writes,
ProfilerEventColumns.Duration,
ProfilerEventColumns.SPID,
ProfilerEventColumns.StartTime,
ProfilerEventColumns.EndTime,
ProfilerEventColumns.DatabaseName,
ProfilerEventColumns.ApplicationName,
ProfilerEventColumns.HostName
);
m_Rdr.SetEvent(ProfilerEvents.StoredProcedures.RPCCompleted,
ProfilerEventColumns.TextData, ProfilerEventColumns.LoginName,
ProfilerEventColumns.CPU, ProfilerEventColumns.Reads,
ProfilerEventColumns.Writes, ProfilerEventColumns.Duration,
ProfilerEventColumns.SPID
, ProfilerEventColumns.StartTime, ProfilerEventColumns.EndTime
, ProfilerEventColumns.DatabaseName
, ProfilerEventColumns.ObjectName
, ProfilerEventColumns.ApplicationName
, ProfilerEventColumns.HostName
);
m_Rdr.SetFilter(ProfilerEventColumns.LoginName, LogicalOperators.AND, ComparisonOperators.Like, "developer_user");
m_Rdr.SetFilter(ProfilerEventColumns.DatabaseName, LogicalOperators.AND, ComparisonOperators.Like, "BalaniRebuild");
m_Rdr.SetFilter(ProfilerEventColumns.ApplicationName, LogicalOperators.AND, ComparisonOperators.NotLike, "SkyLineSQL");
StartProfilerThread();
}
private void PauseProfiling()
{
using (var cn = DM.GetProfilerConnection())
{
cn.Open();
m_Rdr.StopTrace(cn);
cn.Close();
}
m_ProfilingState = ProfilingStateEnum.psPaused;
NewEventArrived(m_EventPaused, true);
}
private void ResumeProfiling()
{
StartProfilerThread();
}
private void StopProfiling()
{
using (var cn = DM.GetProfilerConnection())
{
cn.Open();
m_Rdr.StopTrace(cn);
m_Rdr.CloseTrace(cn);
cn.Close();
}
m_NeedStop = true;
if (m_Thr.IsAlive)
{
try
{
m_Thr.Abort();
}
catch (Exception)
{
}
}
m_Thr.Join();
m_ProfilingState = ProfilingStateEnum.psStopped;
NewEventArrived(m_EventStopped, true);
}
private void StartProfilerThread()
{
m_Rdr.Close();
m_Rdr.StartTrace();
m_Thr = new Thread(ProfilerThread) { IsBackground = true, Priority = ThreadPriority.Lowest };
m_NeedStop = false;
m_ProfilingState = ProfilingStateEnum.psProfiling;
NewEventArrived(m_EventStarted, true);
m_Thr.Start();
}
private void NewEventArrived(ProfilerEvent evt, bool last)
{
ProfileEventModel eventModel = new() {
TextData = GetEventCaption(evt),
LoginName = evt.LoginName,
CPU = evt.CPU,
Reads = evt.Reads,
Writes = evt.Writes,
Duration = evt.Duration,
SPID = evt.SPID,
StartTime = evt.StartTime,
EndTime = evt.EndTime,
DatabaseName = evt.DatabaseName,
ObjectName = evt.ObjectName,
ApplicationName = evt.ApplicationName,
HostName = evt.HostName
};
Events.Add(eventModel);
}
private string GetEventCaption(ProfilerEvent evt)
{
if (evt == m_EventStarted)
{
return "Trace started";
}
if (evt == m_EventPaused)
{
return "Trace paused";
}
if (evt == m_EventStopped)
{
return "Trace stopped";
}
return evt.TextData;
}
private void ProfilerThread(object state)
{
try
{
while (!m_NeedStop && m_Rdr.TraceIsActive)
{
var evt = m_Rdr.Next();
if (evt != null)
{
lock (this)
{
App.Current.Dispatcher.Invoke((System.Action)delegate
{
ProfileEventModel eventModel = new()
{
TextData = GetEventCaption(evt),
LoginName = evt.LoginName,
CPU = evt.CPU,
Reads = evt.Reads,
Writes = evt.Writes,
Duration = evt.Duration,
SPID = evt.SPID,
StartTime = evt.StartTime,
EndTime = evt.EndTime,
DatabaseName = evt.DatabaseName,
ObjectName = evt.ObjectName,
ApplicationName = evt.ApplicationName,
HostName = evt.HostName
};
Events.Add(eventModel);
});
}
}
}
}
catch (Exception e)
{
lock (this)
{
if (!m_NeedStop && m_Rdr.TraceIsActive)
{
m_profilerexception = e;
}
}
}
}
private bool CanExecuteClearCommand(object param)
{
return (m_ProfilingState == ProfilingStateEnum.psStopped);
}
private void ExecuteClearCommand(object param)
{
Events.Clear();
}
private void ExecuteSearchCommand(object param)
{
// TODO
}
#region Notify
/// <inheritdoc cref="INotifyPropertyChanged.PropertyChanged"/>
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
ArgumentNullException.ThrowIfNull(e);
PropertyChanged?.Invoke(this, e);
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}