Skip to content

Instantly share code, notes, and snippets.

@Victor1890
Created October 28, 2024 19:42
Show Gist options
  • Select an option

  • Save Victor1890/bb7e246bb5e533d4d54b39d59c82d211 to your computer and use it in GitHub Desktop.

Select an option

Save Victor1890/bb7e246bb5e533d4d54b39d59c82d211 to your computer and use it in GitHub Desktop.

Revisions

  1. Victor1890 created this gist Oct 28, 2024.
    131 changes: 131 additions & 0 deletions stress-test-with-oha.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    # Parameters for load testing (adjust as needed)
    $duration = "30s" # Duration for the test
    $concurrentConnections = 50 # Number of concurrent connections
    $queryPerSecond = 10 # Queries per second

    # Parameters for load testing (adjust as needed)
    $duration = "30s" # Duration for the test
    $concurrentConnections = 50 # Number of concurrent connections
    $queryPerSecond = 10 # Queries per second

    # Generate the log file name using the current timestamp
    $currentDateTime = Get-Date
    $second = $currentDateTime.ToString("yyyyMMddHHmmss") # Current timestamp in seconds (formatted)

    # Get the current working directory
    $currentDir = Get-Location

    # Define the relative path for the log file
    $relativeLogDir = Join-Path -Path $currentDir.Path -ChildPath "..\log\stress-test"

    # Ensure the directory exists
    if (-not (Test-Path -Path $relativeLogDir)) {
    New-Item -ItemType Directory -Path $relativeLogDir -Force | Out-Null
    Write-Output "Created directory: $relativeLogDir"
    }

    # Combine the relative directory with the log file name
    $logFilePath = Join-Path -Path $relativeLogDir -ChildPath "$second.stress.log.txt" # Combine to form log file path

    # Function to check if `oha` is installed and install it if not
    function CheckOha {
    # Check if `oha` command is available
    if (!(Get-Command "oha" -ErrorAction SilentlyContinue)) {
    Write-Output "`oha` command not found. Installing oha via winget..."

    # Install `oha` using winget
    winget install -e --id hatoo.oha

    # Refresh the PATH in this session if oha was added
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)

    Write-Output "Installation completed. `oha` is now available."
    }
    else {
    Write-Output "`oha` is already installed."
    }
    }

    CheckOha

    # Define the list of requests to be executed
    $requests = @(
    @{
    URL = "http://127.0.0.1:4000/api/client/countries"
    Method = "GET"
    # Headers = "foo: bar"
    # Query = "param1=value1&param2=value2"
    },
    @{
    URL = "http://127.0.0.1:4000/api/client/branch-offices"
    Method = "GET"
    # Headers = "foo: bar"
    # Query = "param1=value1&param2=value2"
    },
    @{
    URL = "http://127.0.0.1:4000/api/client/additional-services"
    Method = "GET"
    # Headers = "foo: bar"
    # Query = "param1=value1&param2=value2"
    }
    # @{
    # URL = "http://localhost:4000"
    # Method = "POST"
    # Headers = "foo: bar"
    # Body = @{
    # key = "1"
    # nestedKey = @{
    # key1 = "value"
    # }
    # } # Nested JSON body as a PowerShell hashtable
    # },
    # @{
    # URL = "http://localhost:4000"
    # Method = "PUT"
    # Headers = "foo: bar"
    # Body = @{
    # updateKey = "newValue"
    # anotherNestedKey = @{
    # subKey = "subValue"
    # }
    # } # Another nested JSON example
    # }
    )

    # Loop through each request and execute using Oha
    foreach ($request in $requests) {

    $ohaCommand = "oha $($request.URL) -z $duration -c $concurrentConnections -q $queryPerSecond --latency-correction --disable-keepalive -m $($request.Method) -T application/json -j"

    # Add specific arguments based on the HTTP method
    if ($request.Method -eq "GET") {
    if ($request.Query) {
    # Append query string to the URL if provided
    $ohaCommand = "oha `"$($request.URL)?$($request.Query)`" -n $numberOfRequests -c $concurrentConnections -m GET"
    }
    }
    elseif ($request.Method -eq "POST" -or $request.Method -eq "PUT" -or $request.Method -eq "PATCH") {
    if ($request.Body) {
    $jsonBody = ($request.Body | ConvertTo-Json -Compress) -replace '"', '\"'
    $ohaCommand += " -d `"$jsonBody`""
    }
    }
    else {
    Write-Output "Unsupported HTTP method: $($request.Method)"
    continue
    }

    if ($request.Headers) {
    $ohaCommand += " -H `"$($request.Headers)`""
    }

    # Display and execute the command
    Write-Output "Executing: $ohaCommand"
    Invoke-Expression $ohaCommand

    # Execute the command and capture the output
    $output = Invoke-Expression $ohaCommand

    # Append the output to the log file
    Add-Content -Path $logFilePath -Value $output
    }