Skip to content

Commit a4e44fb

Browse files
committed
Add verbose mode, improve save discovery messages
Add -verboseResult mode that creates an excel file with tick based log data stored in sheets for every separate run. Feature required ImportExcel dependency. See the readme for installation instructions. -outputNameVerbose and -verboseItems for picking the verbose excel filename and which debug stats are used for verbose logging Improvements to save discovery user messages Human readable headers for normal csv output file Fix remaining vars to camelCase Add dependencies.txt
1 parent 93fdc22 commit a4e44fb

File tree

4 files changed

+137
-55
lines changed

4 files changed

+137
-55
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.csv
22
*.log
3+
Results

Diff for: Readme.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
* Aggregation of benchmark data into an output CSV file
66
* Disabling of mods for the duration of the benchmark (can use mods while benchmarking with -enableMods)
7-
* Cpu Priority selection (Cmdline flag -cpuPriority, defaults to "High")
7+
* Cpu Priority selection (-cpuPriority, defaults to "High")
88
* Loading of benchmarked savefiles via a command line param / config param (-savePath / $savePath)
99
* Regex pattern can be used to further limit which saves are benchmarked (-pattern "some pattern")
10+
* Verbose result mode (-verboseResult) allows creation of an excel file where
11+
separate run results are saved to their own sheets with tick based update
12+
times
1013

1114
Various other command line options and flags for customizing functionality.
1215
Default values can be changed by editing the script, in the params section
@@ -20,6 +23,15 @@ Before running please go through at least the Basic Settings section of the
2023
script and switch the paths that are different for you. The defaults use Steam
2124
Factorio paths.
2225

