磁盘(含优盘识别)读写速度测试

来源:互联网 发布:java socket双向通信 编辑:程序博客网 时间:2024/04/29 19:05

可以测试驱动器(包含优盘自动识别插入、卸载)的读(Read)写(Write)速度,并以图形化的方式直观显示

读写按照二进制形式进行测试

程序运行效果图如下:

 

 

主要源码如下:

1、柱状图控件源码

Public Class SpeedView

    
Private xStep, yStep As Single
    
Private mMaxSpeed As Integer = 10
    
Private Infos As New ArrayList

    
Public Class Info
        
Public Write As Integer
        
Public Read As Integer
        
Public y As Single
        
Public DrawWrite As Boolean
        
Public DrawRead As Boolean
    
End Class


    
Public Property MaxSpeed() As Integer
        
Get
            
Return mMaxSpeed
        
End Get
        
Set(ByVal value As Integer)
            
If mMaxSpeed <> value Then
                mMaxSpeed 
= value
                PictureBox1.Invalidate()
                
Me.Invalidate()
            
End If
        
End Set
    
End Property


    
Private Sub PictureBox1_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        
Dim point1, point2 As PointF

        
'绘制网格
        For i As Integer = 1 To 9
            point1 
= New PointF(xStep * i, 0)
            point2 
= New PointF(xStep * i, PictureBox1.Height)
            e.Graphics.DrawLine(Pens.Blue, point1, point2)
        
Next

        
'绘制图形
        For i As Integer = 0 To Infos.Count - 1
            
Dim ThisInfo As Info = CType(Infos(i), Info)

            
'绘制写速度
            If ThisInfo.DrawWrite Then
                
Dim p1, p2 As Point
                p1 
= New Point(0, yStep * i)
                p2 
= New Point(ThisInfo.Write * PictureBox1.Width / (MaxSpeed * 1024), yStep * i)
                e.Graphics.DrawLine(
New Pen(Color.Red, 4), p1, p2)
            
End If

            
'绘制读速度
            If ThisInfo.DrawRead Then
                
Dim p1, p2 As Point
                p1 
= New Point(0, yStep * i + 4)
                p2 
= New Point(ThisInfo.Read * PictureBox1.Width / (MaxSpeed * 1024), yStep * i + 4)
                e.Graphics.DrawLine(
New Pen(Color.Black, 4), p1, p2)
            
End If
        
Next
    
End Sub


    
Private Sub PictureBox1_SizeChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles PictureBox1.SizeChanged
        xStep 
= PictureBox1.Width / 10
        yStep 
= PictureBox1.Height / 12
    
