Agent TFS Build Duration Report

I am trying to create a report to show the relative performance of my various build agents and there is a problem getting the information I need from the tool.

What I would like to have is a simple grid with the following columns:

  • Build number
  • Build definition
  • Build agent
  • Build status
  • Build start time
  • Build time

That would allow me to do something like a graph of the duration of successful builds of a given assembly definition for agent1 with respect to the same assembly definition for agent2 through agentN.

How can i do this?

+3
source share
3 answers

My initial intention was to tell you the TFS OLAP Cube and describe how you could get what came after. Then I realized that the cube does not contain the information that the agent built that Build.

Then I thought it would be easy to write a small TFS console application that prints the information you are after:

using System; using Microsoft.TeamFoundation.Build.Client; using Microsoft.TeamFoundation.Client; namespace BuildDetails { class Program { static void Main() { TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFS:8080/tfs/CoLLeCtIoNNaMe")); var buildService = (IBuildServer)teamProjectCollection.GetService(typeof(IBuildServer)); IBuildDefinition buildDefinition = buildService.GetBuildDefinition("TeamProjectName", "BuildDefinitionName"); IBuildDetail[] buildDetails = buildService.QueryBuilds(buildDefinition); foreach (var buildDetail in buildDetails) { Console.Write(buildDetail.BuildNumber+"\t"); Console.Write(buildDefinition.Name+"\t"); Console.Write(buildDetail.BuildAgent.Name+"\t"); Console.Write(buildDetail.Status+"\t"); Console.Write(buildDetail.StartTime+"\t"); Console.WriteLine((buildDetail.FinishTime - buildDetail.StartTime).Minutes); } } } } 

This will not compile since
enter image description here

I ended up in IBuildInformationNode[] and got the build agent as follows:

 IBuildInformation buildInformation = buildDetail.Information; IBuildInformationNode[] buildInformationNodes = buildInformation.Nodes; string agentName; try { agentName = buildInformationNodes[0].Children.Nodes[3].Fields["ReservedAgentName"]; } catch { agentName = "Couldn't determine BuildAgent"; } Console.Write(agentName + "\t"); 

Try-catch is required, so you can deal with assemblies that did not run / stop before selecting an agent.

If you use this last part as a replacement for the failed Console.Write(buildDetail.BuildAgent.Name+"\t"); , you should end up with a console application whose output can be transferred to a * .CSV file and then imported into Excel.

+3
source

The following code should help you get the name of the assembly agent for this assembly part.

 private string GetBuildAgentName(IBuildDetail build) { var buildInformationNodes = build.Information.GetNodesByType("AgentScopeActivityTracking", true); if (buildInformationNodes != null) { var node = buildInformationNodes.Find(s => s.Fields.ContainsKey(InformationFields.ReservedAgentName)); return node != null ? node.Fields[InformationFields.ReservedAgentName] : string.Empty; } return string.Empty; } 

Make sure you update the assembly information in the assembly part object. You can do this either by calling the following code in the Assembly Details object before receiving assembly agents

 string[] refreshAllDetails = {"*"}; build.Refresh(refreshAllDetails, QueryOptions.Agents); 

Hope this helps :)

+2
source

Assembly agent information is not always in one place.

I found it for the assembly I was looking at in buildInformationNodes [1] .Children.Nodes [2] .Fields ["ReservedAgentName"]. It seems to work for me (still).

 private static string GetAgentName(IBuildDetail buildDetail) { string agentName = "Unknown"; bool fAgentFound = false; try { foreach (IBuildInformationNode node in buildDetail.Information.Nodes) { foreach (IBuildInformationNode childNode in node.Children.Nodes) { if (childNode.Fields.ContainsKey("ReservedAgentName")) { agentName = childNode.Fields["ReservedAgentName"]; break; } } if (fAgentFound) break; } } catch (Exception ex) { // change to your own routine as needed DumpException(ex); } return agentName; } 
0
source

All Articles