我有一个由 20 个 SSD 磁盘组成的 RAID0 阵列,每个 SSD 的最大写入速度为 530 MB/s。PCIexpress 控制器为 8x。
该阵列映射到 Windows 10 中用 NTFS 格式化的虚拟磁盘 (D)
我原本期望我的 C# 应用程序中的写入速度为 7.7 GB/s(PCIExpress 瓶颈会将阵列的 10 GB/s 降低到 20*500MB/s),但使用一个线程我最多只能达到 2.5 GB/S。我的假设完全错误吗?
我还注意到一件事:如果我有一个线程在阵列上写入 127 MB 文件,则平均写入速度为 2.5 GB/s,如果有两个线程,则平均写入速度会下降到 1.8 GB/s。多个线程访问 RAID 阵列可能存在什么问题?
我的代码使用 OpenFileNoBuff 类进行无缓冲写入这里
class Program
{
const int SIZE = 134184960;
static string Folder = @"D:\Test";
static int BlockSize = 65536 ;
static void Main(string[] args)
{
var watchTotal = System.Diagnostics.Stopwatch.StartNew();
Thread ConsumerThread1 = new Thread(SaveFramesOnDisk);
Thread ConsumerThread2 = new Thread(SaveFramesOnDisk);
ConsumerThread1.Start(Folder + "1");
ConsumerThread2.Start(Folder + "2");
ConsumerThread1.Join();
ConsumerThread2.Join();
watchTotal.Stop();
long elapsedMs = watchTotal.ElapsedMilliseconds;
long elapsedS = elapsedMs / 1000;
Console.WriteLine("TOTAL ELAPSED s: " + elapsedS);
double gbWritten = 134184960f * 1200 / 1073741824f;
double avgWriteSpeed = Math.Round(gbWritten / elapsedS,3);
Console.WriteLine("Avg. write speed GB/s: " + avgWriteSpeed);
}
static void SaveFramesOnDisk(object f)
{
//Write 600 frame - 30 seconds acquisition
for (int i = 0; i < 600; i++)
{
byte[] data = new byte[SIZE];
Directory.CreateDirectory(Convert.ToString(f));
//var watchFrame = System.Diagnostics.Stopwatch.StartNew();
FileStream fs = null;
OpenFileNoBuff of = null;
of = new OpenFileNoBuff();
fs = of.openStream(Path.Combine(Convert.ToString(f), DateTime.Now.ToString("yyyy-MM-dd-HHmmss-fffffff") + ".raw"),
FileMode.Create,
FileAccess.Write,
FileShare.None,
true,
false,
BlockSize);
//SafeFileHandle handle = File.OpenHandle(Path.Combine(Convert.ToString(f), DateTime.Now.ToString("yyyy-MM-dd-HHmmss-fffffff") + ".raw"),
// FileMode.Create,
// FileAccess.Write,
// FileShare.None,
// FileOptions.Asynchronous,
// SIZE);
//fs = new FileStream(Path.Combine(Convert.ToString(f), DateTime.Now.ToString("yyyy-MM-dd-HHmmss-fffffff") + ".raw"), FileMode.CreateNew, FileAccess.Write, FileShare.None, 1);
//fs = new FileStream(handle, FileAccess.Write, 0);
fs.SetLength(SIZE);
fs.Write(data, 0, data.Length);
//fs.Flush();
of.closeStream();
fs.Close();
fs.Dispose();
//watchFrame.Stop();
//long elapsedFrameMs = watchFrame.ElapsedMilliseconds;
//Console.WriteLine("elapsedMs for " + i + " write: " + elapsedFrameMs);
//double time = (double)elapsedFrameMs / 1000;
//double size = 134184960f / 1073741824f;
//double speed = size / time;
//Console.WriteLine("Write speed " + speed + " GB/s");
}
}
}