@@ -24,9 +24,22 @@ function Get-CIPPTenantAlignment {
2424 [Parameter (Mandatory = $false )]
2525 [string ]$TemplateId
2626 )
27+
28+ # Initialize overall stopwatch
29+ $OverallStopwatch = [System.Diagnostics.Stopwatch ]::StartNew()
30+ $SectionTimings = @ {}
31+
32+ # Measure template table initialization
33+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
2734 $TemplateTable = Get-CippTable - tablename ' templates'
2835 $TemplateFilter = " PartitionKey eq 'StandardsTemplateV2'"
36+ $sw.Stop ()
37+ $SectionTimings [' TemplateTableInit' ] = $sw.ElapsedMilliseconds
38+ Write-Verbose " Template table initialization took: $ ( $sw.ElapsedMilliseconds ) ms"
39+
2940 try {
41+ # Measure template loading
42+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
3043 # Get all standard templates
3144 $Templates = (Get-CIPPAzDataTableEntity @TemplateTable - Filter $TemplateFilter ) | ForEach-Object {
3245 $JSON = $_.JSON
@@ -42,12 +55,18 @@ function Get-CIPPTenantAlignment {
4255 $Data
4356 }
4457 }
58+ $sw.Stop ()
59+ $SectionTimings [' TemplateLoading' ] = $sw.ElapsedMilliseconds
60+ Write-Verbose " Template loading took: $ ( $sw.ElapsedMilliseconds ) ms"
61+ Write-Information " Loaded $ ( $Templates.Count ) templates in $ ( $sw.ElapsedMilliseconds ) ms"
4562
4663 if (-not $Templates ) {
4764 Write-Warning ' No templates found matching the criteria'
4865 return @ ()
4966 }
5067
68+ # Measure standards data retrieval
69+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
5170 # Get standards comparison data
5271 $StandardsTable = Get-CippTable - TableName ' CippStandardsReports'
5372 # this if statement is to bring down performance when running scheduled checks, we have to revisit this to a better query due to the extreme size this can get.
@@ -57,7 +76,13 @@ function Get-CIPPTenantAlignment {
5776 $filter = " PartitionKey ne 'StandardReport' and PartitionKey ne ''"
5877 }
5978 $AllStandards = Get-CIPPAzDataTableEntity @StandardsTable - Filter $filter
79+ $sw.Stop ()
80+ $SectionTimings [' StandardsDataRetrieval' ] = $sw.ElapsedMilliseconds
81+ Write-Verbose " Standards data retrieval took: $ ( $sw.ElapsedMilliseconds ) ms"
82+ Write-Information " Retrieved $ ( $AllStandards.Count ) standards records in $ ( $sw.ElapsedMilliseconds ) ms"
6083
84+ # Measure tenant filtering
85+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
6186 # Filter by tenant if specified
6287 $Standards = if ($TenantFilter ) {
6388 $AllStandards
@@ -66,6 +91,12 @@ function Get-CIPPTenantAlignment {
6691 $AllStandards | Where-Object { $_.PartitionKey -in $Tenants.defaultDomainName }
6792 }
6893 $TagTemplates = Get-CIPPAzDataTableEntity @TemplateTable
94+ $sw.Stop ()
95+ $SectionTimings [' TenantFiltering' ] = $sw.ElapsedMilliseconds
96+ Write-Verbose " Tenant filtering and tag template loading took: $ ( $sw.ElapsedMilliseconds ) ms"
97+
98+ # Measure tenant data structure building
99+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
69100 # Build tenant standards data structure
70101 $tenantData = @ {}
71102 foreach ($Standard in $Standards ) {
@@ -93,11 +124,19 @@ function Get-CIPPTenantAlignment {
93124 }
94125 }
95126 $TenantStandards = $tenantData
127+ $sw.Stop ()
128+ $SectionTimings [' TenantDataStructureBuilding' ] = $sw.ElapsedMilliseconds
129+ Write-Verbose " Tenant data structure building took: $ ( $sw.ElapsedMilliseconds ) ms"
130+ Write-Information " Built data structure for $ ( $tenantData.Count ) tenants in $ ( $sw.ElapsedMilliseconds ) ms"
96131
97132 $Results = [System.Collections.Generic.List [object ]]::new()
98133
134+ # Measure template processing
135+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
136+ $TemplateProcessingCount = 0
99137 # Process each template against all tenants
100138 foreach ($Template in $Templates ) {
139+ $TemplateProcessingCount ++
101140 $TemplateStandards = $Template.standards
102141 if (-not $TemplateStandards ) {
103142 continue
@@ -330,6 +369,22 @@ function Get-CIPPTenantAlignment {
330369 $Results.Add ($Result )
331370 }
332371 }
372+ $sw.Stop ()
373+ $SectionTimings [' TemplateProcessing' ] = $sw.ElapsedMilliseconds
374+ Write-Verbose " Template processing took: $ ( $sw.ElapsedMilliseconds ) ms for $TemplateProcessingCount templates"
375+ Write-Information " Processed $TemplateProcessingCount templates in $ ( $sw.ElapsedMilliseconds ) ms"
376+
377+ # Output timing summary
378+ $OverallStopwatch.Stop ()
379+ Write-Information ' === Get-CIPPTenantAlignment Performance Summary ==='
380+ Write-Information " Total execution time: $ ( $OverallStopwatch.ElapsedMilliseconds ) ms"
381+ Write-Verbose ' Section timings:'
382+ foreach ($Section in $SectionTimings.GetEnumerator () | Sort-Object Value - Descending) {
383+ $Percentage = [math ]::Round(($Section.Value / $OverallStopwatch.ElapsedMilliseconds ) * 100 , 2 )
384+ Write-Verbose " $ ( $Section.Key ) : $ ( $Section.Value ) ms ($Percentage %)"
385+ Write-Information " $ ( $Section.Key ) : $ ( $Section.Value ) ms ($Percentage %)"
386+ }
387+ Write-Information ' ========================================'
333388
334389 return $Results
335390 } catch {
0 commit comments