Displaying build times in Visual Studio?

2019-01-04 18:01发布

Our build server is taking too long to build one of our C++ projects. It uses Visual Studio 2008. Is there any way to get devenv.com to log the time taken to build each project in the solution, so that I know where to focus my efforts?

Improved hardware is not an option in this case.

I've tried setting the output verbosity (under Tools / Options / Projects and Solutions / Build and Run / MSBuild project build output verbosity). This doesn't seem to have any effect in the IDE.

When running MSBuild from the command line (and, for Visual Studio 2008, it needs to be MSBuild v3.5), it displays the total time elapsed at the end, but not in the IDE.

I really wanted a time-taken report for each project in the solution, so that I could figure out where the build process was taking its time.

Alternatively, since we actually use NAnt to drive the build process (we use Jetbrains TeamCity), is there a way to get NAnt to tell me the time taken for each step?

12条回答
一纸荒年 Trace。
2楼-- · 2019-01-04 18:16

If you want to invoke an external program that can track your total build times, you can use the following solution for VS 2010 (and maybe older). The code below uses CTime by Casey Muratori. Of course you can also use it to simply print the build time.

Open up the macro explorer, and paste the following before End Module:

Dim buildStart As Date
Private Sub RunCtime(ByVal StartRatherThanEnd As Boolean)
    Dim Arg As String
    Dim psi As New System.Diagnostics.ProcessStartInfo("ctime.exe")
    If StartRatherThanEnd Then
        psi.Arguments = "-begin"
    Else
        psi.Arguments = "-end"
    End If
    psi.Arguments += " c:\my\path\build.ctm"
    psi.RedirectStandardOutput = False
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.UseShellExecute = False
    psi.CreateNoWindow = True
    Dim process As System.Diagnostics.Process
    process = System.Diagnostics.Process.Start(psi)
    Dim myOutput As System.IO.StreamReader = process.StandardOutput
    process.WaitForExit(2000)
    If process.HasExited Then
        Dim output As String = myOutput.ReadToEnd
        WriteToBuildWindow("CTime output: " + output)
    End If
End Sub

Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
    WriteToBuildWindow("Build started!")
    buildStart = Date.Now
    RunCtime(True)
End Sub

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
    Dim buildTime = Date.Now - buildStart
    WriteToBuildWindow(String.Format("Total build time: {0} seconds", buildTime.ToString))
    RunCtime(False)
End Sub

Private Sub WriteToBuildWindow(ByVal message As String)
    Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim ow As OutputWindow = CType(win.Object, OutputWindow)
    If (Not message.EndsWith(vbCrLf)) Then
        message = message + vbCrLf
    End If
    ow.OutputWindowPanes.Item("Build").OutputString(message)
End Sub

Answer taken from here and here.

查看更多
干净又极端
3楼-- · 2019-01-04 18:16

Options -> Projects and Solutions -> VC++ Project Settings -> Build Timing

enter image description here

查看更多
姐就是有狂的资本
4楼-- · 2019-01-04 18:19

If you're stuck on VS2005 you could use the vs-build-timer plugin. At the completion of a build it shows the total time taken and a (optional) summary of each of the project durations.

Disclaimer; I wrote it. And yes, I need to create an installer...one day!

查看更多
干净又极端
5楼-- · 2019-01-04 18:21

Since your question involves using DevEnv from the command line, I would also suggest using MSBuild (which can build .sln files without modification).

msbuild /fl /flp:Verbosity=diagnostic Your.sln

msbuild /? will show you other useful options for the filelogger.

查看更多
放我归山
6楼-- · 2019-01-04 18:24

If you want to visualize your build you can use IncrediBuild. IncrediBuild's now available in standalone-mode (not distributed but for use only on 8 cores on your local machine) for free as part of Visual Studio 2015 Update 1

Disclaimer: I work for IncrediBuild

查看更多
Viruses.
7楼-- · 2019-01-04 18:24

I ended up here because I just wanted the date and time included in the build output. Should others be searching for something similar it's as simple as adding echo %date% %time% to the Pre-build and/or Post-build events under project, PropertiesCompileBuild Events.

查看更多
登录 后发表回答