Skip to content

Commit 0f5dd3c

Browse files
committed
Create Test-BackupStorageComparison.ps1
1 parent 0f815bb commit 0f5dd3c

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
param(
2+
[Parameter(Mandatory = $false)] [string] $ConnectionString = $env:AzureWebJobsStorage,
3+
[Parameter(Mandatory = $false)] [ValidateSet('Small', 'Medium', 'Large', 'All')] [string] $TestSize = 'All',
4+
[Parameter(Mandatory = $false)] [bool] $Cleanup = $true
5+
)
6+
7+
$ErrorActionPreference = 'Stop'
8+
9+
# Import CIPPCore module from repository
10+
$modulePath = Join-Path $PSScriptRoot '..' 'Modules' 'CIPPCore' 'CIPPCore.psm1'
11+
if (-not (Test-Path -LiteralPath $modulePath)) {
12+
throw "CIPPCore module not found at $modulePath"
13+
}
14+
Import-Module -Force $modulePath
15+
16+
if (-not $ConnectionString) {
17+
throw 'Azure Storage connection string not provided. Set AzureWebJobsStorage or pass -ConnectionString.'
18+
}
19+
20+
Write-Host '================================' -ForegroundColor Cyan
21+
Write-Host 'Backup Storage Comparison Tests' -ForegroundColor Cyan
22+
Write-Host '================================' -ForegroundColor Cyan
23+
24+
# Test data configurations
25+
$testConfigs = @(
26+
@{
27+
Name = 'Small'
28+
ItemCount = 10
29+
PropertiesPerItem = 5
30+
Description = 'Small payload (~5KB)'
31+
},
32+
@{
33+
Name = 'Medium'
34+
ItemCount = 100
35+
PropertiesPerItem = 15
36+
Description = 'Medium payload (~250KB)'
37+
},
38+
@{
39+
Name = 'Large'
40+
ItemCount = 500
41+
PropertiesPerItem = 30
42+
Description = 'Large payload (~2.5MB)'
43+
}
44+
)
45+
46+
function Generate-TestData {
47+
param(
48+
[int]$ItemCount,
49+
[int]$PropertiesPerItem,
50+
[string]$Type
51+
)
52+
53+
$data = @()
54+
for ($i = 0; $i -lt $ItemCount; $i++) {
55+
$item = @{
56+
id = [guid]::NewGuid().ToString()
57+
rowKey = "item_$i"
58+
timestamp = (Get-Date).ToUniversalTime()
59+
table = $Type
60+
}
61+
62+
for ($p = 0; $p -lt $PropertiesPerItem; $p++) {
63+
$item["property_$p"] = "This is test property $p with some additional content to make it realistic. Lorem ipsum dolor sit amet." * 3
64+
}
65+
66+
$data += $item
67+
}
68+
69+
return $data
70+
}
71+
72+
function Test-TableStorage {
73+
param(
74+
[array]$TestData,
75+
[string]$TestName
76+
)
77+
78+
Write-Host "`n[TABLE STORAGE] Testing $TestName..." -ForegroundColor Yellow
79+
80+
$tableName = "TestBackup$(Get-Random -Maximum 100000)"
81+
$Table = Get-CippTable -tablename $tableName
82+
83+
$jsonString = $TestData | ConvertTo-Json -Depth 100 -Compress
84+
$jsonSizeKB = [math]::Round(($jsonString | Measure-Object -Character).Characters / 1KB, 2)
85+
86+
Write-Host " JSON Size: $jsonSizeKB KB"
87+
88+
# Time the storage operation
89+
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
90+
try {
91+
$entity = @{
92+
PartitionKey = 'TestBackup'
93+
RowKey = $TestName
94+
Backup = [string]$jsonString
95+
}
96+
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force -ErrorAction Stop
97+
$stopwatch.Stop()
98+
99+
Write-Host " Write Time: $($stopwatch.ElapsedMilliseconds)ms" -ForegroundColor Green
100+
Write-Host ' Status: Success ✓' -ForegroundColor Green
101+
102+
return @{
103+
Method = 'Table Storage'
104+
TestName = $TestName
105+
Size = $jsonSizeKB
106+
WriteTime = $stopwatch.ElapsedMilliseconds
107+
Success = $true
108+
Details = "Stored in table '$tableName'"
109+
}
110+
} catch {
111+
$stopwatch.Stop()
112+
Write-Host " Status: Failed ✗ - $($_.Exception.Message)" -ForegroundColor Red
113+
114+
return @{
115+
Method = 'Table Storage'
116+
TestName = $TestName
117+
Size = $jsonSizeKB
118+
WriteTime = $stopwatch.ElapsedMilliseconds
119+
Success = $false
120+
Details = $_.Exception.Message
121+
}
122+
}
123+
}
124+
125+
function Test-BlobStorage {
126+
param(
127+
[array]$TestData,
128+
[string]$TestName
129+
)
130+
131+
Write-Host "`n[BLOB STORAGE] Testing $TestName..." -ForegroundColor Yellow
132+
133+
$containerName = 'test-backup-comparison'
134+
$blobName = "backup_$TestName`_$(Get-Random -Maximum 100000).json"
135+
136+
$jsonString = $TestData | ConvertTo-Json -Depth 100 -Compress
137+
$jsonSizeKB = [math]::Round(($jsonString | Measure-Object -Character).Characters / 1KB, 2)
138+
139+
Write-Host " JSON Size: $jsonSizeKB KB"
140+
141+
try {
142+
# Ensure container exists
143+
$containers = @()
144+
try {
145+
$containers = New-CIPPAzStorageRequest -Service 'blob' -Component 'list' -ConnectionString $ConnectionString
146+
} catch { $containers = @() }
147+
148+
$exists = ($containers | Where-Object { $_.Name -eq $containerName }) -ne $null
149+
if (-not $exists) {
150+
Write-Host " Creating container '$containerName'..." -ForegroundColor Gray
151+
$null = New-CIPPAzStorageRequest -Service 'blob' -Resource $containerName -Method 'PUT' -QueryParams @{ restype = 'container' } -ConnectionString $ConnectionString
152+
Start-Sleep -Milliseconds 500
153+
}
154+
155+
# Time the upload operation
156+
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
157+
$null = New-CIPPAzStorageRequest -Service 'blob' -Resource "$containerName/$blobName" -Method 'PUT' -ContentType 'application/json; charset=utf-8' -Body $jsonString -ConnectionString $ConnectionString
158+
$stopwatch.Stop()
159+
160+
Write-Host " Write Time: $($stopwatch.ElapsedMilliseconds)ms" -ForegroundColor Green
161+
Write-Host ' Status: Success ✓' -ForegroundColor Green
162+
Write-Host " Location: $containerName/$blobName" -ForegroundColor Gray
163+
164+
return @{
165+
Method = 'Blob Storage'
166+
TestName = $TestName
167+
Size = $jsonSizeKB
168+
WriteTime = $stopwatch.ElapsedMilliseconds
169+
Success = $true
170+
Details = "$containerName/$blobName"
171+
}
172+
} catch {
173+
$stopwatch.Stop()
174+
Write-Host " Status: Failed ✗ - $($_.Exception.Message)" -ForegroundColor Red
175+
176+
return @{
177+
Method = 'Blob Storage'
178+
TestName = $TestName
179+
Size = $jsonSizeKB
180+
WriteTime = $stopwatch.ElapsedMilliseconds
181+
Success = $false
182+
Details = $_.Exception.Message
183+
}
184+
}
185+
}
186+
187+
# Run tests
188+
$results = @()
189+
$configsToRun = if ($TestSize -eq 'All') { $testConfigs } else { $testConfigs | Where-Object { $_.Name -eq $TestSize } }
190+
191+
foreach ($config in $configsToRun) {
192+
Write-Host "`n`n$($config.Description)" -ForegroundColor Magenta
193+
Write-Host "Generating test data ($($config.ItemCount) items, $($config.PropertiesPerItem) properties)..." -ForegroundColor Gray
194+
195+
$testData = Generate-TestData -ItemCount $config.ItemCount -PropertiesPerItem $config.PropertiesPerItem -Type "Backup_$($config.Name)"
196+
197+
# Test table storage
198+
$tableResult = Test-TableStorage -TestData $testData -TestName $config.Name
199+
$results += $tableResult
200+
201+
Start-Sleep -Milliseconds 500
202+
203+
# Test blob storage
204+
$blobResult = Test-BlobStorage -TestData $testData -TestName $config.Name
205+
$results += $blobResult
206+
}
207+
208+
# Summary
209+
Write-Host "`n`n================================" -ForegroundColor Cyan
210+
Write-Host 'Test Summary' -ForegroundColor Cyan
211+
Write-Host '================================' -ForegroundColor Cyan
212+
213+
$results | Group-Object -Property TestName | ForEach-Object {
214+
$testGroup = $_
215+
Write-Host "`n$($testGroup.Name):" -ForegroundColor Magenta
216+
217+
$testGroup.Group | ForEach-Object {
218+
$status = if ($_.Success) { '' } else { '' }
219+
Write-Host " $($_.Method): $($_.Size)KB | Write: $($_.WriteTime)ms | $status" -ForegroundColor $(if ($_.Success) { 'Green' } else { 'Red' })
220+
}
221+
}
222+
223+
# Detailed comparison
224+
Write-Host "`n`n================================" -ForegroundColor Cyan
225+
Write-Host 'Performance Comparison' -ForegroundColor Cyan
226+
Write-Host '================================' -ForegroundColor Cyan
227+
228+
$results | Group-Object -Property TestName | ForEach-Object {
229+
$testGroup = $_
230+
$tableResult = $testGroup.Group | Where-Object { $_.Method -eq 'Table Storage' }
231+
$blobResult = $testGroup.Group | Where-Object { $_.Method -eq 'Blob Storage' }
232+
233+
if ($tableResult -and $blobResult -and $tableResult.Success -and $blobResult.Success) {
234+
$timeDiff = $blobResult.WriteTime - $tableResult.WriteTime
235+
$timePercentage = [math]::Round(($timeDiff / $tableResult.WriteTime) * 100, 2)
236+
237+
Write-Host "`n$($testGroup.Name):" -ForegroundColor Magenta
238+
Write-Host " Table Write Time: $($tableResult.WriteTime)ms" -ForegroundColor Gray
239+
Write-Host " Blob Write Time: $($blobResult.WriteTime)ms" -ForegroundColor Gray
240+
241+
if ($timeDiff -gt 0) {
242+
Write-Host " Blob is $($timeDiff)ms slower ($($timePercentage)% slower)" -ForegroundColor Yellow
243+
} else {
244+
Write-Host " Blob is $((-$timeDiff))ms faster ($($(-$timePercentage))% faster)" -ForegroundColor Green
245+
}
246+
}
247+
}
248+
249+
Write-Host "`n`nTest Complete!" -ForegroundColor Green

0 commit comments

Comments
 (0)