Calculating hard drive throughput

泄露秘密 提交于 2019-12-22 10:25:35

问题


My app creates a 2GB file and needs to select the fastest drive on the system with enough space. I am trying to calculate throughput by creating the file, setting the length, then writing data to it sequentially as follows:

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 am getting make no 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, the SSDs and HDDs are too close together.
  • Secondly, the speeds are much faster than I would expect.
  • Thirdly, the Ram Disk (created with RAMDisk) seems to have the lowest throughput. In practice, the Ram Disk outperforms others by far when writing actual video data.

LAPTOP

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 a throughput of 4 GBps for an SSD while the Ram Disk tops out at 2.5 GBps?

I understand there are many factors affecting throughput and that benchmarking software is very sophisticated. However, in my case writing a 2GB video file at 120 frames per second without losing frames is crucial and the above code is supposed to present the user with a quick and dirty recommendation on which drive to use to hold transient video frames. The frames are later post-processed and transcoded into an MP4 video only a few megabytes in size.

Lastly, I have tried the above code alongside Contig.exe from Sysinternals to ensure a contiguous layout for better HDD performance. However, I did not notice a difference in performance which indicates that the file was not fragmented enough to begin with (upon creation).


回答1:


If a program writes data to disk, there happen quite a lot of different things:

First the data is written into a RAM buffer and the operation is acknowledged to the writing program way before the data is transfered to the next stages.

Then data is written to the Harddisk controller which may do its own caching.

Then data is written to the hard-drive which in turn may do its own caching.

It is very complicated to measure the real throughput with high level software.

One possibility: Write a very large file, which is expected to be much larger than any chaches in the Operating System / Controler/Hard drive. This gives you a good estimate of sustained writing rate.



来源:https://stackoverflow.com/questions/31781840/calculating-hard-drive-throughput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!