This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProfilerItem.cs
124 lines (99 loc) · 2.69 KB
/
ProfilerItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace HatlessEngine
{
/// <summary>
/// Class for measuring and processing profiler data.
/// </summary>
public class ProfilerItem
{
/// <summary>
/// Starting tick of measurement, also used to check if measurement is running (if -1L).
/// </summary>
private long _startTick = -1L;
private readonly Stopwatch _stopwatch;
public readonly string Id;
public readonly ProfilerItem Parent;
public readonly List<ProfilerItem> Children = new List<ProfilerItem>();
private long _totalDuration;
public int TimesCompleted { get; private set; }
public float PercentageOfParent
{
get
{
if (Parent == null)
return 100f;
return (float)Math.Round(100f / Parent._totalDuration * _totalDuration, 2);
}
}
internal ProfilerItem(string id, Stopwatch stopwatch, ProfilerItem parent = null)
{
Id = id;
_stopwatch = stopwatch;
Parent = parent;
}
public float GetTotalDuration(bool inMs = false)
{
float result = _totalDuration;
if (!inMs)
return result;
result /= Stopwatch.Frequency / 1000f;
result = (float)Math.Round(result, 2);
return result;
}
public float GetAverageDuration(bool inMs = false)
{
if (TimesCompleted == 0)
return _totalDuration;
float result = _totalDuration / TimesCompleted;
if (!inMs)
return result;
result /= Stopwatch.Frequency / 1000f;
result = (float)Math.Round(result, 2);
return result;
}
/// <summary>
/// Starts a measurement. Will stop a previous measurement if in progress.
/// </summary>
internal void Start()
{
Stop();
_startTick = _stopwatch.ElapsedTicks;
}
/// <summary>
/// Completes a measurement, setting lastDuration.
/// </summary>
internal void Stop()
{
if (_startTick == -1L)
return;
_totalDuration += _stopwatch.ElapsedTicks - _startTick;
TimesCompleted++;
_startTick = -1L;
}
/// <summary>
/// Returns the child if it exists, or null if not.
/// </summary>
public ProfilerItem GetChildById(string childId, bool recursively)
{
ProfilerItem directMatch = Children.Find(child => child.Id == childId);
if (directMatch != null)
return directMatch;
if (!recursively)
return null;
//search recursively if still not found
foreach (ProfilerItem child in Children)
{
ProfilerItem indirectMatch = child.GetChildById(childId, true);
if (indirectMatch != null)
return indirectMatch;
}
return null;
}
public override string ToString()
{
return String.Format("{0}: {1}/s {2}ms/s ~{3}ms/r {4}%", Id, TimesCompleted, GetTotalDuration(true), GetAverageDuration(true), PercentageOfParent);
}
}
}