Click here to Skip to main content
Click here to Skip to main content

Solution Build Timer for VS2005

By , 21 Aug 2008
 

Introduction

This add-in allows users of Visual Studio 2005 to see the time taken to build a complete solution.

Background

I recently discovered a blog post that referred to an undocumented switch (/MP) in Visual Studio 2005 C++ solutions that would compile source files in multiple threads.

I wanted to determine the time saving I would get on our 30 project solution, so needed to time a solution build with and without the switch. Unfortunately, the VS2005 C++ project settings that allow you to switch on build timing will not time the entire solution, only the time to build each individual project. After suffering through two builds using a stopwatch to determine the total build times (for the record, 28m 26s without the switch, and 17m 42s with), I searched for a way to get the IDE to tell me the total build time.

Googling the problem led me to other blog and forum posts from people complaining of this missing feature (which was present back in the days of Visual Studio 6).

My solution was to use the IDE automation present in Visual Studio 2005 to add back a most welcome feature.

How the code works

The add-in was initially generated using the Visual Studio 2005 C# AddIn wizard. This created the framework on which the rest of the code could be hung.

The IDE automation model exposes two events, OnBuildBegin and OnBuildDone, which were perfect for use in the add-in as they are fired when a build is started and after it ends.

In the OnConnection event of the add-in, we get the window pane to which we intend to send output (in this case, the Build window) and add our own event handlers to the IDE:

public void OnConnection(object application, ext_ConnectMode connectMode,
                         object addInInst, ref Array custom)
{
  _applicationObject = (DTE2)application;
  _addInInstance = (AddIn)addInInst;
  // We want our output in the Build window
  OutputWindow outputWindow = 
   (OutputWindow)_applicationObject.Windows.Item(Constants.vsWindowKindOutput).Object;
  outputWindowPane = outputWindow.OutputWindowPanes.Item("Build");
  // Add ourselves as a OnBuildBegin/OnBuildDone handler
  EnvDTE.Events events = _applicationObject.Events;
  buildEvents = (EnvDTE.BuildEvents)events.BuildEvents;
  buildEvents.OnBuildBegin += 
   new _dispBuildEvents_OnBuildBeginEventHandler(this.OnBuildBegin);
  buildEvents.OnBuildDone += 
   new _dispBuildEvents_OnBuildDoneEventHandler(this.OnBuildDone);
}

In the OnDisconnection event of the add-in, we clean up after ourselves:

public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
  // Remove ourselves as a OnBuildBegin/OnBuildEnd handler
  if (buildEvents != null)
  {
    buildEvents.OnBuildBegin -=
     new _dispBuildEvents_OnBuildBeginEventHandler(this.OnBuildBegin);
    buildEvents.OnBuildDone -=
     new _dispBuildEvents_OnBuildDoneEventHandler(this.OnBuildDone);
  }
}

The meat of the add-in comes in the handlers that we added during the OnConnection event. Firstly, the OnBuildBegin event:

public void OnBuildBegin(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
{
  // Check for a solution build for Build or RebuildAll
  if (EnvDTE.vsBuildScope.vsBuildScopeSolution == Scope &&
      (EnvDTE.vsBuildAction.vsBuildActionBuild == Action ||
       EnvDTE.vsBuildAction.vsBuildActionRebuildAll == Action))
  {
    // Flag our build timer
    amTiming = true;
    dtStart = DateTime.Now;
    outputWindowPane.OutputString(String.Format(
         "Starting timed solution build on {0}\n", dtStart));
  }
}

The OnBuildBegin event first checks that we are building a solution (as opposed to a project), and further limits our timing to the Build or Rebuild All commands (we want to ignore Clean commands). If the checks are successful, we then set a flag to say we are timing the build, get the current date and time, and send that to the Build window.

Secondly, the OnBuildDone event:

public void OnBuildDone(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
{
  // Check if we are actually timing this build
  if (amTiming)
  {
    amTiming = false;
    dtEnd = DateTime.Now;
    outputWindowPane.OutputString(String.Format(
        "Ended timed solution build on {0}\n", dtEnd));
    TimeSpan tsElapsed = dtEnd - dtStart;
    outputWindowPane.OutputString(String.Format("Total build time: {0}\n", 
                                                tsElapsed));
   }
}

The OnBuildDone event first checks that we were actually timing the build. If that is correct, we then get the current date and time, send that to the Build window, calculate the elapsed time between the start and the end of the build and, finally, send that to the Build window.

Points of interest

I found it quite difficult to determine what was available in terms of automation in the IDE. The MSDN Help on the subject appears to be sorely lacking for someone just starting out in the world of add-ins (this is only my third).

I did find a very helpful resource in the VS2005 Automation samples (downloadable from the Microsoft website) which gave me all the entry points I needed once I could browse the source code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Brett Rowbotham
Architect Knowledge Base Software
South Africa South Africa
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to install and does it work with VS 2008memberbencbr22 Apr '09 - 9:49 
I haven't been able to figure out how to actually use this in my solution? Can you provide some step by step instructions?
AnswerRe: How to install and does it work with VS 2008 [modified] PinmemberBrett Rowbotham22 Apr '09 - 18:46 
The source code download includes a setup program you can run which will install the prebuilt addin for you. Alternatively, after building the addin from the source, copy the resulting .DLL and .AddIn files to the Visual Studio 2005\Addins folder in your My Documents folder. That allows the addin to load every time VS is started.
 
Then, whenever you select the Build Solution or Rebuild Solution commands, you will get an indication of the time the build took.
 
As for working with VS2008, I have no idea. This was written specifically for VS2005.
 
Cheers,
Brett
 
modified on Thursday, April 23, 2009 3:01 AM

GeneralRe: How to install and does it work with VS 2008 Pinmemberbencbr23 Apr '09 - 4:24 
Great. It does work in VS 2008 with one tweak in the addin file.
 
Do you happen to have any thoughts on how to make it work for batch builds?
GeneralRe: How to install and does it work with VS 2008 PinmemberBrett Rowbotham23 Apr '09 - 4:39 
I set the addin to run only with the GUI. You could set it to run with the command line but that might require changing some of the event handlers and/or the events to which it reacts.
 
Cheers,
Brett
GeneralRe: How to install and does it work with VS 2008 PinmemberDon Shrout5 May '09 - 11:20 
What was the tweak you made in the addin file? I am wanting to use this as a springboard for an addin I'm trying to write but I can't seem to get it to work in VS2008.
 
Thanks
-madwhit
GeneralRe: How to install and does it work with VS 2008 PinmemberMember 457912418 May '10 - 2:49 
In SolutionBuildTimer.AddIn change
<Version>8.0</Version>
to
<Version>9.0</Version>

GeneralRe: How to install and does it work with VS 2008 PinmemberBrett Rowbotham18 May '10 - 3:04 
You could also add another section to the .addin file:
 
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>9.0</Version>
</HostApplication>
 
in which case it will be supported by both VS2005 and VS2008.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 22 Aug 2008
Article Copyright 2008 by Brett Rowbotham
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid