Skip to content

Commit 6508b75

Browse files
build.ps1: make time summary hierarchical
1 parent 75d60b3 commit 6508b75

File tree

1 file changed

+96
-32
lines changed

1 file changed

+96
-32
lines changed

utils/build.ps1

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ $WindowsSDKBuilds = @($WindowsSDKArchitectures | ForEach-Object {
581581
## Helpers for logging and timing build steps.
582582

583583
$TimingData = New-Object System.Collections.Generic.List[System.Object]
584+
$CurrentOperation = $null
584585

585586
function Add-TimingData {
586587
param
@@ -589,16 +590,86 @@ function Add-TimingData {
589590
[Hashtable] $Platform,
590591
[Parameter(Mandatory)]
591592
[string] $BuildStep,
592-
[Parameter(Mandatory)]
593-
[System.TimeSpan] $ElapsedTime
593+
[PSCustomObject] $Parent = $null
594594
)
595595

596-
$TimingData.Add([PSCustomObject]@{
596+
$TimingEntry = [PSCustomObject]@{
597597
Arch = $Platform.Architecture.LLVMName
598598
Platform = $Platform.OS.ToString()
599599
"Build Step" = $BuildStep
600-
"Elapsed Time" = $ElapsedTime
601-
})
600+
"Elapsed Time" = [TimeSpan]::Zero
601+
"Child Time" = [TimeSpan]::Zero
602+
Parent = $Parent
603+
Children = @()
604+
}
605+
606+
if ($Parent) {
607+
$Parent.Children += $TimingEntry
608+
}
609+
610+
$TimingData.Add($TimingEntry)
611+
return $TimingEntry
612+
}
613+
614+
function Record-OperationTime {
615+
param
616+
(
617+
[Parameter(Mandatory)]
618+
[Hashtable] $Platform,
619+
[Parameter(Mandatory)]
620+
[string] $BuildStep,
621+
[Parameter(Mandatory)]
622+
[ScriptBlock] $ScriptBlock
623+
)
624+
if (!$Summary) {
625+
& $ScriptBlock
626+
return
627+
}
628+
629+
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
630+
$TimingEntry = Add-TimingData $Platform $BuildStep $script:CurrentOperation
631+
$script:CurrentOperation = $TimingEntry
632+
633+
try {
634+
& $ScriptBlock
635+
} finally {
636+
$Stopwatch.Stop()
637+
$TimingEntry."Elapsed Time" = $Stopwatch.Elapsed
638+
if ($TimingEntry.Parent) {
639+
$TimingEntry.Parent."Child Time" = $TimingEntry.Parent."Child Time".Add($Stopwatch.Elapsed)
640+
}
641+
$script:CurrentOperation = $TimingEntry.Parent
642+
}
643+
}
644+
645+
function Flatten-TimingEntry {
646+
param(
647+
[Parameter(Mandatory)]
648+
[PSCustomObject] $Entry,
649+
[Parameter(Mandatory)]
650+
[TimeSpan] $TotalTime,
651+
[int] $Depth = 0
652+
)
653+
654+
$Indent = " " * $Depth
655+
$Percentage = [math]::Round(($Entry."Elapsed Time".TotalSeconds / $TotalTime.TotalSeconds) * 100, 1)
656+
$FormattedTime = "{0:hh\:mm\:ss\.ff}" -f $Entry."Elapsed Time"
657+
$SelfTime = $Entry."Elapsed Time".Subtract($Entry."Child Time")
658+
$FormattedSelfTime = "{0:hh\:mm\:ss\.ff}" -f $SelfTime
659+
660+
[PSCustomObject]@{
661+
"Total Time" = $FormattedTime
662+
"Self Time" = $FormattedSelfTime
663+
"%" = "$Percentage%"
664+
"Build Step" = "$Indent$($Entry.'Build Step')"
665+
Platform = $Entry.Platform
666+
Arch = $Entry.Arch
667+
}
668+
669+
$SortedChildren = $Entry.Children | Sort-Object -Descending -Property "Elapsed Time"
670+
foreach ($Child in $SortedChildren) {
671+
Flatten-TimingEntry $Child $TotalTime ($Depth + 1)
672+
}
602673
}
603674

604675
function Write-Summary {
@@ -611,31 +682,27 @@ function Write-Summary {
611682

612683
$TotalTime = [TimeSpan]::Zero
613684
foreach ($Entry in $TimingData) {
614-
$TotalTime = $TotalTime.Add($Entry."Elapsed Time")
615-
}
616-
617-
$SortedData = $TimingData | ForEach-Object {
618-
$Percentage = [math]::Round(($_.("Elapsed Time").TotalSeconds / $TotalTime.TotalSeconds) * 100, 1)
619-
$FormattedTime = "{0:hh\:mm\:ss\.ff}" -f $_."Elapsed Time"
620-
[PSCustomObject]@{
621-
"Build Step" = $_."Build Step"
622-
Platform = $_.Platform
623-
Arch = $_.Arch
624-
"Elapsed Time" = $FormattedTime
625-
"%" = "$Percentage%"
685+
if (-not $Entry.Parent) {
686+
$TotalTime = $TotalTime.Add($Entry."Elapsed Time")
626687
}
627-
} | Sort-Object -Descending -Property "Elapsed Time"
688+
}
689+
690+
$RootEntries = $TimingData | Where-Object { -not $_.Parent } | Sort-Object -Descending -Property "Elapsed Time"
691+
$Result = foreach ($RootEntry in $RootEntries) {
692+
Flatten-TimingEntry $RootEntry $TotalTime
693+
}
628694

629695
$FormattedTotalTime = "{0:hh\:mm\:ss\.ff}" -f $TotalTime
630696
$TotalRow = [PSCustomObject]@{
697+
"Total Time" = $FormattedTotalTime
698+
"Self Time" = $FormattedTotalTime
699+
"%" = "100.0%"
631700
"Build Step" = "TOTAL"
632701
Platform = ""
633702
Arch = ""
634-
"Elapsed Time" = $FormattedTotalTime
635-
"%" = "100.0%"
636703
}
637704

638-
@($SortedData) + $TotalRow | Format-Table -AutoSize
705+
@($Result) + $TotalRow | Format-Table -AutoSize
639706
}
640707

641708
function Get-AndroidNDK {
@@ -720,10 +787,6 @@ function Invoke-BuildStep {
720787
[Object[]] $RemainingArgs
721788
)
722789

723-
if ($Summary) {
724-
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
725-
}
726-
727790
$SplatArgs = @{}
728791
if ($RemainingArgs) {
729792
$Enumerator = $RemainingArgs.GetEnumerator()
@@ -750,10 +813,8 @@ function Invoke-BuildStep {
750813
}
751814
}
752815

753-
& $Name $Platform @SplatArgs
754-
755-
if ($Summary) {
756-
Add-TimingData $Platform $Name $Stopwatch.Elapsed
816+
Record-OperationTime $Platform $Name {
817+
& $Name $Platform @SplatArgs
757818
}
758819
}
759820

@@ -1044,7 +1105,7 @@ function Invoke-VsDevShell([Hashtable] $Platform) {
10441105
}
10451106
}
10461107

1047-
function Get-Dependencies {
1108+
function Get-Dependencies-Impl {
10481109
Write-Host "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Get-Dependencies ..." -ForegroundColor Cyan
10491110
$ProgressPreference = "SilentlyContinue"
10501111

@@ -1285,8 +1346,11 @@ function Get-Dependencies {
12851346
Write-Host -ForegroundColor Cyan "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Get-Dependencies took $($Stopwatch.Elapsed)"
12861347
Write-Host ""
12871348
}
1288-
if ($Summary) {
1289-
Add-TimingData $BuildPlatform "Get-Dependencies" $Stopwatch.Elapsed
1349+
}
1350+
1351+
function Get-Dependencies {
1352+
Record-OperationTime $BuildPlatform "Get-Dependencies" {
1353+
Get-Dependencies-Impl
12901354
}
12911355
}
12921356

0 commit comments

Comments
 (0)