Skip to content

Commit

Permalink
Performance counter creation script deletes category only when necess…
Browse files Browse the repository at this point in the history
…ary (#37)

* exptected script content

* cs script generator

* ps1 script writer
  • Loading branch information
tmasternak authored and Scooletz committed Sep 19, 2017
1 parent d349b69 commit f70c683
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 30 deletions.
31 changes: 22 additions & 9 deletions src/ScriptBuilder/CSharpCounterWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,39 @@ public static void WriteCode(string scriptPath, IEnumerable<TimerDefinition> tim
using System.Runtime.CompilerServices;
[CompilerGenerated]
public static class CounterCreator
public static class CounterCreator
{{
public static void Create()
public static void Create()
{{
var counterCreationCollection = new CounterCreationDataCollection(Counters);
try
{{
var categoryName = ""NServiceBus"";
if (PerformanceCounterCategory.Exists(categoryName))
{{
PerformanceCounterCategory.Delete(categoryName);
foreach (CounterCreationData counter in counterCreationCollection)
{{
if (!PerformanceCounterCategory.CounterExists(counter.CounterName, categoryName))
{{
PerformanceCounterCategory.Delete(categoryName);
break;
}}
}}
}}
if (PerformanceCounterCategory.Exists(categoryName) == false)
{{
PerformanceCounterCategory.Create(
categoryName: categoryName,
categoryHelp: ""NServiceBus statistics"",
categoryType: PerformanceCounterCategoryType.MultiInstance,
counterData: counterCreationCollection);
}}
PerformanceCounterCategory.Create(
categoryName: categoryName,
categoryHelp: ""NServiceBus statistics"",
categoryType: PerformanceCounterCategoryType.MultiInstance,
counterData: counterCreationCollection);
PerformanceCounter.CloseSharedResources();
}} catch(Exception ex) when(ex is SecurityException || ex is UnauthorizedAccessException)
}}
catch (Exception ex) when (ex is SecurityException || ex is UnauthorizedAccessException)
{{
throw new Exception(""Execution requires elevated permissions"", ex);
}}
Expand Down
29 changes: 22 additions & 7 deletions src/ScriptBuilder/PowerShellCounterWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,37 @@ public static void WriteScript(string scriptPath, IEnumerable<TimerDefinition> t
}
}

const string Template = @"
#requires -RunAsAdministrator
const string Template = @"#requires -RunAsAdministrator
Function InstallNSBPerfCounters {{
$category = @{{Name=""NServiceBus""; Description=""NServiceBus statistics""}}
$counters = New-Object System.Diagnostics.CounterCreationDataCollection
$counters.AddRange(@(
{0}
))
if ([System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) {{
[System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name)
foreach($counter in $counters){{
$exists = [System.Diagnostics.PerformanceCounterCategory]::CounterExists($counter.CounterName, $category.Name)
if (!$exists){{
Write-Host ""One or more counters are missing.The performance counter category will be recreated""
[System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name)
break
}}
}}
}}
[void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters)
if (![System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) {{
Write-Host ""Creating the performance counter category""
[void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters)
}}
else {{
Write-Host ""No performance counters have to be created""
}}
[System.Diagnostics.PerformanceCounter]::CloseSharedResources()
}}
InstallNSBPerfCounters
";
InstallNSBPerfCounters";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@
using System.Runtime.CompilerServices;

[CompilerGenerated]
public static class CounterCreator
public static class CounterCreator
{
public static void Create()
public static void Create()
{
var counterCreationCollection = new CounterCreationDataCollection(Counters);
try
{
var categoryName = "NServiceBus";

if (PerformanceCounterCategory.Exists(categoryName))
{
PerformanceCounterCategory.Delete(categoryName);
foreach (CounterCreationData counter in counterCreationCollection)
{
if (!PerformanceCounterCategory.CounterExists(counter.CounterName, categoryName))
{
PerformanceCounterCategory.Delete(categoryName);
break;
}
}
}

if (PerformanceCounterCategory.Exists(categoryName) == false)
{
PerformanceCounterCategory.Create(
categoryName: categoryName,
categoryHelp: "NServiceBus statistics",
categoryType: PerformanceCounterCategoryType.MultiInstance,
counterData: counterCreationCollection);
}

PerformanceCounterCategory.Create(
categoryName: categoryName,
categoryHelp: "NServiceBus statistics",
categoryType: PerformanceCounterCategoryType.MultiInstance,
counterData: counterCreationCollection);
PerformanceCounter.CloseSharedResources();
} catch(Exception ex) when(ex is SecurityException || ex is UnauthorizedAccessException)
}
catch (Exception ex) when (ex is SecurityException || ex is UnauthorizedAccessException)
{
throw new Exception("Execution requires elevated permissions", ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#requires -RunAsAdministrator
Function InstallNSBPerfCounters {

$category = @{Name="NServiceBus"; Description="NServiceBus statistics"}
$counters = New-Object System.Diagnostics.CounterCreationDataCollection
$counters.AddRange(@(
Expand All @@ -13,10 +12,27 @@ Function InstallNSBPerfCounters {
New-Object System.Diagnostics.CounterCreationData "# of msgs successfully processed / sec", "The current number of messages processed successfully by the transport per second.", RateOfCountsPerSecond32

))

if ([System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) {
[System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name)
foreach($counter in $counters){
$exists = [System.Diagnostics.PerformanceCounterCategory]::CounterExists($counter.CounterName, $category.Name)
if (!$exists){
Write-Host "One or more counters are missing.The performance counter category will be recreated"
[System.Diagnostics.PerformanceCounterCategory]::Delete($category.Name)

break
}
}
}
[void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters)

if (![System.Diagnostics.PerformanceCounterCategory]::Exists($category.Name)) {
Write-Host "Creating the performance counter category"
[void] [System.Diagnostics.PerformanceCounterCategory]::Create($category.Name, $category.Description, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters)
}
else {
Write-Host "No performance counters have to be created"
}

[System.Diagnostics.PerformanceCounter]::CloseSharedResources()
}
InstallNSBPerfCounters
InstallNSBPerfCounters
3 changes: 3 additions & 0 deletions src/ScriptBuilderTask.Tests/PowershellCodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using ApprovalTests;
using ApprovalTests.Reporters;
using NServiceBus.Metrics.PerformanceCounters;
using NUnit.Framework;

Expand Down Expand Up @@ -36,6 +37,8 @@ public void Generates()
{
task.Execute();

GenericDiffReporter.RegisterTextFileTypes(".ps1");

var powershell = Directory.EnumerateFiles(tempPath, "*.ps1", SearchOption.AllDirectories).Single();
Approvals.VerifyFile(powershell);
}
Expand Down

0 comments on commit f70c683

Please sign in to comment.