User Tools

Site Tools


obtener_rendimiento_de_lectura_del_disco

Disk Benchmark Script Using PowerShell

This PowerShell script benchmarks the read speed of the C: drive by creating a 1 GB file, reading it multiple times, and calculating the average speed. The results, including date, time, hostname, and average speed, are saved to a CSV file.

Script Details

  1. File Creation and Speed Measurement: The script creates a 1 GB file and measures the read speed multiple times.
  2. Gathering Additional Information: It captures the current date and time, and the hostname of the computer.
  3. Creating and Saving the Result Object: A custom object with `DateTime`, `Hostname`, and `AvgSpeedMBps` is created.
  4. CSV File Handling:
    1. If the CSV file already exists, the script appends the new data.
    2. If the CSV file does not exist, the script creates a new CSV file and writes the data.

PowerShell Script

# Define the file path and size
$filePath = "C:\temp\testfile.tmp"
$fileSizeGB = 1
$fileSizeBytes = $fileSizeGB * 1024 * 1024 * 1024
 
# Create the file with random content
$buffer = New-Object byte[] $fileSizeBytes
$random = [System.Random]::new()
$random.NextBytes($buffer)
[System.IO.File]::WriteAllBytes($filePath, $buffer)
 
# Number of times to read the file for averaging
$n = 5
$speeds = @()
 
for ($i = 1; $i -le $n; $i++) {
    # Measure the time to read the file
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    $content = [System.IO.File]::ReadAllBytes($filePath)
    $sw.Stop()
    $timeTaken = $sw.Elapsed.TotalSeconds
 
    # Calculate speed in MB/s
    $speedMBps = ($fileSizeBytes / (1024 * 1024)) / $timeTaken
    $speeds += $speedMBps
 
    Write-Host "Read $i $speedMBps MB/s"
}
 
# Calculate and display the average speed
$averageSpeed = ($speeds | Measure-Object -Average).Average
Write-Host "Average read speed: $averageSpeed MB/s"
 
# Clean up by deleting the test file
Remove-Item $filePath
 
# Gather additional information
$dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$hostname = $env:COMPUTERNAME
 
# Create an object with the data to be saved
$result = [PSCustomObject]@{
    DateTime   = $dateTime
    Hostname   = $hostname
    AvgSpeedMBps = $averageSpeed
}
 
# Define the CSV file path
$csvFilePath = "C:\temp\disk_benchmark_results.csv"
 
# Check if the CSV file exists
if (Test-Path $csvFilePath) {
    # Append the data to the existing CSV file
    $result | Export-Csv -Path $csvFilePath -Append -NoTypeInformation
} else {
    # Create a new CSV file and save the data
    $result | Export-Csv -Path $csvFilePath -NoTypeInformation
}

How to Run the Script

  1. Open PowerShell as an administrator.
  2. Copy and paste the script into the PowerShell window.
  3. Press `Enter` to execute.

This will create or append to a CSV file at `C:\temp\disk_benchmark_results.csv` with the date, time, hostname, and average read speed.

obtener_rendimiento_de_lectura_del_disco.txt · Last modified: 2024/10/17 21:42 by 127.0.0.1