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

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

程序运行效果图如下:



 

主要源码如下:

1、柱状图控件源码
Public Class SpeedViewClass SpeedView

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

    Public Class InfoClass 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()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()Sub PictureBox1_Paint(ByVal sender As Object, ByVal 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()Sub PictureBox1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.SizeChanged
        xStep = PictureBox1.Width / 10
        yStep = PictureBox1.Height / 12
    End Sub

    Private Sub SpeedView_Paint()Sub SpeedView_Paint(ByVal sender As Object, ByVal 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(str, Me.Font)
            point = New Point(PictureBox1.Left + i * xStep - size.Width / 2, PictureBox1.Bottom)
            e.Graphics.DrawString(str, Me.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(str, Me.Font)
                Dim p As Point = New Point(Label3.Left, Label3.Bottom + yStep * i)
                e.Graphics.DrawString(str, Me.Font, Brushes.Red, p)
            End If
            If ThisInfo.DrawRead Then
                Dim str As String = ThisInfo.Read.ToString
                Dim size As SizeF = e.Graphics.MeasureString(str, Me.Font)
                Dim p As Point = New Point(Label4.Left, Label4.Bottom + yStep * i)
                e.Graphics.DrawString(str, Me.Font, Brushes.Black, p)
            End If
        Next
    End Sub

    Public Sub New()Sub New()
        InitializeComponent()
    End Sub

    Public Sub AddReadInfo()Sub AddReadInfo(ByVal y As Single, ByVal 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()Sub AddWriteInfo(ByVal y As Single, ByVal 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()Sub Reset()
        Infos.Clear()
        MaxSpeed = 1
        PictureBox1.Invalidate()
        Me.Invalidate()
    End Sub

    Private Sub SetY()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 FrmTestClass FrmTest

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

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

        Public Sub New()Sub New(ByVal mDrive As String, ByVal mStartIndex As Integer, ByVal mEndIndex As Integer, ByVal mTotalIndex As Integer)
            Drive = mDrive
            StartIndex = mStartIndex
            EndIndex = mEndIndex
            TotalIndex = mTotalIndex
        End Sub
    End Class

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

    Private Sub FrmTest_Load()Sub FrmTest_Load(ByVal sender As Object, ByVal 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()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()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()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()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()Sub Button1_Click(ByVal sender As Object, ByVal 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()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()Function TestWrite(ByVal BufferSize As Integer, ByVal TotalSize As Integer, ByVal FileName As String) As 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()Function TestRead(ByVal BufferSize As Integer, ByVal TotalSize As Integer, ByVal FileName As String) As 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()Sub RunEndSub(ByVal mEnabled As Boolean)

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

    Private Function GetTotalSize()Function GetTotalSize(ByVal Index As Integer) As 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()Function GetCurrentTest(ByVal index) As Single
        Return CType(Me.ComboBox2.Items(index), Single)
    End Function

    Private Sub SetControlEnabled()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()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()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()Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class

郑重声明:资讯 【磁盘(含优盘识别)读写速度测试_jepsen1_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——