-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerformanceCounters.cs
179 lines (144 loc) · 5.22 KB
/
PerformanceCounters.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
using System;
using System.Diagnostics;
// Windows Performance Counters
//
// Note: Run as administrator if exception occurs when creating perf. counters.
namespace PerformanceCounters
{
public sealed class WeatherCheckPeformanceMetrics : IDisposable
{
private const string PerformanceCategoryName = "Weather Checker Perf";
private const string PerformanceCategoryDesc = "Weather Checking Performance Counters";
private readonly PerformanceCounter numberOfCats;
private readonly PerformanceCounter numberOfDogs;
private readonly PerformanceCounter RainDropsPerSecond;
private readonly PerformanceCounter windowChecks;
public WeatherCheckPeformanceMetrics()
{
if (!PerformanceCounterCategory.Exists(PerformanceCategoryName))
CreatePerformanceCatagory();
windowChecks = new PerformanceCounter(PerformanceCategoryName, "Weather Check - Window Checks", false);
numberOfCats = new PerformanceCounter(PerformanceCategoryName, "Weather Check - Cats", false);
numberOfDogs = new PerformanceCounter(PerformanceCategoryName, "Weather Check - Dogs", false);
RainDropsPerSecond = new PerformanceCounter(PerformanceCategoryName, "Weather Check - Rain Drops/sec", false);
windowChecks.Increment();
}
/// <summary>
/// Creates new performance category
/// </summary>
private static void CreatePerformanceCatagory()
{
var counters = new CounterCreationDataCollection();
counters.Add(new CounterCreationData(
"Weather Check - Window Checks",
"Number of times I looked out the window.",
PerformanceCounterType.NumberOfItems32
)
);
counters.Add(new CounterCreationData(
"Weather Check - Cats",
"Number of cats.",
PerformanceCounterType.NumberOfItems32
)
);
counters.Add(new CounterCreationData(
"Weather Check - Dogs",
"Number of dogs.",
PerformanceCounterType.NumberOfItems32
)
);
counters.Add(new CounterCreationData(
"Weather Check - Rain Drops/sec",
"Number of rain drops per second.",
PerformanceCounterType.RateOfCountsPerSecond32
)
);
PerformanceCounterCategory.Create(PerformanceCategoryName, PerformanceCategoryDesc,
PerformanceCounterCategoryType.SingleInstance, counters);
}
public void OnCheckWindow()
{
windowChecks.Increment();
}
public void OnSeeDogs(int dogs)
{
numberOfDogs.IncrementBy(dogs);
}
public void OnSeeCats(int cats)
{
numberOfCats.IncrementBy(cats);
}
public void OnMeasureRainDrops(int numberOfDrops)
{
RainDropsPerSecond.IncrementBy(numberOfDrops);
}
private bool isDisposed;
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
windowChecks.Dispose();
numberOfCats.Dispose();
numberOfDogs.Dispose();
RainDropsPerSecond.Dispose();
}
}
}
public class WeatherChecker
{
private readonly WeatherCheckPeformanceMetrics metrics;
Random rnd = new Random();
public WeatherChecker() { }
public WeatherChecker(WeatherCheckPeformanceMetrics metrics)
{
this.metrics = metrics;
}
public int FakeSomeData()
{
return rnd.Next(0, 10000);
}
public void CheckOutside()
{
if (metrics != null)
metrics.OnCheckWindow();
MeasureCats();
MeasureDogs();
MeasureDrops();
}
private void MeasureCats()
{
var cats = FakeSomeData();
Console.WriteLine("Measured {0} cats with cat-o-meter", cats);
if (metrics != null)
metrics.OnSeeCats(cats);
}
private void MeasureDogs()
{
var dogs = FakeSomeData();
Console.WriteLine("Measured {0} dogs with dog-o-scope", dogs);
if (metrics != null)
metrics.OnSeeDogs(dogs);
}
private void MeasureDrops()
{
var drops = FakeSomeData();
Console.WriteLine("Measured {0} rain drops with teaspoon", drops);
if (metrics != null)
metrics.OnMeasureRainDrops(drops);
}
}
public static class Program
{
public static void Main()
{
var metrics = new WeatherCheckPeformanceMetrics();
var checker = new WeatherChecker(metrics);
while (!Console.KeyAvailable)
checker.CheckOutside();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Press enter to exit]");
Console.ReadLine();
}
}
}