timeout_probe.ps1 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. param(
  2. [string]$Url = 'http://127.0.0.1:8099/',
  3. [int]$TimeoutMs = 0,
  4. [string]$Body = '{"params":[],"code":"99070061"}'
  5. )
  6. # Reproduces the request-issuing logic of HttpServices.cs Post() so that the
  7. # measured timeout is the same one UppPay would experience in production.
  8. $ErrorActionPreference = 'Stop'
  9. $u = [Uri]$Url
  10. $req = [System.Net.HttpWebRequest]::Create($u)
  11. $req.Method = 'POST'
  12. $req.ContentType = 'application/json'
  13. $req.AllowAutoRedirect = $true
  14. $req.MaximumAutomaticRedirections = 1
  15. $bytes = [System.Text.Encoding]::UTF8.GetBytes($Body)
  16. $req.ContentLength = $bytes.Length
  17. if ($TimeoutMs -gt 0) {
  18. $req.Timeout = $TimeoutMs
  19. $req.ReadWriteTimeout = $TimeoutMs
  20. Write-Host "mode : Timeout EXPLICITLY set"
  21. } else {
  22. Write-Host "mode : Timeout NOT set (exactly like UppPay today)"
  23. }
  24. Write-Host ("effective Timeout = {0} ms ({1:N1} s)" -f $req.Timeout, ($req.Timeout / 1000))
  25. Write-Host ("effective ReadWriteTimeout = {0} ms" -f $req.ReadWriteTimeout)
  26. Write-Host ("target : {0}" -f $Url)
  27. Write-Host ("body : {0}" -f $Body)
  28. Write-Host "-" * 70
  29. $sw = [System.Diagnostics.Stopwatch]::StartNew()
  30. try {
  31. $s = $req.GetRequestStream()
  32. $s.Write($bytes, 0, $bytes.Length)
  33. $s.Close()
  34. $resp = $req.GetResponse()
  35. $sr = New-Object System.IO.StreamReader($resp.GetResponseStream(), [System.Text.Encoding]::UTF8)
  36. $txt = $sr.ReadToEnd()
  37. $sr.Close(); $resp.Close()
  38. $sw.Stop()
  39. Write-Host ("RESULT: SUCCESS after {0:N2} s http={1}" -f $sw.Elapsed.TotalSeconds, [int]$resp.StatusCode)
  40. Write-Host ("BODY : {0}" -f $txt)
  41. } catch {
  42. $sw.Stop()
  43. Write-Host ("RESULT: FAILED after {0:N2} s" -f $sw.Elapsed.TotalSeconds)
  44. Write-Host "exception chain:"
  45. $ex = $_.Exception
  46. $level = 0
  47. while ($null -ne $ex -and $level -lt 6) {
  48. Write-Host (" [{0}] {1}" -f $level, $ex.GetType().FullName)
  49. Write-Host (" Message : {0}" -f $ex.Message)
  50. if ($ex -is [System.Net.WebException]) {
  51. Write-Host (" Status : {0}" -f $ex.Status)
  52. }
  53. $ex = $ex.InnerException
  54. $level++
  55. }
  56. }
  57. Write-Host ("elapsed = {0:N2} s" -f $sw.Elapsed.TotalSeconds)