/
SqlTiming.cs
252 lines (217 loc) · 8.06 KB
/
SqlTiming.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
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
#if !NETCORE
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using ServiceStack.DataAnnotations;
using ServiceStack.MiniProfiler.Data;
using ServiceStack.MiniProfiler.Helpers;
//using System.Web.Script.Serialization;
namespace ServiceStack.MiniProfiler
{
/// <summary>
/// Profiles a single sql execution.
/// </summary>
[ExcludeMetadata]
[DataContract]
public class SqlTiming
{
/// <summary>
/// Unique identifier for this SqlTiming.
/// </summary>
//[ScriptIgnore]
public Guid Id { get; set; }
/// <summary>
/// Category of sql statement executed.
/// </summary>
[DataMember(Order = 1, Name = "ExecuteType")]
public ExecuteType ExecuteType { get; set; }
/// <summary>
/// The sql that was executed.
/// </summary>
//[ScriptIgnore]
[DataMember(Order = 2, Name = "CommandString")]
public string CommandString { get; set; }
/// <summary>
/// The command string with special formatting applied based on MiniProfiler.Settings.SqlFormatter
/// </summary>
[DataMember(Order = 3, Name = "FormattedCommandString")]
public string FormattedCommandString
{
get
{
if (MiniProfiler.Settings.SqlFormatter == null) return CommandString;
return MiniProfiler.Settings.SqlFormatter.FormatSql(this);
}
}
/// <summary>
/// Roughly where in the calling code that this sql was executed.
/// </summary>
[DataMember(Order = 4, Name = "StackTraceSnippet")]
public string StackTraceSnippet { get; set; }
/// <summary>
/// Offset from main MiniProfiler start that this sql began.
/// </summary>
[DataMember(Order = 5, Name = "StartMilliseconds")]
public decimal StartMilliseconds { get; set; }
/// <summary>
/// How long this sql statement took to execute.
/// </summary>
[DataMember(Order = 6, Name = "DurationMilliseconds")]
public decimal DurationMilliseconds { get; set; }
/// <summary>
/// When executing readers, how long it took to come back initially from the database,
/// before all records are fetched and reader is closed.
/// </summary>
[DataMember(Order = 7, Name = "FirstFetchDurationMilliseconds")]
public decimal FirstFetchDurationMilliseconds { get; set; }
/// <summary>
/// Stores any parameter names and values used by the profiled DbCommand.
/// </summary>
[DataMember(Order = 8, Name = "Parameters")]
public List<SqlTimingParameter> Parameters { get; set; }
/// <summary>
/// Id of the Timing this statement was executed in.
/// </summary>
/// <remarks>
/// Needed for database deserialization.
/// </remarks>
[DataMember(Order = 9, Name = "ParentTimingId")]
public Guid? ParentTimingId { get; set; }
private Timing _parentTiming;
/// <summary>
/// The Timing step that this sql execution occurred in.
/// </summary>
//[ScriptIgnore]
public Timing ParentTiming
{
get { return _parentTiming; }
set
{
_parentTiming = value;
if (value != null && ParentTimingId != value.Id)
ParentTimingId = value.Id;
}
}
/// <summary>
/// True when other identical sql statements have been executed during this MiniProfiler session.
/// </summary>
[DataMember(Order = 10, Name = "IsDuplicate")]
public bool IsDuplicate { get; set; }
private long _startTicks;
private MiniProfiler _profiler;
/// <summary>
/// Creates a new SqlTiming to profile 'command'.
/// </summary>
public SqlTiming(DbCommand command, ExecuteType type, MiniProfiler profiler)
{
Id = Guid.NewGuid();
CommandString = AddSpacesToParameters(command.CommandText);
Parameters = GetCommandParameters(command);
ExecuteType = type;
if (!MiniProfiler.Settings.ExcludeStackTraceSnippetFromSqlTimings)
StackTraceSnippet = ServiceStack.MiniProfiler.Helpers.StackTraceSnippet.Get();
_profiler = profiler;
if (_profiler != null)
{
_profiler.AddSqlTiming(this);
_startTicks = _profiler.ElapsedTicks;
StartMilliseconds = _profiler.GetRoundedMilliseconds(_startTicks);
}
}
/// <summary>
/// Obsolete - used for serialization.
/// </summary>
[Obsolete("Used for serialization")]
public SqlTiming()
{
}
/// <summary>
/// Returns a snippet of the sql command and the duration.
/// </summary>
public override string ToString()
{
return CommandString.Truncate(30) + " (" + DurationMilliseconds + " ms)";
}
/// <summary>
/// Returns true if Ids match.
/// </summary>
public override bool Equals(object obj)
{
return obj != null && obj is SqlTiming && Id.Equals(((SqlTiming)obj).Id);
}
/// <summary>
/// Returns hashcode of Id.
/// </summary>
public override int GetHashCode()
{
return Id.GetHashCode();
}
/// <summary>
/// Called when command execution is finished to determine this SqlTiming's duration.
/// </summary>
public void ExecutionComplete(bool isReader)
{
if (isReader)
{
FirstFetchDurationMilliseconds = GetDurationMilliseconds();
}
else
{
DurationMilliseconds = GetDurationMilliseconds();
}
}
/// <summary>
/// Called when database reader is closed, ending profiling for <see cref="Data.ExecuteType.Reader"/> SqlTimings.
/// </summary>
public void ReaderFetchComplete()
{
DurationMilliseconds = GetDurationMilliseconds();
}
private decimal GetDurationMilliseconds()
{
return _profiler.GetRoundedMilliseconds(_profiler.ElapsedTicks - _startTicks);
}
/// <summary>
/// To help with display, put some space around sammiched commas
/// </summary>
private string AddSpacesToParameters(string commandString)
{
return Regex.Replace(commandString, @",([^\s])", ", $1");
}
private List<SqlTimingParameter> GetCommandParameters(DbCommand command)
{
if (command.Parameters == null || command.Parameters.Count == 0) return null;
var result = new List<SqlTimingParameter>();
foreach (DbParameter dbParameter in command.Parameters)
{
if (!dbParameter.ParameterName.IsNullOrWhiteSpace())
{
var formattedParameterValue = GetFormattedParameterValue(dbParameter);
result.Add(new SqlTimingParameter
{
ParentSqlTimingId = Id,
Name = dbParameter.ParameterName.Trim(),
Value = formattedParameterValue,
DbType = dbParameter.DbType.ToString(),
Size = dbParameter.Size
});
}
}
return result;
}
private static string GetFormattedParameterValue(DbParameter dbParameter)
{
object rawValue = dbParameter.Value;
if (rawValue == null || rawValue == DBNull.Value)
{
return null;
}
return rawValue is DateTime
? ((DateTime)rawValue).ToString("s", System.Globalization.CultureInfo.InvariantCulture)
: rawValue.ToString();
}
}
}
#endif