【问题标题】:Monitoring application code监控应用程序代码
【发布时间】:2015-07-24 03:03:36
【问题描述】:

我正在使用 Visual Studio 2015 RC 社区。​​p>

我写了一个简单的函数,应该下载html源页面,并显示结果richtextbox。

例如:

Imports System.Text

Public Class frmMain
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.Text = PagesDownloader(4, 8) ' Download Pages from 4 to 8
    End Sub

    Private Function PagesDownloader(StartPage As Integer, EndPage As Integer)
        Const SiteUrl As String = "http://MySite.co.il/TestPage.php?id={0}" ' Every Html Page size is 30MB
        Dim WebClnt As New System.Net.WebClient
        Dim SourceResults As New StringBuilder

        For i = StartPage To EndPage - 1
            SourceResults.AppendLine("Page: " & i)
            SourceResults.AppendLine(String.Format(WebClnt.DownloadString(SiteUrl), i))
            SourceResults.AppendLine("-------------" & vbNewLine)
        Next

        Return SourceResults.ToString

    End Function


End Class

他运行后的代码步骤,是这样的:

  1. 与服务器建立连接
  2. 从服务器下载页面
  3. 将html页面写入内存
  4. 使用 html 源代码构建字符串
  5. 重复这些步骤,直到“结束页”到达最后一个数字(完成)。

现在我问,我如何才能获得用于编程/我的 PC 执行代码的每一行代码的时间?我的 PC 中的硬件消耗了多少电量(以及消耗的电量)?

例如,如果 html 源代码文件大小为 30 MB,则取决于我的互联网速度和服务器速度。我如何获得这些信息?

我希望这是可以理解的,否则我很乐意再次尝试解释。 总的来说,我正在监视代码的性能,以及它如何影响硬件。

【问题讨论】:

    标签: vb.net visual-studio performance-testing webclient


    【解决方案1】:

    您可以使用 StopWatch 类来衡量您的代码执行了多长时间。

    Dim stopWatch As New Stopwatch()
    stopWatch.Start()
    Thread.Sleep(10000) ' to simulate a time consuming task
    stopWatch.Stop()
    Dim ts As TimeSpan = stopWatch.Elapsed
    Console.WriteLine(ts.TotalSeconds)
    

    https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

    【讨论】:

      【解决方案2】:

      谢谢。 至于问题的第二部分: 有没有办法观察它影响的硬件,以及它需要多少功率(CPU、内存、硬盘、互联网、GPU)?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2011-06-04
        相关资源
        最近更新 更多