2 using System.Collections.Generic;
3 using System.Diagnostics;
5 using System.Runtime.CompilerServices;
6 using Tizen.NUI.Binding;
7 using Tizen.NUI.Binding.Internals;
9 namespace Tizen.NUI.Xaml.Build.Tasks
11 [Preserve(AllMembers = true)]
12 internal class PerformanceProvider : IPerformanceProvider
14 internal class Statistic
16 public readonly List<Tuple<string, long>> StartTimes = new List<Tuple<string, long>>();
18 public long TotalTime;
22 readonly Dictionary<string, Statistic> _Statistics = new Dictionary<string, Statistic>();
24 public Dictionary<string, Statistic> Statistics {
25 get { return _Statistics; }
33 public void Start(string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
35 string id = GetId(tag, path, member);
37 Statistic stats = GetStat(id);
40 stats.IsDetail = true;
43 stats.StartTimes.Add(new Tuple<string, long>(reference, Stopwatch.GetTimestamp()));
46 public void Stop(string reference, string tag = null, [CallerFilePath] string path = null, [CallerMemberName] string member = null)
48 string id = GetId(tag, path, member);
49 long stop = Stopwatch.GetTimestamp();
51 Statistic stats = GetStat(id);
53 if (!stats.StartTimes.Any())
56 long start = stats.StartTimes.Single(s => s.Item1 == reference).Item2;
57 stats.TotalTime += stop - start;
60 public IEnumerable<string> GetStats()
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);
71 static string ShortenPath(string path)
73 int index = path.IndexOf("Tizen.NUI.Xaml.");
75 path = path.Substring(index + 14);
80 static string GetId(string tag, string path, string member)
82 return string.Format("{0}:{1}{2}", path, member, (tag != null ? "-" + tag : string.Empty));
85 Statistic GetStat(string id)
88 if (!Statistics.TryGetValue(id, out stats)) {
89 Statistics[id] = stats = new Statistic();