Calculating hard disk bandwidth
My application generates a 2 GB file and you need to select the fastest disk on the system with enough space. I am trying to calculate the bandwidth by creating a file, setting the length, then sequentially writing the data like this:
FileInfo file = null;
var drives = DriveInfo.GetDrives();
var stats = new List<DriveInfoStatistics>();
foreach (var drive in drives)
{
do
{
file = new FileInfo(Path.Combine(drive.RootDirectory.FullName, Guid.NewGuid().ToString("D") + ".tmp"));
}
while (file.Exists);
try
{
using (var stream = file.Open(FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
var seconds = 10;
var frameRate = 120F;
var bytesWritten = 0L;
var bytesPerPixel = 1;
var watch = new Stopwatch();
var videoSize = new Size(1328, 1048);
var buffer = new byte [(int) (videoSize.Width * videoSize.Height * bytesPerPixel)];
stream.SetLength((long) (videoSize.Width * videoSize.Height * bytesPerPixel * frameRate * seconds));
watch.Restart();
for (int i = 0; i < seconds; i++)
{
for (int j = 0; j < frameRate; j++)
{
stream.Write(buffer, 0, buffer.Length);
bytesWritten += buffer.Length;
}
}
watch.Stop();
stats.Add(new DriveInfoStatistics(drive, bytesWritten / watch.Elapsed.TotalSeconds));
}
}
catch
{
}
finally
{
file.Refresh();
if (file.Exists) { try { file.Delete(); } finally { file.Refresh(); } }
}
}
if (stats.Count == 0)
{
throw (new Exception("No suitable drives were found."));
}
else
{
stats.Sort((x, y) => y.DataTransferRate.CompareTo(x.DataTransferRate));
message
= "The following drives are suitable candidates (best to worst):"
+ Environment.NewLine
+ Environment.NewLine
+ string.Join(Environment.NewLine, stats.ConvertAll<string>(s => (s.DriveInfo.RootDirectory.FullName.Substring(0, 2).ToUpper() + " " + ConversionUtilities.ToIsuBytesNotation(s.DataTransferRate) + "ps")))
+ Environment.NewLine
+ Environment.NewLine
+ "Test results may vary based on other applications accessing the drives."
+ Environment.NewLine
+ Environment.NewLine
+ "Try the test with the system configured as it would be in production."
;
MessageBox.Show(message);
}
The results I get don't make sense:
DESKTOP
D: 4.15 GBps // SSD.
F: 4.09 GBps // HDD (5200 RPM).
E: 4.06 GBps // HDD (7500 RPM).
C: 4.03 GBps // SSD.
H: 2.45 GBps // Ram Disk!!!
- First of all, SSDs and hard drives are too close to each other.
- Secondly, the speeds are much faster than I expected.
- Third, the RAM disk (created with the RAMDisk) appears to have the lowest bandwidth. In practice, Ram Disk is far superior to others when recording actual video data.
NOTEBOOK
E: 981.24 MBps // Ram Disk.
C: 100.17 MBps // HDD (5200 RPM).
D: 055.94 MBps // HDD (5200 RPM).
The results of the same code on my development laptop are more believable.
Is there something wrong with the code above? If not, how would you explain 4GB / s bandwidth for an SSD while Ram Disk will hit 2.5GBps?
I understand that there are many factors that affect throughput and that the testing software is very complex. However, in my case, recording a 2GB video file at 120fps with no frame loss is critical, and the code above should provide the user with a quick and dirty recommendation on which disk to use to store the timeframes. The frames are later processed and transcoded into MP4 videos of just a few megabytes.
Finally, I tried the above code next to Contig.exe
from Sysinternals
to provide a continuous layout to improve hard drive performance. However, I did not notice a performance difference, indicating that the file was not fragmented enough to start (after being created).
source to share
When a program writes data to disk, there are many different things:
Data is first written to the RAM buffer and the operation is recognized by the writing program before the data is transferred to the next steps.
The data is then written to the hard disk controller, which can perform its own caching.
The data is then written to the hard drive, which in turn can perform its own caching.
It is very difficult to measure real throughput with high-level software.
One possibility: write a very large file that is expected to be much larger than any chaches on the operating system / controller / hard drive. This gives you a good estimate of consistent write speed.
source to share