-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathSchedulingStopwatch.cs
More file actions
127 lines (114 loc) · 4.49 KB
/
SchedulingStopwatch.cs
File metadata and controls
127 lines (114 loc) · 4.49 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Query.Core.Metrics
{
using Microsoft.Azure.Documents;
/// <summary>
/// This class keeps track of scheduling metrics for a single process using a stopwatch interface.
/// Internally this class is composed of Stopwatches keeping track of scheduling metrics.
/// The main metrics are turnaround, response, run, and wait time.
/// However this class only handles behavior; if you want the data / results, then you will have to call on the
/// </summary>
internal sealed class SchedulingStopwatch
{
/// <summary>
/// Stopwatch used to measure turnaround time.
/// </summary>
private ValueStopwatch turnaroundTimeStopwatch;
/// <summary>
/// Stopwatch used to measure response time.
/// </summary>
private ValueStopwatch responseTimeStopwatch;
/// <summary>
/// Stopwatch used to measure runtime.
/// </summary>
private ValueStopwatch runTimeStopwatch;
/// <summary>
/// Number of times the process was preempted.
/// </summary>
private long numPreemptions;
/// <summary>
/// Whether or not the process got a response yet.
/// </summary>
private bool responded;
/// <summary>
/// Initializes a new instance of the SchedulingStopwatch class.
/// </summary>
public SchedulingStopwatch()
{
this.turnaroundTimeStopwatch = new ValueStopwatch();
this.responseTimeStopwatch = new ValueStopwatch();
this.runTimeStopwatch = new ValueStopwatch();
}
/// <summary>
/// Gets the SchedulingMetricsTimeSpan, which is a readonly snapshot of the SchedulingMetrics.
/// </summary>
/// <returns>the SchedulingMetricsResult.</returns>
public SchedulingTimeSpan Elapsed
{
get
{
return new SchedulingTimeSpan(
this.turnaroundTimeStopwatch.Elapsed,
this.responseTimeStopwatch.Elapsed,
this.runTimeStopwatch.Elapsed,
this.turnaroundTimeStopwatch.Elapsed - this.runTimeStopwatch.Elapsed,
this.numPreemptions);
}
}
/// <summary>
/// Tells the SchedulingStopwatch know that the process is in a state where it is ready to be worked on,
/// which in turn starts the stopwatch for for response time and turnaround time.
/// </summary>
public void Ready()
{
this.turnaroundTimeStopwatch.Start();
this.responseTimeStopwatch.Start();
}
/// <summary>
/// Starts or resumes the stopwatch for runtime meaning that the process in the run state for the first time
/// or was preempted and now back in the run state.
/// </summary>
public void Start()
{
if (!this.runTimeStopwatch.IsRunning)
{
if (!this.responded)
{
// This is the first time the process got a response, so the response time stopwatch needs to stop.
this.responseTimeStopwatch.Stop();
this.responded = true;
}
this.runTimeStopwatch.Start();
}
}
public void Stop()
{
if (this.runTimeStopwatch.IsRunning)
{
this.runTimeStopwatch.Stop();
this.numPreemptions++;
}
}
/// <summary>
/// Stops all the internal stopwatches.
/// This is mainly useful for marking the end of a process to get an accurate turnaround time.
/// It is undefined behavior to start a stopwatch that has been terminated.
/// </summary>
public void Terminate()
{
this.turnaroundTimeStopwatch.Stop();
this.responseTimeStopwatch.Stop();
}
/// <summary>
/// Returns a string version of this SchedulingStopwatch
/// </summary>
/// <returns>String version of the SchedulingStopwatch.</returns>
public override string ToString()
{
// Just passing on to the SchedulingTimeSpan ToString function.
return this.Elapsed.ToString();
}
}
}