forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiming.cs
More file actions
364 lines (317 loc) · 11.6 KB
/
Copy pathTiming.cs
File metadata and controls
364 lines (317 loc) · 11.6 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
#if !NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using ServiceStack.DataAnnotations;
using ServiceStack.MiniProfiler.Data;
//using System.Web.Script.Serialization;
namespace ServiceStack.MiniProfiler
{
/// <summary>
/// An individual profiling step that can contain child steps.
/// </summary>
[Exclude(Feature.Soap)]
[DataContract]
public class Timing : IDisposable
{
/// <summary>
/// Unique identifer for this timing; set during construction.
/// </summary>
[DataMember(Order = 1, Name = "Id")]
public Guid Id { get; set; }
/// <summary>
/// Text displayed when this Timing is rendered.
/// </summary>
[DataMember(Order = 2, Name = "Name")]
public string Name { get; set; }
/// <summary>
/// How long this Timing step took in ms; includes any <see cref="Children"/> Timings' durations.
/// </summary>
[DataMember(Order = 3, Name = "DurationMilliseconds")]
public decimal? DurationMilliseconds { get; set; }
/// <summary>
/// The offset from the start of profiling.
/// </summary>
[DataMember(Order = 4, Name = "StartMilliseconds")]
public decimal StartMilliseconds { get; set; }
/// <summary>
/// All sub-steps that occur within this Timing step. Add new children through <see cref="AddChild"/>
/// </summary>
[DataMember(Order = 5, Name = "Children")]
public List<Timing> Children { get; set; }
/// <summary>
/// Stores arbitrary key/value strings on this Timing step. Add new tuples through <see cref="AddKeyValue"/>.
/// </summary>
[DataMember(Order = 6, Name = "KeyValues")]
public Dictionary<string, string> KeyValues { get; set; }
/// <summary>
/// Any queries that occurred during this Timing step.
/// </summary>
[DataMember(Order = 7, Name = "SqlTimings")]
public List<SqlTiming> SqlTimings { get; set; }
/// <summary>
/// Needed for database deserialization and JSON serialization.
/// </summary>
[DataMember(Order = 8, Name = "ParentTimingId")]
public Guid? ParentTimingId { get; set; }
private Timing _parentTiming;
/// <summary>
/// Which Timing this Timing is under - the duration that this step takes will be added to its parent's duration.
/// </summary>
/// <remarks>This will be null for the root (initial) Timing.</remarks>
//[ScriptIgnore]
public Timing ParentTiming
{
get { return _parentTiming; }
set
{
_parentTiming = value;
if (value != null && ParentTimingId != value.Id)
ParentTimingId = value.Id;
}
}
/// <summary>
/// Rebuilds all the parent timings on deserialization calls
/// </summary>
public void RebuildParentTimings()
{
if (SqlTimings != null)
{
foreach (var timing in SqlTimings)
{
timing.ParentTiming = this;
}
}
if (Children != null)
{
foreach (var child in Children)
{
child.ParentTiming = this;
child.RebuildParentTimings();
}
}
}
/// <summary>
/// Gets the elapsed milliseconds in this step without any children's durations.
/// </summary>
[DataMember(Name = "DurationWithoutChildrenMilliseconds")]
public decimal DurationWithoutChildrenMilliseconds
{
get
{
var result = DurationMilliseconds.GetValueOrDefault();
if (HasChildren)
{
foreach (var child in Children)
{
result -= child.DurationMilliseconds.GetValueOrDefault();
}
}
return Math.Round(result, 1);
}
}
/// <summary>
/// Gets the aggregate elapsed milliseconds of all SqlTimings executed in this Timing, excluding Children Timings.
/// </summary>
[DataMember(Name = "SqlTimingsDurationMilliseconds")]
public decimal SqlTimingsDurationMilliseconds
{
get { return HasSqlTimings ? Math.Round(SqlTimings.Sum(s => s.DurationMilliseconds), 1) : 0; }
}
/// <summary>
/// Returns true when this <see cref="DurationWithoutChildrenMilliseconds"/> is less than the configured
/// <see cref="MiniProfiler.Settings.TrivialDurationThresholdMilliseconds"/>, by default 2.0 ms.
/// </summary>
[DataMember(Name = "IsTrivial")]
public bool IsTrivial
{
get { return DurationWithoutChildrenMilliseconds <= MiniProfiler.Settings.TrivialDurationThresholdMilliseconds; }
}
/// <summary>
/// Reference to the containing profiler, allowing this Timing to affect the Head and get Stopwatch readings.
/// </summary>
internal MiniProfiler Profiler { get; private set; }
/// <summary>
/// Offset from parent MiniProfiler's creation that this Timing was created.
/// </summary>
private readonly long _startTicks;
/// <summary>
/// Returns true when this Timing has inner Timing steps.
/// </summary>
[DataMember(Name = "HasChildren")]
public bool HasChildren
{
get { return Children != null && Children.Count > 0; }
}
/// <summary>
/// Returns true if this Timing step collected sql execution timings.
/// </summary>
[DataMember(Name = "HasSqlTimings")]
public bool HasSqlTimings
{
get { return SqlTimings != null && SqlTimings.Count > 0; }
}
/// <summary>
/// Returns true if any <see cref="SqlTiming"/>s executed in this step are detected as duplicate statements.
/// </summary>
[DataMember(Name = "HasDuplicateSqlTimings")]
public bool HasDuplicateSqlTimings
{
get { return HasSqlTimings && SqlTimings.Any(s => s.IsDuplicate); }
}
/// <summary>
/// Returns true when this Timing is the first one created in a MiniProfiler session.
/// </summary>
[DataMember(Name = "IsRoot")]
public bool IsRoot
{
get { return ParentTiming == null; }
}
/// <summary>
/// How far away this Timing is from the Profiler's Root.
/// </summary>
[DataMember(Name = "Depth")]
public Int16 Depth
{
get
{
Int16 result = 0;
var parent = ParentTiming;
while (parent != null)
{
parent = parent.ParentTiming;
result++;
}
return result;
}
}
/// <summary>
/// How many sql data readers were executed in this Timing step. Does not include queries in any child Timings.
/// </summary>
[DataMember(Name = "ExecutedReaders")]
public int ExecutedReaders
{
get { return GetExecutedCount(ExecuteType.Reader); }
}
/// <summary>
/// How many sql scalar queries were executed in this Timing step. Does not include queries in any child Timings.
/// </summary>
[DataMember(Name = "ExecutedScalars")]
public int ExecutedScalars
{
get { return GetExecutedCount(ExecuteType.Scalar); }
}
/// <summary>
/// How many sql non-query statements were executed in this Timing step. Does not include queries in any child Timings.
/// </summary>
[DataMember(Name = "ExecutedNonQueries")]
public int ExecutedNonQueries
{
get { return GetExecutedCount(ExecuteType.NonQuery); }
}
/// <summary>
/// Creates a new Timing named 'name' in the 'profiler's session, with 'parent' as this Timing's immediate ancestor.
/// </summary>
public Timing(MiniProfiler profiler, Timing parent, string name)
{
this.Id = Guid.NewGuid();
Profiler = profiler;
Profiler.Head = this;
if (parent != null) // root will have no parent
{
parent.AddChild(this);
}
Name = name;
_startTicks = profiler.ElapsedTicks;
StartMilliseconds = profiler.GetRoundedMilliseconds(_startTicks);
}
/// <summary>
/// Obsolete - used for serialization.
/// </summary>
[Obsolete("Used for serialization")]
public Timing()
{
}
/// <summary>
/// Returns this Timing's Name.
/// </summary>
public override string ToString()
{
return Name;
}
/// <summary>
/// Returns true if Ids match.
/// </summary>
public override bool Equals(object obj)
{
return obj != null && obj is Timing && Id.Equals(((Timing)obj).Id);
}
/// <summary>
/// Returns hashcode of Id.
/// </summary>
public override int GetHashCode()
{
return Id.GetHashCode();
}
/// <summary>
/// Adds arbitrary string 'value' under 'key', allowing custom properties to be stored in this Timing step.
/// </summary>
public void AddKeyValue(string key, string value)
{
if (KeyValues == null)
KeyValues = new Dictionary<string, string>();
KeyValues[key] = value;
}
/// <summary>
/// Completes this Timing's duration and sets the MiniProfiler's Head up one level.
/// </summary>
public void Stop()
{
if (DurationMilliseconds == null)
{
DurationMilliseconds = Profiler.GetRoundedMilliseconds(Profiler.ElapsedTicks - _startTicks);
Profiler.Head = ParentTiming;
}
}
void IDisposable.Dispose()
{
Stop();
}
/// <summary>
/// Add the parameter 'timing' to this Timing's Children collection.
/// </summary>
/// <remarks>
/// Used outside this assembly for custom deserialization when creating an <see cref="IStorage"/> implementation.
/// </remarks>
public void AddChild(Timing timing)
{
if (Children == null)
Children = new List<Timing>();
Children.Add(timing);
timing.ParentTiming = this;
}
/// <summary>
/// Adds the parameter 'sqlTiming' to this Timing's SqlTimings collection.
/// </summary>
/// <param name="sqlTiming">A sql statement profiling that was executed in this Timing step.</param>
/// <remarks>
/// Used outside this assembly for custom deserialization when creating an <see cref="IStorage"/> implementation.
/// </remarks>
public void AddSqlTiming(SqlTiming sqlTiming)
{
if (SqlTimings == null)
SqlTimings = new List<SqlTiming>();
SqlTimings.Add(sqlTiming);
sqlTiming.ParentTiming = this;
}
/// <summary>
/// Returns the number of sql statements of <paramref name="type"/> that were executed in this <see cref="Timing"/>.
/// </summary>
internal int GetExecutedCount(ExecuteType type)
{
return HasSqlTimings ? SqlTimings.Count(s => s.ExecuteType == type) : 0;
}
}
}
#endif