|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Format the result of an iPerf JSON results file. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +The Parse-iPerfResults.ps1 script formats the results of an iPerf results file. |
| 7 | +With iPerf you can output the results to JSON using the "-J" flag. |
| 8 | +Output all results with "-J" to a concatenated file. |
| 9 | +
|
| 10 | +This script will pull each of the discrete results into a JSON array, |
| 11 | +and then output relevant fields to a CSV file. |
| 12 | +
|
| 13 | +.NOTES |
| 14 | +Written by Steven Tong for community usage |
| 15 | +GitHub: stevenctong |
| 16 | +Date: 10/15/21 |
| 17 | +
|
| 18 | +.EXAMPLE |
| 19 | +./Parse-iPerfResults.ps1 -file <input_filename.json> |
| 20 | +Takes <filename> as the input and outputs it to a file with the same name but as .csv. |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +./Parse-iPerfResults.ps1 -file <input_filename.json> -output <CSV_output_filename.csv> |
| 24 | +Takes <filename> as the input and outputs it to <output> filename. |
| 25 | +#> |
| 26 | + |
| 27 | +param ( |
| 28 | + [CmdletBinding()] |
| 29 | + |
| 30 | + # Input filename of iPerf results in a JSON array |
| 31 | + [Parameter(Mandatory=$true)] |
| 32 | + [string]$file = '', |
| 33 | + |
| 34 | + # Output filename of the formatted iPerf results as a CSV file |
| 35 | + [Parameter(Mandatory=$false)] |
| 36 | + [string]$output = '' |
| 37 | +) |
| 38 | + |
| 39 | +# CreateJSONArray - Takes in JSON file with a bunch of separate JSON objects |
| 40 | +# Combines the separate objects and returns and array of JSON objects |
| 41 | +Function CreateJSONArray($jsonFile) { |
| 42 | + $jsonCount = 0 |
| 43 | + $textObject = "" |
| 44 | + $jsonArray = @() |
| 45 | + |
| 46 | + [System.IO.File]::ReadLines($jsonFile) | ForEach-Object { |
| 47 | + if ($_ -match '{') { |
| 48 | + $jsonCount += 1 |
| 49 | + } |
| 50 | + |
| 51 | + if ($_ -match '}') { |
| 52 | + $jsonCount -= 1 |
| 53 | + } |
| 54 | + |
| 55 | + if ($jsonCount -gt 0) |
| 56 | + { |
| 57 | + $textObject += $_ |
| 58 | + } else { |
| 59 | + $textObject += $_ |
| 60 | + $jsonArray += $textObject | ConvertFrom-Json |
| 61 | + $textObject = "" |
| 62 | + } |
| 63 | + } |
| 64 | + return $jsonArray |
| 65 | +} # FUNCTION - CreateJSONArray($jsonFile) |
| 66 | + |
| 67 | + |
| 68 | +# Pass in the iPerf results file and put each JSON object into an array for processing |
| 69 | +$jsonArray = CreateJSONArray $file |
| 70 | + |
| 71 | +# If the file is already an array of JSON objects you can use the following |
| 72 | +# $jsonArray = Get-Content -Path $file | ConvertFrom-Json |
| 73 | + |
| 74 | +$resultList = @() |
| 75 | + |
| 76 | +foreach ($test in $jsonArray) |
| 77 | +{ |
| 78 | + # If iPerf was run with the -r (reverse) flag, swap source & target |
| 79 | + if ($test.start.test_start.reverse -eq 1) { |
| 80 | + $source = $test.start.connected[0].remote_host |
| 81 | + $target = $test.start.connected[0].local_host |
| 82 | + $note = "Source/Target swapped" |
| 83 | + } else { |
| 84 | + $source = $test.start.connected[0].local_host |
| 85 | + $target = $test.start.connected[0].remote_host |
| 86 | + $note = "" |
| 87 | + } |
| 88 | + |
| 89 | + $gbps = [math]::round($test.end.sum_sent.bits_per_second / 1000/1000/1000, 2) |
| 90 | + |
| 91 | + $timesecs = $test.start.timestamp.timesecs |
| 92 | + $localTime = [DateTime]$test.start.timestamp.time |
| 93 | + $localTZ = Get-TimeZone | Select -ExpandProperty DisplayName |
| 94 | + |
| 95 | + $result = [PSCustomObject] @{ |
| 96 | + "Source" = $source |
| 97 | + "Target" = $target |
| 98 | + "Gbps" = $gbps |
| 99 | + "num_streams" = $test.start.test_start.num_streams |
| 100 | + "blksize" = $test.start.test_start.blksize |
| 101 | + "duration_secs" = $test.start.test_start.duration |
| 102 | + "reverse" = $test.start.test_start.reverse |
| 103 | + "tcp_mss_default" = $test.start.tcp_mss_default |
| 104 | + "sum_sent_bytes" = $test.end.sum_sent.bytes |
| 105 | + "sum_sent_bps" = $test.end.sum_sent.bits_per_second |
| 106 | + "sum_sent_retransmits" = $test.end.sum_sent.retransmits |
| 107 | + "host_cpu_total" = $test.end.cpu_utilization_percent.host_total |
| 108 | + "remote_cpu_total" = $test.end.cpu_utilization_percent.remote_total |
| 109 | + "note" = $note |
| 110 | + "time_local" = $localTime |
| 111 | + "time_local_zone" = $localTZ |
| 112 | + "time_utc" = $test.start.timestamp.time |
| 113 | + } |
| 114 | + |
| 115 | + $resultList += $result |
| 116 | +} |
| 117 | + |
| 118 | +# If no output filename is given, use the input filename as the base output filename |
| 119 | +if ($output -eq '') |
| 120 | +{ |
| 121 | + $resultList | Export-CSV -Path "$(Split-Path $file -Leafbase).csv" |
| 122 | +} else { |
| 123 | + $resultList | Export-CSV -Path $output |
| 124 | +} |
0 commit comments