End Sub


    
Private Sub SpeedView_Paint(ByVal sender As ObjectByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        
'绘制x轴刻度
        For i As Integer = 0 To 10
            
Dim point As Point
            
Dim str As String = (MaxSpeed * i / 10).ToString
            
Dim size As SizeF = e.Graphics.MeasureString(strMe.Font)
            point 
= New Point(PictureBox1.Left + i * xStep - size.Width / 2, PictureBox1.Bottom)
            e.Graphics.DrawString(
strMe.Font, Brushes.Black, point)
        
Next

        
'绘制y轴刻度
        For i As Integer = 0 To Infos.Count - 1
            
Dim ThisInfo As Info = CType(Infos(i), Info)
            
Dim size As SizeF = e.Graphics.MeasureString(ThisInfo.y.ToString, Me.Font)
            
Dim point As Point = New Point(PictureBox1.Left - size.Width, yStep * i + PictureBox1.Top - 4)
            e.Graphics.DrawString(ThisInfo.y.ToString, 
Me.Font, Brushes.Black, point)
        
Next

        
'绘制数值
        For i As Integer = 0 To Infos.Count - 1
            
Dim ThisInfo As Info = CType(Infos(i), Info)
            
If ThisInfo.DrawWrite Then
                
Dim str As String = ThisInfo.Write.ToString
                
Dim size As SizeF = e.Graphics.MeasureString(strMe.Font)
                
Dim p As Point = New Point(Label3.Left, Label3.Bottom + yStep * i)
                e.Graphics.DrawString(
strMe.Font, Brushes.Red, p)
            
End If
            
If ThisInfo.DrawRead Then
                
Dim str As String = ThisInfo.Read.ToString
                
Dim size As SizeF = e.Graphics.MeasureString(strMe.Font)
                
Dim p As Point = New Point(Label4.Left, Label4.Bottom + yStep * i)
                e.Graphics.DrawString(
strMe.Font, Brushes.Black, p)
            
End If
        
Next
    
End Sub


    
Public Sub New()
        InitializeComponent()
    
End Sub


    
Public Sub AddReadInfo(ByVal y As SingleByVal value As Integer)
        
Dim Find As Boolean = False
        
Call SetY(value)
        
For i As Integer = 0 To Infos.Count - 1
            
Dim ThisInfo As Info = CType(Infos(i), Info)
            
If ThisInfo.y = y Then
                Find 
= True
                ThisInfo.Read 
= value
                ThisInfo.DrawRead 
= True
                
Exit For
            
End If
        
Next

        
If Not Find Then
            
Dim NewInfo As New Info
            NewInfo.y 
= y
            NewInfo.Read 
= value
            NewInfo.DrawRead 
= True
            Infos.Add(NewInfo)
        
End If

        PictureBox1.Invalidate()
        
Me.Invalidate()
    
End Sub


    
Public Sub AddWriteInfo(ByVal y As SingleByVal value As Integer)
        
Dim Find As Boolean = False
        
Call SetY(value)
        
For i As Integer = 0 To Infos.Count - 1
            
Dim ThisInfo As Info = CType(Infos(i), Info)
            
If ThisInfo.y = y Then
                Find 
= True
                ThisInfo.Write 
= value
                ThisInfo.DrawWrite 
= True
                
Exit For
            
End If
        
Next

        
If Not Find Then
            
Dim NewInfo As New Info
            NewInfo.y 
= y
            NewInfo.Write 
= value
            NewInfo.DrawWrite 
= True
            Infos.Add(NewInfo)
        
End If

        PictureBox1.Invalidate()
        
Me.Invalidate()
    
End Sub


    
Public Sub Reset()
        Infos.Clear()
        MaxSpeed 
= 1
        PictureBox1.Invalidate()
        
Me.Invalidate()
    
End Sub


    
Private Sub SetY(ByVal value As Integer)
        
Dim NewMax As Integer
        
Select Case value
            
Case 0 To 1024 : NewMax = 2
            
Case 1024 To 4 * 1024 : NewMax = 5
            
Case 4 * 1024 To 9 * 1024 : NewMax = 10
            
Case 9 * 1024 To 19 * 1024 : NewMax = 20
            
Case 19 * 1024 To 48 * 1024 : NewMax = 50
            
Case 48 * 1024 To 97 * 1024 : NewMax = 100
            
Case Else : NewMax = 1000
        
End Select
        
If NewMax > MaxSpeed Then MaxSpeed = NewMax
    
End Sub


End Class

 

2、主窗体源码

Imports System.IO

Public Class FrmTest

    
Private IsStart As Boolean = False
    
Private ThreadTest As Threading.Thread

    
Private Class BE
        
Public Drive As String
        
Public StartIndex As Integer
        
Public EndIndex As Integer
        
Public TotalIndex As Integer

        
Public Sub New(ByVal mDrive As StringByVal mStartIndex As IntegerByVal mEndIndex As IntegerByVal mTotalIndex As Integer)
            Drive 
= mDrive
            StartIndex 
= mStartIndex
            EndIndex 
= mEndIndex
            TotalIndex 
= mTotalIndex
        
End Sub

    
End Class


    
Private Sub FrmTest_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        
If ThreadTest IsNot Nothing Then ThreadTest.Abort()
    
End Sub


    
Private Sub FrmTest_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        
Call Me.ShowDrives()
        
Me.ComboBox1.SelectedIndex = 0
        
Me.ComboBox2.SelectedIndex = 0
        
Me.ComboBox4.SelectedIndex = 11
        
Me.ComboBox3.SelectedIndex = 5
    
End Sub


    
Private Sub ToolbarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolbarToolStripMenuItem.Click
        ToolbarToolStripMenuItem.Checked 
= Not ToolbarToolStripMenuItem.Checked
        
Me.ToolStrip1.Visible = ToolbarToolStripMenuItem.Checked
    
End Sub


    
Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBarToolStripMenuItem.Click
        StatusBarToolStripMenuItem.Checked 
= Not StatusBarToolStripMenuItem.Checked
        
Me.StatusStrip1.Visible = StatusBarToolStripMenuItem.Checked
    
End Sub


    
Private Sub ShowDrives()
        
Dim d() As String = System.IO.Directory.GetLogicalDrives()
        
Dim en As System.Collections.IEnumerator = d.GetEnumerator
        
Me.ComboBox1.Items.Clear()
        
While en.MoveNext
            
Me.ComboBox1.Items.Add(CStr(en.Current))
        
End While
    
End Sub


    
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        
If m.Msg = WM_DEVICECHANGE Then
            
Select Case m.WParam
                
Case DBT_DEVICEARRIVAL : Call Me.ShowDrives() 'U盘插入、卸载
                Case Else
            
End Select
        
End If

        
MyBase.WndProc(m)
    
End Sub


    
Private Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button1.Click
        
If ThreadTest IsNot Nothing Then
            ThreadTest.Abort()
            ThreadTest 
= Nothing
        
End If

        IsStart 
= Not IsStart
        
If IsStart Then
            
Me.SpeedView1.Reset()
            
Call Me.SetControlEnabled(False)
            ThreadTest 
= New Threading.Thread(AddressOf StartTest)
            ThreadTest.Start(
New BE(Me.ComboBox1.Text, Me.ComboBox2.SelectedIndex, Me.ComboBox4.SelectedIndex, Me.ComboBox3.SelectedIndex))
        
Else
            
Call Me.SetControlEnabled(True)
        
End If
    
End Sub


    
Private Sub StartTest(ByVal ThisBE As Object)
        
Dim mBE As BE = CType(ThisBE, BE)
        
For index As Integer = mBE.StartIndex To mBE.EndIndex
            
Dim FileName As String = mBE.Drive + "BigFile.big"
            
            
Dim Size As Integer = GetTotalSize(mBE.TotalIndex)
            
Dim bufSize As Integer = GetBufferSize(index)

            
Me.SpeedView1.AddWriteInfo(GetCurrentTest(index), TestWrite(bufSize, Size, FileName) * 1024)
            
Me.SpeedView1.AddReadInfo(GetCurrentTest(index), TestRead(bufSize, Size, FileName) * 1024)

            Threading.Thread.Sleep(
10)
        
Next
        
Me.Invoke(New RunEndSub(AddressOf SetControlEnabled), True)
    
End Sub


    
'写速度测试
    Private Function TestWrite(ByVal BufferSize As IntegerByVal TotalSize As IntegerByVal FileName As StringAs Integer
        
Try
            
Dim f As New FileStream(FileName, FileMode.Create)
            
Dim fw As New BinaryWriter(f)
            
Dim Bytes(BufferSize) As Byte
            
Dim StartWrite As Date = Date.Now
            
For i As Integer = 0 To TotalSize Step BufferSize
                fw.Write(Bytes)
            
Next
            
Dim EndWrite As Date = Date.Now
            
Dim TimePassed As TimeSpan = EndWrite.Subtract(StartWrite)
            fw.Flush() : fw.Close() : f.Close()

            
Return TotalSize / TimePassed.Ticks
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "TestWrite")
        
End Try
    
End Function


    
'读速度测试
    Private Function TestRead(ByVal BufferSize As IntegerByVal TotalSize As IntegerByVal FileName As StringAs Integer
        
Try
            
Dim f As New FileStream(FileName, FileMode.Open)
            
Dim fr As New BinaryReader(f)
            
Dim StartRead As Date = Date.Now
            
Dim Bytes(BufferSize) As Byte
            
For i As Integer = 0 To TotalSize Step BufferSize
                Bytes 
= fr.ReadBytes(BufferSize)
            
Next
            
Dim EndRead As Date = Date.Now
            
Dim TimePassed As TimeSpan = EndRead.Subtract(StartRead)
            fr.Close() : f.Close()
            
If System.IO.File.Exists(FileName) Then System.IO.File.Delete(FileName)

            
Return TotalSize / TimePassed.Ticks
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "TestRead")
        
End Try
    
End Function


    
Private Delegate Sub RunEndSub(ByVal mEnabled As Boolean)

    
Private Function GetBufferSize(ByVal index As IntegerAs Integer
        
Return 0.5 * 2 ^ index * 1024
    
End Function


    
Private Function GetTotalSize(ByVal Index As IntegerAs Integer
        
Select Case Index
            
Case 0 : Return 64 * 1024
            
Case 1 : Return 128 * 1024
            
Case 2 : Return 256 * 1024
            
Case 3 : Return 1024 * 1024
            
Case 4 : Return 2 * 1024 * 1024
            
Case 5 : Return 4 * 1024 * 1024
            
Case 6 : Return 8 * 1024 * 1024
            
Case 7 : Return 16 * 1024 * 1024
            
Case 8 : Return 32 * 1024 * 1024
        
End Select
    
End Function


    
Public Function GetCurrentTest(ByVal index) As Single
        
Return CType(Me.ComboBox2.Items(index), Single)
    
End Function


    
Private Sub SetControlEnabled(ByVal mEnabled As Boolean)
        
If mEnabled Then Me.Button1.Text = "&Start" Else Me.Button1.Text = "&Stop"
        IsStart 
= Not mEnabled

        
Me.ComboBox1.Enabled = mEnabled
        
Me.ComboBox2.Enabled = mEnabled
        
Me.ComboBox3.Enabled = mEnabled
        
Me.ComboBox4.Enabled = mEnabled

        NewToolStripMenuItem.Enabled 
= mEnabled
        ExitToolStripMenuItem.Enabled 
= mEnabled
        ToolStripButton1.Enabled 
= mEnabled
    
End Sub


    
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click, ToolStripButton1.Click
        
Me.SpeedView1.Reset()
    
End Sub


    
Private Sub AboutBench32ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutBench32ToolStripMenuItem.Click, ToolStripButton7.Click
        
MsgBox("VS.Net 2005" + vbCrLf + "By 王作民", MsgBoxStyle.Information, "About…")
    
End Sub


    
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        
Me.Close()
    
End Sub

End Class

 

由于时间仓促,没有仔细优化,估计还有一些问题存在。

原创粉丝点击