Last active
July 30, 2023 04:09
-
-
Save ericwastaken/f4d79ade2ef4f37831abd68cf1b04552 to your computer and use it in GitHub Desktop.
Powershell Request Post Benchmark (Powershell CURL POST benchmark alternative)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################## | |
# ps-post-benchmark.ps1 | |
# | |
# A powershell script to make a POST to a remote URI | |
# and measure the response time. | |
# | |
# Useful when a client only has Powershell. | |
# | |
# Copyright 2023 eric@issfl.com | |
# | |
# Permission is hereby granted, free of charge, to any person | |
# obtaining a copy of this software and associated documentation | |
# files (the "Software"), to deal in the Software without | |
# restriction, including without limitation the rights to use, | |
# copy, modify, merge, publish, distribute, sublicense, and/or | |
# sell copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following | |
# conditions: | |
# | |
# The above copyright notice and this permission notice shall | |
# be included in all copies or substantial portions of the | |
# Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | |
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
########################################################## | |
# Set the URI to connect to | |
$uri = "https://some-host.local" | |
# Set the full path to a log file | |
$outfilePath = "C:\Users\eric\Documents\request-log.txt" | |
# Set request headers | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("Content-Type", "application/json") | |
$headers.Add("Authorization", "Bearer some-token") | |
# Get the current timestamp | |
$timestamp = Get-Date | |
# Epoch Step 1: Get the current DateTimeOffset | |
$dateTimeOffset = [System.DateTimeOffset]::Now | |
# Epoich Step 2: Calculate the Unix Epoch time (number of seconds since January 1, 1970) | |
$epochTime = $dateTimeOffset.ToUnixTimeMilliseconds() | |
# Set request body | |
$body = @" | |
{ | |
`"param1`": `"some-val-1`", | |
`"param2`": `"some-val-2`", | |
`"timestamp`": $epochTime | |
} | |
"@ | |
# Ignore certificate validation errors (so that we can use self-signed certs on the remote) | |
# Comment this out if connecting to a server with a trusted cert! | |
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } | |
# Create a new WebClient object | |
$webClient = New-Object System.Net.WebClient | |
# Set the headers into the webClient | |
foreach ($header in $headers.Keys) { | |
$webClient.Headers.Add($header, $headers[$header]) | |
} | |
# Convert the body to bytes | |
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body) | |
try { | |
# Make the request while measuring timeElapsed | |
$elapsedTime = MEasure-Command { | |
# Make the HTTP POST request | |
$responseBytes = $webClient.UploadData($uri, 'POST', $bodyBytes) | |
# Convert the response bytes to a string | |
$responseString = [System.Text.Encoding]::UTF8.GetString($responseBytes) | |
} | |
# Write values to the request log file | |
"$timestamp|| $epochTime|| OK|| $($elapsedTime.TotalSeconds)" | Out-File -FilePath $outfilePath -Append | |
} catch { | |
# Write values to the request log file | |
"$timestamp|| $epochTime|| NK $($_.Exception.Message)|| 0" | Out-File -FilePath $outfilePath -Append | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment