-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.ps1
More file actions
305 lines (260 loc) · 9.95 KB
/
benchmark.ps1
File metadata and controls
305 lines (260 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<#
.SYNOPSIS
Automated cold-read benchmark for localStorage first access.
.DESCRIPTION
Launches Edge, populates localStorage, then restarts the browser multiple
times to measure the cold first-read duration. Runs for both LevelDB
(default) and SQLite backends, then prints a comparison summary.
.PARAMETER Runs
Number of cold-read measurements per backend (default: 10).
.PARAMETER Entries
Number of localStorage entries to populate (default: 10000).
.PARAMETER ValueSize
Size of each value in characters (default: 100).
.PARAMETER Delay
Milliseconds to wait after page load before measuring (default: 5000).
.PARAMETER EdgePath
Path to msedge.exe. Defaults to Edge SxS (Canary).
.PARAMETER PageUrl
URL of the benchmark page. Defaults to the GitHub Pages deployment.
.PARAMETER CdpPort
Port for Chrome DevTools Protocol (default: 9222).
.EXAMPLE
.\benchmark.ps1 -Runs 10 -Entries 10000 -ValueSize 100 -Delay 5000
#>
param(
[int]$Runs = 20,
[int]$Entries = 10000,
[int]$ValueSize = 100,
[int]$Delay = 5000,
[string]$EdgePath = "$env:LOCALAPPDATA\Microsoft\Edge SxS\Application\msedge.exe",
[string]$PageUrl = "https://rahulsingh-msft.github.io/localstorage-getall-perf/",
[int]$CdpPort = 9222
)
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------
# Tracks the PID of the most recently launched Edge process tree.
$script:EdgePid = $null
function Stop-EdgeTree {
# Try graceful shutdown first via CDP so Edge can flush data to disk.
try {
Invoke-RestMethod -Uri "http://localhost:$CdpPort/json/close" -TimeoutSec 3 -ErrorAction SilentlyContinue 2>$null | Out-Null
} catch { }
# Wait for Edge to exit gracefully.
if ($script:EdgePid) {
try {
$proc = Get-Process -Id $script:EdgePid -ErrorAction SilentlyContinue
if ($proc) {
$proc.CloseMainWindow() | Out-Null
$proc.WaitForExit(10000) | Out-Null
}
} catch { }
# Force kill only if still running.
$stillRunning = Get-Process -Id $script:EdgePid -ErrorAction SilentlyContinue
if ($stillRunning) {
taskkill /T /F /PID $script:EdgePid 2>$null | Out-Null
}
$script:EdgePid = $null
}
# Catch any orphaned processes from the same Edge install only.
Get-Process msedge -ErrorAction SilentlyContinue |
Where-Object { $_.Path -and $_.Path.StartsWith((Split-Path $EdgePath)) } |
Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 3
}
function Launch-Edge {
param(
[string]$Url,
[string]$UserDataDir,
[string[]]$ExtraFlags = @()
)
$edgeArgs = @(
"--user-data-dir=`"$UserDataDir`"",
"--remote-debugging-port=$CdpPort",
"--no-first-run",
"--no-default-browser-check",
"--disable-extensions",
"--disable-sync"
) + $ExtraFlags + @("`"$Url`"")
$argString = $edgeArgs -join " "
$proc = Start-Process -FilePath $EdgePath -ArgumentList $argString -PassThru
$script:EdgePid = $proc.Id
}
function Get-CdpTitle {
param([int]$TimeoutSec = 60)
$deadline = (Get-Date).AddSeconds($TimeoutSec)
# Wait for CDP to be available.
while ((Get-Date) -lt $deadline) {
try {
$tabs = Invoke-RestMethod -Uri "http://localhost:$CdpPort/json" -TimeoutSec 3
break
} catch {
Start-Sleep -Seconds 1
}
}
# Poll document.title via CDP until it has our result marker.
while ((Get-Date) -lt $deadline) {
try {
$tabs = Invoke-RestMethod -Uri "http://localhost:$CdpPort/json" -TimeoutSec 3
$page = $tabs | Where-Object { $_.type -eq "page" -and $_.url -like "*localstorage*" } | Select-Object -First 1
if ($page -and $page.title -match "^(RESULT|POPULATED):") {
return $page.title
}
} catch { }
Start-Sleep -Seconds 1
}
throw "Timed out waiting for page result"
}
function Run-Backend {
param(
[string]$Name,
[string]$UserDataDir,
[string[]]$FeatureFlags = @()
)
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host " Backend: $Name" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
# Clean user data dir for a fresh start. Retry a few times in case
# file locks from Edge processes haven't fully released.
if (Test-Path $UserDataDir) {
for ($attempt = 1; $attempt -le 5; $attempt++) {
try {
Remove-Item -Recurse -Force $UserDataDir -ErrorAction Stop
break
} catch {
if ($attempt -eq 5) { throw }
Start-Sleep -Seconds 2
}
}
}
# --- Populate ---
Write-Host "Populating $Entries entries ($ValueSize chars each)..." -ForegroundColor Yellow
$populateUrl = "${PageUrl}?populate=${Entries}&valueSize=${ValueSize}"
Launch-Edge -Url $populateUrl -UserDataDir $UserDataDir -ExtraFlags $FeatureFlags
try {
$title = Get-CdpTitle -TimeoutSec 60
if ($title -match "^POPULATED:(\d+)") {
Write-Host " Populated $($Matches[1]) entries." -ForegroundColor Green
} else {
Write-Host " Unexpected populate result: $title" -ForegroundColor Red
}
} catch {
Write-Host " ERROR during populate: $_" -ForegroundColor Red
Stop-EdgeTree
return @()
}
Stop-EdgeTree
# --- Measure cold reads ---
$results = @()
for ($i = 1; $i -le $Runs; $i++) {
Write-Host "Run $i/$Runs ... " -NoNewline
$measureUrl = "${PageUrl}?auto&delay=${Delay}"
Launch-Edge -Url $measureUrl -UserDataDir $UserDataDir -ExtraFlags $FeatureFlags
try {
# Wait for delay + some extra time for page load and CDP.
$timeout = [math]::Max(60, ($Delay / 1000) + 30)
$title = Get-CdpTitle -TimeoutSec $timeout
if ($title -match "^RESULT:([\d.]+):(\d+)") {
$duration = [double]$Matches[1]
$entries = [int]$Matches[2]
$results += $duration
Write-Host "$($duration.ToString('F3')) ms ($entries entries)" -ForegroundColor Green
} else {
Write-Host "Unexpected result: $title" -ForegroundColor Red
}
} catch {
Write-Host "ERROR: $_" -ForegroundColor Red
}
Stop-EdgeTree
}
return $results
}
function Format-Stats {
param(
[string]$Name,
[double[]]$Values
)
if ($Values.Count -eq 0) {
return "$Name : no results"
}
$sorted = $Values | Sort-Object
$mean = ($Values | Measure-Object -Average).Average
$min = $sorted[0]
$max = $sorted[-1]
$median = if ($sorted.Count % 2 -eq 0) {
($sorted[$sorted.Count/2 - 1] + $sorted[$sorted.Count/2]) / 2
} else {
$sorted[[math]::Floor($sorted.Count/2)]
}
$variance = ($Values | ForEach-Object { ($_ - $mean) * ($_ - $mean) } | Measure-Object -Sum).Sum / $Values.Count
$stddev = [math]::Sqrt($variance)
$ciMargin = 1.96 * $stddev / [math]::Sqrt($Values.Count)
$lines = @(
"$Name Results ($($Values.Count) runs):",
" Min: $($min.ToString('F3')) ms",
" Max: $($max.ToString('F3')) ms",
" Mean: $($mean.ToString('F3')) ms",
" Median: $($median.ToString('F3')) ms",
" StdDev: $($stddev.ToString('F3')) ms",
" 95% CI: $($mean.ToString('F3')) +/- $($ciMargin.ToString('F3')) ms",
" All: $($Values | ForEach-Object { $_.ToString('F3') })"
)
return $lines -join "`n"
}
# ---------------------------------------------------------------
# Main
# ---------------------------------------------------------------
Write-Host "LocalStorage First Read Benchmark - Automated" -ForegroundColor White
Write-Host " Runs: $Runs"
Write-Host " Entries: $Entries"
Write-Host " ValueSize: $ValueSize chars"
Write-Host " Delay: $Delay ms"
Write-Host " Delay: $Delay ms"
Write-Host " Edge: $EdgePath"
Write-Host ""
# Make sure Edge exists.
if (-not (Test-Path $EdgePath)) {
Write-Host "ERROR: Edge not found at $EdgePath" -ForegroundColor Red
Write-Host "Set -EdgePath to your msedge.exe location." -ForegroundColor Yellow
exit 1
}
# Ensure no leftover Edge from a previous run.
Stop-EdgeTree
# Run LevelDB (default backend).
$leveldbResults = Run-Backend -Name "LevelDB" `
-UserDataDir "$env:TEMP\bench-leveldb" `
-FeatureFlags @("--disable-features=DomStorageSqlite")
# Run SQLite backend.
$sqliteResults = Run-Backend -Name "SQLite" `
-UserDataDir "$env:TEMP\bench-sqlite" `
-FeatureFlags @("--enable-features=DomStorageSqlite")
# --- Summary ---
$leveldbStats = Format-Stats -Name "LevelDB" -Values $leveldbResults
$sqliteStats = Format-Stats -Name "SQLite" -Values $sqliteResults
Write-Host ""
Write-Host "=============================================" -ForegroundColor White
Write-Host " SUMMARY" -ForegroundColor White
Write-Host "=============================================" -ForegroundColor White
Write-Host ""
Write-Host $leveldbStats -ForegroundColor Cyan
Write-Host ""
Write-Host $sqliteStats -ForegroundColor Cyan
Write-Host ""
# --- Write results to file ---
$resultsPath = Join-Path $PSScriptRoot "results.txt"
$header = @(
"LocalStorage First Read Benchmark",
"Date: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
"Runs: $Runs",
"Entries: $Entries",
"ValueSize: $ValueSize chars",
"Delay: $Delay ms",
"Edge: $EdgePath",
"",
"============================================="
)
($header + @("", $leveldbStats, "", $sqliteStats, "")) -join "`n" | Set-Content -Path $resultsPath
Write-Host "Results written to $resultsPath" -ForegroundColor Green