前日在用SiSoftware Sandra 2004測試硬盤性能時突發其想,用自己熟悉的VB.Net測試行不行呢?具體怎麼做呢?
我們最感興趣的是硬盤在最大負荷下持續的讀取和寫入速度。為了能夠比較准確的測出平均速度,我決定采用先寫入一個1GB的文件再讀取出來的辦法。考慮到不要讓更多的任務花在循環上,我首先建立起一個足夠大的緩沖區,然後往磁盤寫入這個緩沖的內容,從而使硬盤達到最大的負荷。考慮到Windows的讀取機制,硬盤測試不太准確,此程序的讀取部分只能在第一次運行時使用,運行次數越多測試也不准確,而寫入測試多次運行以後依然能夠保持准確性。現在就開始動手。
在VB.Net中創建了一個控制台工程TestHarddisk,然後在Sub Main中寫入下列程序。
Sub Main()
Dim I As Int32
Dim f As New FileStream("E:\BigFile.big", FileMode.Create)
Dim fw As New BinaryWriter(f)
Dim fr As New BinaryReader(f)
Dim Size As Int32 = 1024 * 1024 * 1024 - 1 'File size = 1GB
Dim bufSize As Int32 = 30 * 1024 * 1024 'Buffer Size = 30MB
Dim jLast As Int32 = bufSize - 1
Dim j As Int32
Dim Bytes(bufSize) As Byte
Dim StartWrite As Date = Date.Now
Console.WriteLine("Write Start at {0}", StartWrite)
Console.WriteLine("Creating...")
For I = 0 To Size Step bufSize '1GB
fw.Write(Bytes)
Next
Dim EndWrite As Date = Date.Now
Dim TimePassed As TimeSpan = EndWrite.Subtract(StartWrite)
Console.WriteLine("Write End at {0}", EndWrite)
Console.WriteLine("Time passed:{0}", TimePassed)
Console.WriteLine("Speed:{0}", 1000 / TimePassed.TotalSeconds)
fw.Flush()
Dim StartRead As Date = Date.Now
Console.WriteLine("Read Start at {0}", StartRead)
Console.WriteLine("Reading")
For I = 0 To Size Step bufSize
Bytes = fr.ReadBytes(bufSize)
Next
Dim EndRead As Date = Date.Now
TimePassed = EndRead.Subtract(StartRead)
Console.WriteLine("Read End at {0}", EndRead)
Console.WriteLine("Time passed:{0}", TimePassed)
Console.WriteLine("Read speed:{0}", 1000 / TimePassed.TotalSeconds)
Console.ReadLine()
fw.Close()
End Sub
現在測試。
硬件配置: Athlon 2500+(running at 1.8G)
EPOX 8RDA3+ nForce2主板
512DDR 400(running at 400MHz)
ATA100 Seagate 7200.7 80GB 2MB
軟件配置: Windows 2003 Server Standard Edition
全套最新的WHQL驅動程序
Visual Studio 2003,編譯選擇Realse模式,打開所有優化選項。
第一次運行測試得寫入速度43MB/s,第二次測得42MB/s,第三次41MB/s與Sisoftware測試得的結果43MB/s相差不大,達到了理想的效果。然而讀取測試就變態了,第一次64MB/s,與Sissoftware測試多了5MB/s左右,第二次達到了1096MB/s,第三次1123MB/s,這和Windows的磁盤緩沖機制有關,看來作用還是蠻大的,當然,建議讀取測試在重起電腦以後進行。