[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / XamlBuild / PerformanceProvider.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.Runtime.CompilerServices;
6 using Tizen.NUI.Binding;
7 using Tizen.NUI.Binding.Internals;
8
9 namespace Tizen.NUI.Xaml.Build.Tasks
10 {
11     [Preserve(AllMembers = true)]
12     internal class PerformanceProvider : IPerformanceProvider
13     {
14         internal class Statistic
15         {
16             public readonly List<Tuple<string, long>> StartTimes = new List<Tuple<string, long>>();
17             public int CallCount;
18             public long TotalTime;
19             public bool IsDetail;
20         }
21
22         readonly Dictionary<string, Statistic> _Statistics = new Dictionary<string, Statistic>();
23
24         public Dictionary<string, Statistic> Statistics {
25             get { return _Statistics; }
26         }
27
28         public void Clear()
29         {
30             Statistics.Clear();
31         }
32
33         public void Start(string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
34         {
35             string id = GetId(tag, path, member);
36
37             Statistic stats = GetStat(id);
38
39             if (tag != null)
40                 stats.IsDetail = true;
41
42             stats.CallCount++;
43             stats.StartTimes.Add(new Tuple<string, long>(reference, Stopwatch.GetTimestamp()));
44         }
45
46         public void Stop(string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
47         {
48             string id = GetId(tag, path, member);
49             long stop = Stopwatch.GetTimestamp();
50
51             Statistic stats = GetStat(id);
52
53             if (!stats.StartTimes.Any())
54                 return;
55
56             long start = stats.StartTimes.Single(s => s.Item1 == reference).Item2;
57             stats.TotalTime += stop - start;
58         }
59
60         public IEnumerable<string> GetStats()
61         {
62             yield return "ID                                                                                 | Call Count | Total Time | Avg Time";
63             foreach (KeyValuePair<string, Statistic> kvp in Statistics.OrderBy(kvp => kvp.Key)) {
64                 string key = ShortenPath(kvp.Key);
65                 double total = TimeSpan.FromTicks(kvp.Value.TotalTime).TotalMilliseconds;
66                 double avg = total / kvp.Value.CallCount;
67                 yield return string.Format("{0,-80} | {1,-10} | {2,-10}ms | {3,-8}ms", key, kvp.Value.CallCount, total, avg);
68             }
69         }
70
71         static string ShortenPath(string path)
72         {
73             int index = path.IndexOf("Tizen.NUI.Xaml.");
74             if (index > -1)
75                 path = path.Substring(index + 14);
76
77             return path;
78         }
79
80         static string GetId(string tag, string path, string member)
81         {
82             return string.Format("{0}:{1}{2}", path, member, (tag != null ? "-" + tag : string.Empty));
83         }
84
85         Statistic GetStat(string id)
86         {
87             Statistic stats;
88             if (!Statistics.TryGetValue(id, out stats)) {
89                 Statistics[id] = stats = new Statistic();
90             }
91             return stats;
92         }
93     }
94 }