Home → PERFORMANCE → PowerShell Curl Performance Testing Script
PowerShell Curl Performance Testing Script
PowerShell script for testing website performance using curl with session cookie support on Windows.
Usage
.\curl_performance_script.ps1 -Url "https://your-site.com" [-CookieFile "cookies.txt"]
How to Get Session Cookies from Browser
Chrome/Edge
- Login to your SSO site in browser
- Press F12 to open DevTools
- Go to Application tab > Storage section > Cookies
- Click on your domain name
- Find session/auth cookies (usually named: session_id, JSESSIONID, auth_token, etc.)
- Copy the Value (just the long string)
- Save to file as raw value:
abc123def456789xyz
Firefox
- Login to your SSO site in browser
- Press F12 to open DevTools
- Go to Storage tab > Cookies
- Select your domain from the list
- Copy the session cookie value
- Save to file as raw value
Example Cookie File Content
Just paste the raw cookie value from browser DevTools:
abc123def456789xyz
Script Code
# PowerShell Curl Performance Testing Script with Session Cookie Support
# Usage: .\curl_performance_script.ps1 -Url "https://your-site.com" [-CookieFile "cookies.txt"]
param(
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$false)]
[string]$CookieFile = "cookies.txt"
)
# Check if curl is available
if (-not (Get-Command curl -ErrorAction SilentlyContinue)) {
Write-Host "❌ Error: curl not found. Please install curl or use Windows 10+ which includes curl.exe" -ForegroundColor Red
exit 1
}
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$ResultsFile = "performance_$timestamp.log"
Write-Host "=== CURL PERFORMANCE TEST ===" | Tee-Object -FilePath $ResultsFile
Write-Host "URL: $Url" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Cookie File: $CookieFile" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Timestamp: $(Get-Date)" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "=========================" | Tee-Object -FilePath $ResultsFile -Append
if (-not (Test-Path $CookieFile)) {
Write-Host "⚠ Cookie file not found: $CookieFile" -ForegroundColor Yellow
Write-Host "Creating empty cookie file. Add your session cookie value manually."
New-Item -ItemType File -Path $CookieFile -Force | Out-Null
}
function Run-PerformanceTest {
param([int]$TestNum)
Write-Host "=== TEST $TestNum ===" | Tee-Object -FilePath $ResultsFile -Append
# Read cookie value if file exists and has content
$cookieValue = ""
if ((Test-Path $CookieFile) -and (Get-Content $CookieFile -Raw).Trim()) {
$cookieValue = (Get-Content $CookieFile -Raw).Trim()
}
$curlArgs = @(
"-w"
("DNS Lookup: %{time_namelookup}s`n" +
"TCP Connect: %{time_connect}s`n" +
"SSL Handshake: %{time_appconnect}s`n" +
"Pre-transfer: %{time_pretransfer}s`n" +
"Redirect Time: %{time_redirect}s`n" +
"Time to First Byte: %{time_starttransfer}s`n" +
"Total Time: %{time_total}s`n" +
"Download Speed: %{speed_download} bytes/sec`n" +
"Upload Speed: %{speed_upload} bytes/sec`n" +
"Size Downloaded: %{size_download} bytes`n" +
"Size Uploaded: %{size_upload} bytes`n" +
"Size Request: %{size_request} bytes`n" +
"Size Header: %{size_header} bytes`n" +
"HTTP Response Code: %{http_code}`n" +
"Redirect Count: %{num_redirects}`n" +
"Final URL: %{url_effective}`n" +
"Content Type: %{content_type}`n" +
"Remote IP: %{remote_ip}`n" +
"SSL Verify Result: %{ssl_verify_result}`n`n")
"-H"
"User-Agent: Mozilla/5.0 (compatible; PerformanceTest/1.0)"
"-o"
"$env:TEMP\response_$TestNum.html"
"-s"
"-L"
"-f"
)
# Add cookie header if we have a cookie value
if ($cookieValue) {
$curlArgs += @("-H", "Cookie: session=$cookieValue")
}
$curlArgs += $Url
try {
$result = & curl @curlArgs 2>&1
$result | Tee-Object -FilePath $ResultsFile -Append
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ Test $TestNum failed" -ForegroundColor Red | Tee-Object -FilePath $ResultsFile -Append
}
}
catch {
Write-Host "❌ Test $TestNum failed: $($_.Exception.Message)" -ForegroundColor Red | Tee-Object -FilePath $ResultsFile -Append
}
Write-Host "" | Tee-Object -FilePath $ResultsFile -Append
}
Write-Host "Running performance tests..." | Tee-Object -FilePath $ResultsFile -Append
# Run 5 tests
for ($i = 1; $i -le 5; $i++) {
Run-PerformanceTest -TestNum $i
Start-Sleep -Seconds 2
}
Write-Host "=== CALCULATING AVERAGES ===" | Tee-Object -FilePath $ResultsFile -Append
# Extract timing data
$content = Get-Content $ResultsFile -Raw
$dnsMatches = [regex]::Matches($content, "DNS Lookup:\s+(\d+\.?\d*)s")
$connectMatches = [regex]::Matches($content, "TCP Connect:\s+(\d+\.?\d*)s")
$sslMatches = [regex]::Matches($content, "SSL Handshake:\s+(\d+\.?\d*)s")
$ttfbMatches = [regex]::Matches($content, "Time to First Byte:\s+(\d+\.?\d*)s")
$totalMatches = [regex]::Matches($content, "Total Time:\s+(\d+\.?\d*)s")
$sizeMatches = [regex]::Matches($content, "Size Downloaded:\s+(\d+)")
$speedMatches = [regex]::Matches($content, "Download Speed:\s+(\d+)")
function Calculate-Average {
param([System.Text.RegularExpressions.MatchCollection]$Matches)
if ($Matches.Count -eq 0) { return 0 }
$sum = ($Matches | ForEach-Object { [double]$_.Groups[1].Value }) | Measure-Object -Sum
return [math]::Round($sum.Sum / $Matches.Count, 3)
}
if ($dnsMatches.Count -gt 0) {
$avgDns = Calculate-Average -Matches $dnsMatches
$avgConnect = Calculate-Average -Matches $connectMatches
$avgSsl = Calculate-Average -Matches $sslMatches
$avgTtfb = Calculate-Average -Matches $ttfbMatches
$avgTotal = Calculate-Average -Matches $totalMatches
$avgSize = Calculate-Average -Matches $sizeMatches
$avgSpeed = Calculate-Average -Matches $speedMatches
Write-Host "Average DNS Lookup: ${avgDns}s" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average TCP Connect: ${avgConnect}s" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average SSL Handshake: ${avgSsl}s" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average Time to First Byte: ${avgTtfb}s" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average Total Time: ${avgTotal}s" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average Download Size: ${avgSize} bytes" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Average Download Speed: ${avgSpeed} bytes/sec" | Tee-Object -FilePath $ResultsFile -Append
}
Write-Host "" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "=== PERFORMANCE SUMMARY ===" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Total Tests: 5" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Results saved to: $ResultsFile" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Session cookie file: $CookieFile" | Tee-Object -FilePath $ResultsFile -Append
# Performance warnings
if ($avgTotal -and $avgTotal -gt 3.0) {
Write-Host "⚠ WARNING: Average load time exceeds 3 seconds" -ForegroundColor Yellow | Tee-Object -FilePath $ResultsFile -Append
}
if ($content -match "HTTP Response Code:\s+[45]\d\d") {
Write-Host "⚠ WARNING: HTTP errors detected" -ForegroundColor Yellow | Tee-Object -FilePath $ResultsFile -Append
}
if ((Test-Path $CookieFile) -and (Get-Content $CookieFile -Raw).Trim()) {
Write-Host "✓ Session cookie available" -ForegroundColor Green | Tee-Object -FilePath $ResultsFile -Append
} else {
Write-Host "⚠ No session cookie found" -ForegroundColor Yellow | Tee-Object -FilePath $ResultsFile -Append
}
Write-Host "" | Tee-Object -FilePath $ResultsFile -Append
Write-Host "Performance test completed: $(Get-Date)" | Tee-Object -FilePath $ResultsFile -Append
Write-Host ""
Write-Host "Results summary:" -ForegroundColor Cyan
Write-Host "- Detailed log: $ResultsFile" -ForegroundColor White
Write-Host "- Session cookie file: $CookieFile" -ForegroundColor White
if ($avgTotal) {
Write-Host "- Average total time: $($avgTotal)s" -ForegroundColor White
}
Last updated: 2025-08-26 20:00 UTC