-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProcessTimer.cs
151 lines (133 loc) · 3.89 KB
/
ProcessTimer.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
using System;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Inversion.Process {
/// <summary>
/// Represents a simplistic timer as a start and stop time
/// as a pair of date time objects.
/// </summary>
/// <remarks>
/// This is **NOT** suitable for adult timings, but introduces no
/// overhead and is suitable for applications developers to be
/// able to monitor basic timings of features to know if features
/// are costing more time that expected or are going into distress.
/// </remarks>
public class ProcessTimer : IData {
private DateTime _start;
private DateTime _stop;
/// <summary>
/// Determines if this timer has been stopped or not.
/// </summary>
public bool HasStopped {
get { return _stop != default(DateTime); }
}
/// <summary>
/// The start time of the timer.
/// </summary>
public DateTime Start {
get { return _start; }
}
/// <summary>
/// The stop time of the timer.
/// </summary>
public DateTime Stop {
get { return _stop; }
}
/// <summary>
/// The time that has elapsed between
/// the start and the stop times.
/// </summary>
public TimeSpan Duration {
get { return (this.HasStopped) ? _stop - _start : DateTime.Now - _start; }
}
/// <summary>
/// Provides an abstract representation
/// of the objects data expressed as a JSON object.
/// </summary>
/// <remarks>
/// For this type the json object is created each time
/// it is accessed.
/// </remarks>
public JObject Data {
get { return this.ToJsonObject(); }
}
/// <summary>
/// Instances a new process timer, defaulting its
/// start time as now.
/// </summary>
public ProcessTimer() : this(DateTime.Now) { }
/// <summary>
/// Instantiates a new process timer with
/// the start time specified.
/// </summary>
/// <param name="start">The start time of the new timer.</param>
public ProcessTimer(DateTime start) {
_start = start;
_stop = default(DateTime);
}
/// <summary>
/// Instantiates a new process timer as a copy of
/// a provied timer.
/// </summary>
/// <param name="timer"></param>
public ProcessTimer(ProcessTimer timer) {
_start = timer.Start;
_stop = timer.Stop;
}
/// <summary>
/// Clones this timer as a copy.
/// </summary>
/// <returns>Returns a new process timer.</returns>
object ICloneable.Clone() {
return new ProcessTimer(this);
}
/// <summary>
/// Clones this timer as a copy.
/// </summary>
/// <returns>Returns a new process timer.</returns>
public ProcessTimer Clone() {
return new ProcessTimer(this);
}
/// <summary>
/// Sets the start time of this timer to now.
/// </summary>
/// <returns>Returns this timer.</returns>
public ProcessTimer Begin() {
_start = DateTime.Now;
return this;
}
/// <summary>
/// Sets the stop time of this timer to now.
/// </summary>
/// <returns>Returns this process timer.</returns>
public ProcessTimer End() {
_stop = DateTime.Now;
return this;
}
/// <summary>
/// Produces an xml representation of the model.
/// </summary>
/// <param name="writer">The writer to used to write the xml to. </param>
public void ToXml(XmlWriter writer) {
writer.WriteStartElement("timer");
writer.WriteAttributeString("start", _start.ToString());
writer.WriteAttributeString("duration", this.Duration.Milliseconds.ToString());
writer.WriteEndElement();
}
/// <summary>
/// Produces a json respresentation of the model.
/// </summary>
/// <param name="writer">The writer to use for producing json.</param>
public void ToJson(JsonWriter writer) {
writer.WriteStartObject();
writer.WritePropertyName("_type");
writer.WriteValue("timer");
writer.WritePropertyName("start");
writer.WriteValue(_start.ToString());
writer.WritePropertyName("duration");
writer.WriteValue(this.Duration.Milliseconds.ToString());
writer.WriteEndObject();
}
}
}