xbuild: Collect and dump timing information.
authorAnkit Jain <radical@corewars.org>
Sat, 24 Jul 2010 00:42:40 +0000 (06:12 +0530)
committerAnkit Jain <radical@corewars.org>
Sat, 24 Jul 2010 00:42:40 +0000 (06:12 +0530)
* ConsoleLogger.cs: Collect and dump timing information.

mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ChangeLog
mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/ConsoleLogger.cs

index 6642d67ebf759936a52e8d1f1fb8727b3fbb291f..fccbf5743994b0c7a96c30ed636916a6ba5fd6c9 100644 (file)
@@ -1,3 +1,7 @@
+2010-07-24  Ankit Jain  <jankit@novell.com>
+
+       * ConsoleLogger.cs: Collect and dump timing information.
+
 2010-07-24  Ankit Jain  <jankit@novell.com>
 
        * ConsoleLogger.cs: Honor LoggerVerbosity.Quiet .
index 403978106526622f3812dac063519a52f34033e5..249abee1a0cdcf7cf8899628a617833903760be8 100644 (file)
@@ -32,6 +32,7 @@ using System.Runtime.InteropServices;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Security;
 using System.Text;
 using Microsoft.Build.Framework;
@@ -60,6 +61,7 @@ namespace Microsoft.Build.BuildEngine {
                List<BuildStatusEventArgs> events;
                Dictionary<string, List<string>> errorsTable;
                Dictionary<string, List<string>> warningsTable;
+               SortedDictionary<string, PerfInfo> targetPerfTable, tasksPerfTable;
                string current_events_string;
                
                public ConsoleLogger ()
@@ -96,6 +98,8 @@ namespace Microsoft.Build.BuildEngine {
                        events = new List<BuildStatusEventArgs> ();
                        errorsTable = new Dictionary<string, List<string>> ();
                        warningsTable = new Dictionary<string, List<string>> ();
+                       targetPerfTable = new SortedDictionary<string, PerfInfo> ();
+                       tasksPerfTable = new SortedDictionary<string, PerfInfo> ();
 
                        //defaults
                        errorColor = ConsoleColor.DarkRed;
@@ -230,14 +234,15 @@ namespace Microsoft.Build.BuildEngine {
                                return;
                        }
 
+                       TimeSpan timeElapsed = args.Timestamp - buildStart;
+                       if (performanceSummary || verbosity == LoggerVerbosity.Diagnostic)
+                               DumpPerformanceSummary ();
+
                        if (args.Succeeded == true && !projectFailed) {
                                WriteLine ("Build succeeded.");
                        } else {
                                WriteLine ("Build FAILED.");
                        }
-                       if (performanceSummary == true) {
-                       }
-
                        if (warnings.Count > 0) {
                                WriteLine (Environment.NewLine + "Warnings:");
                                SetColor (warningColor);
@@ -276,7 +281,6 @@ namespace Microsoft.Build.BuildEngine {
                        }
 
                        if (showSummary == true){
-                               TimeSpan timeElapsed = args.Timestamp - buildStart;
                                WriteLine (String.Format ("\t {0} Warning(s)", warningCount));
                                WriteLine (String.Format ("\t {0} Error(s)", errorCount));
                                WriteLine (String.Empty);
@@ -445,6 +449,18 @@ namespace Microsoft.Build.BuildEngine {
 
                void PopEvent ()
                {
+                       if (performanceSummary || verbosity == LoggerVerbosity.Diagnostic) {
+                               var args = events [events.Count - 1];
+                               TargetStartedEventArgs tgt_args = args as TargetStartedEventArgs;
+                               if (tgt_args != null) {
+                                       AddPerfInfo (tgt_args.TargetName, args.Timestamp, targetPerfTable);
+                               } else {
+                                       TaskStartedEventArgs tsk_args = args as TaskStartedEventArgs;
+                                       if (tsk_args != null)
+                                               AddPerfInfo (tsk_args.TaskName, args.Timestamp, tasksPerfTable);
+                               }
+                       }
+
                        events.RemoveAt (events.Count - 1);
                        current_events_string = null;
                }
@@ -455,7 +471,7 @@ namespace Microsoft.Build.BuildEngine {
 
                        string last_imported_target_file = String.Empty;
                        for (int i = 0; i < events.Count; i ++) {
-                               BuildStatusEventArgs args = events [i];
+                               var args = events [i];
                                ProjectStartedEventArgs pargs = args as ProjectStartedEventArgs;
                                if (pargs != null) {
                                        sb.AppendFormat ("{0} ({1}) ->\n", pargs.ProjectFile,
@@ -480,6 +496,39 @@ namespace Microsoft.Build.BuildEngine {
 
                        return sb.ToString ();
                }
+
+               void AddPerfInfo (string name, DateTime start, IDictionary<string, PerfInfo> perf_table)
+               {
+                       PerfInfo pi;
+                       if (!perf_table.TryGetValue (name, out pi)) {
+                               pi = new PerfInfo ();
+                               perf_table [name] = pi;
+                       }
+
+                       pi.Time += DateTime.Now - start;
+                       pi.NumberOfCalls ++;
+               }
+
+               void DumpPerformanceSummary ()
+               {
+                       SetColor (eventColor);
+                       WriteLine ("Target perfomance summary:");
+                       ResetColor ();
+
+                       foreach (var pi in targetPerfTable.OrderBy (pair => pair.Value.Time))
+                               WriteLine (String.Format ("{0,10:0.000} ms  {1,-50}  {2,5} calls", pi.Value.Time.TotalMilliseconds, pi.Key, pi.Value.NumberOfCalls));
+
+                       WriteLine (String.Empty);
+
+                       SetColor (eventColor);
+                       WriteLine ("Tasks perfomance summary:");
+                       ResetColor ();
+
+                       foreach (var pi in tasksPerfTable.OrderBy (pair => pair.Value.Time))
+                               WriteLine (String.Format ("{0,10:0.000} ms  {1,-50}  {2,5} calls", pi.Value.Time.TotalMilliseconds, pi.Key, pi.Value.NumberOfCalls));
+
+                       WriteLine (String.Empty);
+               }
                
                private void WriteLineWithoutIndent (string message)
                {
@@ -695,6 +744,11 @@ namespace Microsoft.Build.BuildEngine {
                        set { writeHandler = value; }
                }
        }
+
+       class PerfInfo {
+               public TimeSpan Time;
+               public int NumberOfCalls;
+       }
 }
 
 #endif