Last active
March 2, 2025 15:25
-
-
Save bixb0012/b8d0aec5bfc40773bc98c9b56aff5335 to your computer and use it in GitHub Desktop.
PowerShell: Measure Copy Performance
This file contains hidden or 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
#Requires -Version 5.1 | |
# Reference: 1) https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist | |
# Reference: 2) https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch | |
# Reference: 3) https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream | |
# Example 1: Copy file and measure combined (read & write) transfer rate over time | |
$FilePath = "" # Path of the file to copy | |
$Destination= "" # Path to the new directory or folder | |
$BufferSize = 64 # Buffer size, in KB, for copying file | |
$Buffer = [byte[]]::New(1024 * $BufferSize) | |
$CumulativeBytes = [Collections.Generic.List[int32]]::New() | |
$CumulativeTicks = [Collections.Generic.List[int64]]::New() | |
$Stopwatch = [Diagnostics.Stopwatch]::New() | |
$NewPath = Join-Path -Path $Destination -ChildPath (Split-Path -Path $FilePath -Leaf) | |
if ($InStream = [IO.File]::OpenRead($FilePath)) { | |
if ($OutStream = [IO.File]::Create($NewPath)) { | |
$Stopwatch.Start() | |
while ($BytesRead = $InStream.Read($Buffer, 0, $Buffer.Length)) { | |
$OutStream.Write($Buffer, 0, $BytesRead) | |
$CumulativeTicks.Add($Stopwatch.ElapsedTicks) | |
$CumulativeBytes.Add($OutStream.Length) | |
} | |
$Stopwatch.Stop() | |
$OutStream.Dispose() | |
} | |
$InStream.Dispose() | |
} | |
$AverageMbps = $CumulativeBytes[-1] * 8 / 1MB / | |
([TimeSpan]$CumulativeTicks[-1]).TotalSeconds | |
# Example 2: Copy file and measure read & write transfer rates over time | |
$FilePath = "" # Path of the file to copy | |
$Destination= "" # Path to the new directory or folder | |
$BufferSize = 64 # Buffer size, in KB, for copying file | |
$Buffer = [byte[]]::New(1024 * $BufferSize) | |
$StreamBytes = [Collections.Generic.List[int32]]::New() | |
$ReadTicks = [Collections.Generic.List[int64]]::New() | |
$WriteTicks = [Collections.Generic.List[int64]]::New() | |
$Stopwatch = [Diagnostics.Stopwatch]::New() | |
$NewPath = Join-Path -Path $Destination -ChildPath (Split-Path -Path $FilePath -Leaf) | |
if ($InStream = [IO.File]::OpenRead($FilePath)) { | |
if ($OutStream = [IO.File]::Create($NewPath)) { | |
$Stopwatch.Start() | |
while ($BytesRead = $InStream.Read($Buffer, 0, $Buffer.Length)) { | |
$Stopwatch.Stop() | |
$StreamBytes.Add($BytesRead) | |
$ReadTicks.Add($Stopwatch.ElapsedTicks) | |
$Stopwatch.Reset() | |
$Stopwatch.Start() | |
$OutStream.Write($Buffer, 0, $BytesRead) | |
$Stopwatch.Stop() | |
$WriteTicks.Add($Stopwatch.ElapsedTicks) | |
$Stopwatch.Reset() | |
$Stopwatch.Start() | |
} | |
$Stopwatch.Stop() | |
$OutStream.Dispose() | |
} | |
$InStream.Dispose() | |
} | |
$AverageReadMbps = [Linq.Enumerable]::Sum($StreamBytes) * 8 / 1MB / | |
([TimeSpan][Linq.Enumerable]::Sum($ReadTicks)).TotalSeconds | |
$AverageWriteMbps = [Linq.Enumerable]::Sum($StreamBytes) * 8 / 1MB / | |
([TimeSpan][Linq.Enumerable]::Sum($WriteTicks)).TotalSeconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment