47 lines
4.2 KiB
PowerShell
47 lines
4.2 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)][string]$ProtocolAssembly,
|
|
[Parameter(Mandatory)][string]$OutputFile,
|
|
[ValidateRange(1,500)][int]$MaximumParagraphs=100,
|
|
[switch]$IncludeLegacyParagraphXml
|
|
)
|
|
$ErrorActionPreference='Stop'
|
|
if($env:OS -ne 'Windows_NT'){throw 'Real Word benchmarks require Windows; no Word process was started.'}
|
|
if([Threading.Thread]::CurrentThread.ApartmentState -ne 'STA'){throw 'Run in Windows PowerShell: powershell.exe -STA -File <this script> ...'}
|
|
if(Test-Path -LiteralPath $OutputFile){throw 'Choose a new output file. Existing results were preserved.'}
|
|
Add-Type -Path (Resolve-Path -LiteralPath $ProtocolAssembly)
|
|
# Attach only to Word which the human deliberately opened for the benchmark. Never launch, select, save or close it.
|
|
$word=[Runtime.InteropServices.Marshal]::GetActiveObject('Word.Application')
|
|
$selection=$null;$range=$null;$paragraphs=$null
|
|
try{
|
|
$selection=$word.Selection;$range=$selection.Range.Duplicate
|
|
if($range.Start -eq $range.End){throw 'Select one representative chapter in Word first. The script never changes the selection.'}
|
|
if(!$range.Document.Saved -or $word.BackgroundSavingStatus -ne 0){throw 'Save Word and wait for background saving before benchmarking.'}
|
|
$times=[ordered]@{}
|
|
$watch=[Diagnostics.Stopwatch]::StartNew();$text=[string]$range.Text;$watch.Stop();$times.rangeTextMilliseconds=$watch.Elapsed.TotalMilliseconds
|
|
$paragraphs=$range.Paragraphs;$count=$paragraphs.Count
|
|
# Range.Text gives exact paragraph separators. The hash benchmark uses the production versioned encoder.
|
|
$rows=[Collections.Generic.List[string]]::new();$offset=0
|
|
foreach($piece in $text.Split([char]13)){if($offset -ge $text.Length){break};$length=$piece.Length+1;if($offset+$length -gt $text.Length){throw 'Selection must end on a complete Word paragraph.'};$rows.Add($text.Substring($offset,$length));$offset+=$length}
|
|
$watch.Restart();$hash=[PlotDirector.Companion.Protocol.Manuscript.ChapterFingerprint]::Content($rows);$watch.Stop();$times.sha256Milliseconds=$watch.Elapsed.TotalMilliseconds
|
|
$watch.Restart();$xml=[string]$range.WordOpenXML;$watch.Stop();$times.chapterWordOpenXmlMilliseconds=$watch.Elapsed.TotalMilliseconds
|
|
$parseError=$null;$parsed=@();$watch.Restart();try{$parsed=[PlotDirector.Companion.Protocol.Manuscript.ChapterFingerprint]::ReadXml($xml,$text)}catch{$parseError=$_.Exception.Message};$watch.Stop();$times.portableXmlIdentityParseMilliseconds=$watch.Elapsed.TotalMilliseconds
|
|
$nativeCount=0
|
|
if($IncludeLegacyParagraphXml){
|
|
# Explicit expensive baseline, capped and cancellable with Ctrl+C; never part of unattended qualification.
|
|
$watch.Restart()
|
|
for($i=1;$i -le [Math]::Min($count,$MaximumParagraphs);$i++){
|
|
$paragraph=$null;$one=$null
|
|
try{$paragraph=$paragraphs.Item($i);$one=$paragraph.Range;$id=[PlotDirector.Companion.Protocol.Manuscript.WordParagraphIdentity]::Read([string]$one.WordOpenXML);if($id){$nativeCount++}}
|
|
finally{if($one){[void][Runtime.InteropServices.Marshal]::ReleaseComObject($one)};if($paragraph){[void][Runtime.InteropServices.Marshal]::ReleaseComObject($paragraph)}}
|
|
}
|
|
$watch.Stop();$times.legacyParagraphXmlMilliseconds=$watch.Elapsed.TotalMilliseconds
|
|
}
|
|
# Cross-process COM call wall times include RPC/marshalling. Use in-add-in metrics for exact UI operation timings.
|
|
$report=[ordered]@{utc=[DateTime]::UtcNow.ToString('o');wordVersion=[string]$word.Version;characters=$text.Length;paragraphs=$count;nativeIds=$parsed.Length;parseError=$parseError;legacyMeasuredParagraphs=$(if($IncludeLegacyParagraphXml){[Math]::Min($count,$MaximumParagraphs)}else{0});legacyNativeIds=$nativeCount;algorithm=[PlotDirector.Companion.Protocol.Manuscript.ChapterFingerprint]::Version;hash=$hash;timings=$times;measurementScope='Human-selected chapter, running Word, cross-process STA COM wall time. No UI responsiveness claim.'}
|
|
$report|ConvertTo-Json -Depth 5|Set-Content -LiteralPath $OutputFile -Encoding UTF8
|
|
Write-Output "Recorded chapter benchmark: $OutputFile. No document was changed, saved or closed."
|
|
}finally{
|
|
foreach($com in @($paragraphs,$range,$selection,$word)){if($com){[void][Runtime.InteropServices.Marshal]::ReleaseComObject($com)}}
|
|
}
|