26+
## Dependencies
27+
28+
Verbose mode depends on [Import-Excel](https://github.com/dfinke/ImportExcel)
29+
to create the output excel file with runs in their own sheets.
30+
31+
Install it by running the following command in powershell:
32+
33+
Install-Module ImportExcel -scope CurrentUser
34+
2335
## Usage:
2436

2537
Open the folder you put the ```benchmark.ps1``` script in using explorer. Select the

Diff for: benchmark.ps1

+117-54
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Factorio Benchmark Script v1.0.0
1+
# Factorio Benchmark Script v1.1.0
2+
# Depends on Import-Excel https://github.com/dfinke/ImportExcel for -verboseOutput
23
param (
34

45
##################
@@ -43,8 +44,11 @@ param (
4344
# ADVANCED SETTINGS #
4445
#####################
4546

46-
# Output filename
47-
[string]$outputName = "results.csv",
47+
# Output csv filename
48+
[string]$outputName = "results",
49+
50+
# Output xlsx verbose filename
51+
[string]$outputNameVerbose = "verbose",
4852

4953
# Which folder to output results into.
5054
[string]$outputFolder = ".\Results\",
@@ -61,6 +65,18 @@ param (
6165
# If given disables automatic mod disabling feature
6266
[switch]$enableMods = $false,
6367

68+
# If given enables verbose mode which logs per-tick benchmarks
69+
[switch]$verboseResult = $false,
70+
71+
# Which items are selected for verbose logging
72+
#
73+
# Available options are:
74+
#
75+
# tick,timestamp,wholeUpdate,latencyUpdate,gameUpdate,circuitNetworkUpdate,transportLinesUpdate,fluidsUpdate,heatManagerUpdate,entityUpdate,particleUpdate,mapGenerator,mapGeneratorBasicTilesSupportCompute,mapGeneratorBasicTilesSupportApply,mapGeneratorCorrectedTilesPrepare,mapGeneratorCorrectedTilesCompute,mapGeneratorCorrectedTilesApply,mapGeneratorVariations,mapGeneratorEntitiesPrepare,mapGeneratorEntitiesCompute,mapGeneratorEntitiesApply,crcComputation,electricNetworkUpdate,logisticManagerUpdate,constructionManagerUpdate,pathFinder,trains,trainPathFinder,commander,chartRefresh,luaGarbageIncremental,chartUpdate,scriptUpdate,
76+
#
77+
# tick must be one of the selected items, otherwise the script won't work
78+
[string]$verboseItems = "tick,wholeUpdate,wholeUpdate,gameUpdate,circuitNetworkUpdate,transportLinesUpdate,fluidsUpdate,entityUpdate,electricNetworkUpdate,logisticManagerUpdate,trains,trainPathFinder",
79+
6480
# Can customize used CPU priority.
6581
[string]$cpuPriority = "High",
6682

@@ -71,17 +87,25 @@ param (
7187
# Eg. enabling core 1, 3 and 5 is 1 + 4 + 16 = 21
7288
# 0 to disable feature
7389
[int]$cpuAffinity = 0
74-
75-
# If given, don't interleave runs and use factorio.exe run mechanism which is faster in real time
76-
# TODO: implement
77-
# [switch]$noRunInterleaving = $false,
7890
)
7991
#End of user variables
8092

8193
$ErrorActionPreference = "Stop"
8294

95+
$excelEnabled = $false
96+
if (Get-Command Export-Excel -errorAction SilentlyContinue)
97+
{
98+
$excelEnabled = $true
99+
}
100+
elseif ($verboseResult) {
101+
Write-Output "`nUNMET DEPENDENCY. Export-Excel cmdlet not found for verbose mode." `
102+
"Please install it by running this command in powershell:" `
103+
"`n Install-Module ImportExcel -scope CurrentUser`n" `
104+
"Script will run normally but verbose excel file won't be generated."
105+
}
106+
83107
# Collect the saves to benchmark
84-
echo ""
108+
Write-Output ""
85109
if ($pattern -ne "") {
86110
[array]$saves = dir $savepath -file -recurse | where {$_.BaseName -Match $pattern}
87111
$saveFoundMessage = "found matching pattern '$pattern'"
@@ -92,17 +116,17 @@ else {
92116
}
93117

94118
if ($saves.length -ne 0) {
95-
echo "Following saves ${saveFoundMessage}:"
119+
Write-Output "Following saves ${saveFoundMessage}:"
96120
}
97121
else {
98-
echo "No saves $saveFoundMessage."
99-
echo ""
122+
Write-Output "No saves $saveFoundMessage."
123+
Write-Output ""
100124
exit
101125
}
102126

103-
echo ""
104-
echo $($saves | select -ExpandProperty BaseName)
105-
echo ""
127+
Write-Output ""
128+
Write-Output $($saves | select -ExpandProperty BaseName)
129+
Write-Output ""
106130
Write-Host -NoNewline "Executing benchmark after confirmation. Ctrl-c to cancel. "
107131
pause
108132

@@ -113,11 +137,17 @@ try {
113137
# Remove illegal filename characters from pattern for output filename
114138
$sanitized_pattern = ($pattern.Split([IO.Path]::GetInvalidFileNameChars()) -join '_') + " "
115139
}
116-
[System.IO.FileInfo]$output = Join-Path $outputFolder -ChildPath ($sanitized_pattern + $outputName)
140+
[System.IO.FileInfo]$output = Join-Path $outputFolder -ChildPath ($sanitized_pattern + $outputName + ".csv")
141+
[System.IO.FileInfo]$outputVerbose = Join-Path $outputFolder -ChildPath ($sanitized_pattern + $outputNameVerbose + ".xlsx")
117142

118143
# Delete output file if feature is enabled
119-
if (($clearOutputFile) -and (Test-Path $output)) {
120-
rm $output
144+
if ($clearOutputFile) {
145+
if (Test-Path $output) {
146+
rm $output
147+
}
148+
if (Test-Path $outputVerbose) {
149+
rm $outputVerbose
150+
}
121151
}
122152

123153
# Check if output file already exists
@@ -126,41 +156,47 @@ try {
126156
[Void](New-Item -Force (Split-Path -Path $output) -ItemType Directory)
127157

128158
# Create output and print headers
129-
echo "save_name,run,startup_time_s,end_time_s,avg_ms,min_ms,max_ms,ticks,execution_time_ms,effective_UPS,version,platform,calibration" > $output
159+
Write-Output "Save,Run,Startup time,End time,Avg ms,Min ms,Max ms,Ticks,Execution Time ms,Effective UPS,Version,Platform,Calibration" > $output
130160
}
131161

132162
# Mod disabling block
133163
if (-not $enableMods) {
134-
$mods_path = Join-Path -Path $configpath -ChildPath "mods"
135-
$backup_path = Join-Path -Path $configpath -ChildPath "mods_disabled"
136-
if (Test-Path $mods_path) {
137-
if (-not (Test-Path $backup_path)) {
138-
echo "Disabling mods."
139-
Move-Item -Path $mods_path -Destination $backup_path
164+
$modsPath = Join-Path -Path $configpath -ChildPath "mods"
165+
$backupPath = Join-Path -Path $configpath -ChildPath "mods_disabled"
166+
if (Test-Path $modsPath) {
167+
if (-not (Test-Path $backupPath)) {
168+
Write-Output "Disabling mods."
169+
Move-Item -Path $modsPath -Destination $backupPath
140170
}
141171
else {
142-
echo "Mods already disabled previously. Doing nothing."
172+
Write-Output "Mods already disabled previously. Doing nothing."
143173
}
144174
}
145175
else {
146-
echo "Mod folder not found. Doing nothing."
176+
Write-Output "Mod folder not found. Doing nothing."
147177
}
148178
}
149179

150-
echo ""
180+
Write-Output ""
151181
# Main benchmark loop
152182
for ($i = 0; $i -lt $runs; $i++) {
153183
for ($j = 0; $j -lt $saves.length; $j++) {
154184
$run = $i + 1
155185
$save = $saves[$j].FullName
156-
$save_name = $saves[$j].BaseName
157-
$log_path = Join-Path $outputFolder ($save_name + " Run" + $run + ".log")
186+
$saveName = $saves[$j].BaseName
187+
$runName = $saveName + " Run " + $run
188+
$logPath = Join-Path $outputFolder ($runName + ".log")
158189

159-
Write-Host -NoNewline "Benchmarking '$save_name' Run $run`t"
190+
Write-Host -NoNewline "Benchmarking $runName`t"
160191

161192
# Run factorio
162-
$arg_list = "--benchmark `"$save`" --benchmark-ticks $ticks --disable-audio"
163-
$process = Start-Process -PassThru -FilePath $executable -ArgumentList $arg_list -RedirectStandardOutput $log_path
193+
$argList = "--benchmark `"$save`" --benchmark-ticks $ticks --disable-audio"
194+
195+
if ($verboseResult) {
196+
$argList += " --benchmark-verbose " + $verboseItems
197+
}
198+
199+
$process = Start-Process -PassThru -FilePath $executable -ArgumentList $argList -RedirectStandardOutput $logPath
164200

165201
# Set process flags and wait for execution to finish
166202
$process.PriorityClass = $cpuPriority
@@ -169,43 +205,70 @@ try {
169205
}
170206
$process.WaitForExit()
171207

172-
# Perform a cleanup pass on the data, since depending on the time to benchmark a number of spaces will be added to the lines
173-
$log_data = (Get-Content $log_path) -replace '^\s+', ''
174-
$log_data | Set-Content $log_path
208+
# Perform a cleanup pass on the data, since depending on the time to benchmark a number of spaces will be added to
209+
# the lines
210+
$logData = (Get-Content $logPath) -replace '^\s+', ''
211+
$logData | Set-Content $logPath
175212

176213
# Parse data
177-
$avg_ms = (($log_data | Select-String "avg:") -split " ")[1]
178-
$min_ms = (($log_data | Select-String "avg:") -split " ")[4]
179-
$max_ms = (($log_data | Select-String "avg:") -split " ")[7]
180-
$version = (($log_data | Select-Object -First 1) -split " ")[4]
181-
$execution_time_ms = (($log_data | Select-String "Performed") -split " ")[4]
182-
$startup_time_s = (($log_data | Select-String "Loading script.dat") -split " ")[0]
183-
$end_time_s = (($log_data | Select-Object -Last 1) -split " ")[0]
184-
$effective_UPS = [math]::Round((1000 * $ticks / $execution_time_ms), 2)
214+
$avg = (($logData | Select-String "avg:") -split " ")[1]
215+
$min = (($logData | Select-String "avg:") -split " ")[4]
216+
$max = (($logData | Select-String "avg:") -split " ")[7]
217+
$version = (($logData | Select-Object -First 1) -split " ")[4]
218+
$executionTime = (($logData | Select-String "Performed") -split " ")[4]
219+
$startupTime = (($logData | Select-String "Loading script.dat") -split " ")[0]
220+
$endTime = (($logData | Select-Object -Last 1) -split " ")[0]
221+
$effectiveUPS = [math]::Round((1000 * $ticks / $executionTime), 2)
185222

186223
# Save the results
187-
echo "$save_name,$run,$startup_time_s,$end_time_s,$avg_ms,$min_ms,$max_ms,$ticks,$execution_time_ms,$effective_UPS,$version,$platform,$calibration" >> $output
188-
189-
echo "end_time $end_time_s seconds"
224+
Write-Output "$($executionTime / 1000) seconds"
225+
Write-Output "$saveName,$run,$startupTime,$endTime,$avg,$min,$max,$ticks,$executionTime,$effectiveUPS,$version,$platform,$calibration" >> $output
226+
227+
# If verbose result is enabled produce a separarte excel file with verbose results
228+
if (($verboseResult) -and ($excelEnabled)) {
229+
$time = Get-Date -Format "HHmm "
230+
231+
# Select run-specific lines
232+
# remove 't' from tick number
233+
$verboseData = $logData `
234+
| Select-String -Pattern "(^tick,)|(^t\d+)," `
235+
| ForEach-Object {$_ -Replace "^t(\d+)", '$1'} `
236+
| ConvertFrom-Csv `
237+
238+
# Change to milliseconds and make ticks 1-based
239+
$verboseData | ForEach-Object {
240+
foreach ($property in $_.PSObject.Properties) {
241+
if ($property.Name -eq "tick") {
242+
[int]$property.Value += 1
243+
}
244+
else {
245+
$property.Value = $property.Value / 1000000
246+
}
247+
}
248+
}
249+
250+
# Output excel file
251+
$verboseData | Export-Excel -AutoSize -WorksheetName ($time + $runName) $outputVerbose
252+
}
190253

191254
if (-not ($keepLogs)) {
192-
rm "$log_path"
255+
rm "$logPath"
193256
}
194257
}
195258
}
196259
}
197260
# Cleanup
198261
finally {
199262
if (-not $enableMods) {
200-
if (Test-Path $backup_path) {
201-
if (Test-Path $mods_path) {
202-
echo "`nRestoring original mods."
203-
mv -Force (Join-Path -Path $backup_path -ChildPath "\*") $mods_path
204-
rm $backup_path
263+
if (Test-Path $backupPath) {
264+
if (Test-Path $modsPath) {
265+
Write-Output "`nRestoring original mods."
266+
mv -Force (Join-Path -Path $backupPath -ChildPath "\*") $modsPath
267+
rm $backupPath
205268
}
206269
else {
207-
echo "Mods folder not created. Probably factorio didn't run. Restoring mods."
208-
mv $backup_path $mods_path
270+
Write-Output "Mods folder not created. Probably factorio didn't run. Restoring mods."
271+
mv $backupPath $modsPath
209272
}
210273
}
211274
}

Diff for: dependencies.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://github.com/dfinke/ImportExcel
2+
Import-Excel
3+
4+
# Install it by running this command:
5+
#
6+
# Install-Module ImportExcel -scope CurrentUser

0 commit comments

Comments
 (0)