-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic CLR metrics, some minor API refactoring
- Loading branch information
1 parent
4497b92
commit 38cc306
Showing
8 changed files
with
201 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Diagnostics; | ||
using metrics.Core; | ||
using NUnit.Framework; | ||
|
||
namespace metrics.Tests.Core | ||
{ | ||
[TestFixture] | ||
public class CLRProfilerTests | ||
{ | ||
[Test] | ||
public void Can_get_heap_usage() | ||
{ | ||
var heap = CLRProfiler.HeapUsage; | ||
Assert.IsNotNull(heap); | ||
Trace.WriteLine(heap); | ||
} | ||
|
||
[Test] | ||
public void Can_get_uptime() | ||
{ | ||
var heap = CLRProfiler.Uptime; | ||
Assert.IsNotNull(heap); | ||
Trace.WriteLine(heap); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace metrics.Core | ||
{ | ||
public static class CLRProfiler | ||
{ | ||
private const string CategoryMemory = ".NET CLR Memory"; | ||
private static readonly Process _process; | ||
private static readonly IDictionary<Process, IDictionary<string, PerformanceCounter>> _counters; | ||
|
||
static CLRProfiler() | ||
{ | ||
_process = Process.GetCurrentProcess(); | ||
_counters = new Dictionary<Process, IDictionary<string, PerformanceCounter>> | ||
{ | ||
{_process, new Dictionary<string, PerformanceCounter>()} | ||
}; | ||
} | ||
|
||
public static string DumpThreads() | ||
{ | ||
throw new NotImplementedException(); | ||
return "Not implemented; how about a fork?"; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the number of seconds the CLR process has been running | ||
/// </summary> | ||
public static long Uptime | ||
{ | ||
get { return Convert.ToInt64(_process.TotalProcessorTime.TotalSeconds); } | ||
} | ||
|
||
/// <summary> | ||
/// Returns the percentage of the CLR's heap which is being used | ||
/// </summary> | ||
public static double HeapUsage | ||
{ | ||
get | ||
{ | ||
var counter = GetOrInstallCounter("HeapUsage", CategoryMemory); | ||
var used = WaitForNextRawValue(counter); | ||
|
||
var available = _process.PrivateMemorySize64; | ||
var usage = (double)used / available; | ||
return usage; | ||
} | ||
} | ||
|
||
private static long WaitForNextRawValue(PerformanceCounter counter) | ||
{ | ||
long used; | ||
while((used = counter.NextSample().RawValue) == 0) | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
return used; | ||
} | ||
|
||
private static PerformanceCounter GetOrInstallCounter(string property, string category) | ||
{ | ||
if (!_counters[_process].ContainsKey(property)) | ||
{ | ||
_counters[_process].Add(property, new PerformanceCounter(category, "# bytes in all heaps", _process.ProcessName)); | ||
} | ||
return _counters[_process][property]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters