tools/profiler/csharp/ProfReport.cs
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:59:26 2008 -0500 (2008-09-14)
changeset 2288 15c77b21589a
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Eliminated head for dead branch: refac-jit
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Xml;
     5 
     6 namespace profviewer
     7 {
     8     enum ProfileType : int
     9     {
    10         ProfType_Unknown = 0,
    11         ProfType_Native,
    12         ProfType_Callback,
    13         ProfType_Function
    14     }
    15 
    16     class ProfileItem
    17     {
    18         public string name;
    19         public double total_time;
    20         public uint num_calls;
    21         public double min_time;
    22         public double max_time;
    23         public ProfileType type;
    24 
    25         public double AverageTime
    26         {
    27             get
    28             {
    29                 return total_time / num_calls;
    30             }
    31         }
    32     }
    33 
    34     class ProfileReport
    35     {
    36         public static string[] TypeStrings;
    37         private DateTime m_start_time;
    38         private double m_duration;
    39         private List<ProfileItem> m_Items;
    40 
    41         public int Count
    42         {
    43             get
    44             {
    45                 return m_Items.Count;
    46             }
    47         }
    48 
    49         static ProfileReport()
    50         {
    51             TypeStrings = new string[4];
    52 
    53             TypeStrings[0] = "unknown";
    54             TypeStrings[1] = "native";
    55             TypeStrings[2] = "callback";
    56             TypeStrings[3] = "function";
    57         }
    58 
    59         public ProfileReport(string file)
    60         {
    61             bool in_profile;
    62             ProfileType type;
    63             string cur_report;
    64             XmlTextReader xml;
    65 
    66             xml = new XmlTextReader(file);
    67             xml.WhitespaceHandling = WhitespaceHandling.None;
    68 
    69             m_Items = new List<ProfileItem>();
    70 
    71             type = ProfileType.ProfType_Unknown;
    72             in_profile = false;
    73             cur_report = null;
    74             while (xml.Read())
    75             {
    76                 if (xml.NodeType == XmlNodeType.Element)
    77                 {
    78                     if (xml.Name.CompareTo("profile") == 0)
    79                     {
    80                         int t;
    81                         in_profile = true;
    82                         m_duration = Double.Parse(xml.GetAttribute("uptime"));
    83                         m_start_time = new DateTime(1970, 1, 1, 0, 0, 0);
    84                         t = Int32.Parse(xml.GetAttribute("time"));
    85                         m_start_time = m_start_time.AddSeconds(t);
    86                     }
    87                     else if (in_profile)
    88                     {
    89                         if (xml.Name.CompareTo("report") == 0)
    90                         {
    91                             cur_report = xml.GetAttribute("name");
    92                             if (cur_report.CompareTo("natives") == 0)
    93                             {
    94                                 type = ProfileType.ProfType_Native;
    95                             }
    96                             else if (cur_report.CompareTo("callbacks") == 0)
    97                             {
    98                                 type = ProfileType.ProfType_Callback;
    99                             }
   100                             else if (cur_report.CompareTo("functions") == 0)
   101                             {
   102                                 type = ProfileType.ProfType_Function;
   103                             }
   104                             else
   105                             {
   106                                 type = ProfileType.ProfType_Unknown;
   107                             }
   108                         }
   109                         else if (xml.Name.CompareTo("item") == 0 && cur_report != null)
   110                         {
   111                             ProfileItem item;
   112 
   113                             item = new ProfileItem();
   114                             item.name = xml.GetAttribute("name");
   115                             item.max_time = Double.Parse(xml.GetAttribute("maxtime"));
   116                             item.min_time = Double.Parse(xml.GetAttribute("mintime"));
   117                             item.num_calls = UInt32.Parse(xml.GetAttribute("numcalls"));
   118                             item.total_time = Double.Parse(xml.GetAttribute("totaltime"));
   119                             item.type = type;
   120                             m_Items.Add(item);
   121                         }
   122                     }
   123                 }
   124                 else if (xml.NodeType == XmlNodeType.EndElement)
   125                 {
   126                     if (xml.Name.CompareTo("profile") == 0)
   127                     {
   128                         break;
   129                     }
   130                     else if (xml.Name.CompareTo("report") == 0)
   131                     {
   132                         cur_report = null;
   133                     }
   134                 }
   135             }
   136         }
   137 
   138         public double Duration
   139         {
   140             get
   141             {
   142                 return m_duration;
   143             }
   144         }
   145 
   146         public DateTime StartTime
   147         {
   148             get
   149             {
   150                 return m_start_time;
   151             }
   152         }
   153 
   154         public ProfileItem GetItem(int i)
   155         {
   156             return m_Items[i];
   157         }
   158     }
   159 }