Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Profiler.cs
浏览该文件的文档.
1using System.Diagnostics;
2using System.Text;
3using Engine;
4
5namespace Game {
6 public struct Profiler : IDisposable {
7 public class Metric {
8 public string Name;
9
10 public int HitCount;
11
12 public long TotalTicks;
13
14 public long MaxTicks;
15
16 public readonly RunningAverage AverageHitCount = new(5f);
17
18 public readonly RunningAverage AverageTime = new(5f);
19 }
20
21 public static Dictionary<string, Metric> m_metrics = [];
22
23 public static List<Metric> m_sortedMetrics = [];
24
25 public static int m_maxNameLength;
26
27 public static bool m_sortNeeded;
28
29 public long m_startTicks;
30
32
33 public static bool Enabled = true;
34
35 public static int MaxNameLength => m_maxNameLength;
36
37 public static ReadOnlyList<Metric> Metrics {
38 get {
39 if (m_sortNeeded) {
40 m_sortedMetrics.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name));
41 m_sortNeeded = false;
42 }
43 return new ReadOnlyList<Metric>(m_sortedMetrics);
44 }
45 }
46
47 public Profiler(string name) {
48 if (Enabled) {
49 if (!m_metrics.TryGetValue(name, out m_metric)) {
50 m_metric = new Metric { Name = name };
52 m_metrics.Add(name, m_metric);
54 m_sortNeeded = true;
55 }
56 m_startTicks = Stopwatch.GetTimestamp();
57 }
58 else {
59 m_startTicks = 0L;
60 m_metric = null;
61 }
62 }
63
64 public void Dispose() {
65 if (m_metric != null) {
66 long num = Stopwatch.GetTimestamp() - m_startTicks;
67 m_metric.TotalTicks += num;
68 m_metric.MaxTicks = Math.Max(m_metric.MaxTicks, num);
69 m_metric.HitCount++;
70 m_metric = null;
71 return;
72 }
73 throw new InvalidOperationException("Profiler.Dispose called without a matching constructor.");
74 }
75
76 public static void Sample() {
77 foreach (Metric metric in Metrics) {
78 float sample = metric.TotalTicks / (float)Stopwatch.Frequency;
79 metric.AverageHitCount.AddSample(metric.HitCount);
80 metric.AverageTime.AddSample(sample);
81 metric.HitCount = 0;
82 metric.TotalTicks = 0L;
83 metric.MaxTicks = 0L;
84 }
85 }
86
87 public static void ReportAverage(Metric metric, StringBuilder text) {
88 int num = m_maxNameLength + 2;
89 int length = text.Length;
90 text.Append(metric.Name);
91 text.Append('.', Math.Max(1, num - text.Length + length));
92 text.AppendNumber(metric.AverageHitCount.Value, 2);
93 text.Append("x");
94 text.Append('.', Math.Max(1, num + 9 - text.Length + length));
96 }
97
98 public static void ReportFrame(Metric metric, StringBuilder text) {
99 int num = m_maxNameLength + 2;
100 int length = text.Length;
101 text.Append(metric.Name);
102 text.Append('.', Math.Max(1, num - text.Length + length));
103 FormatTimeSimple(text, metric.TotalTicks / (float)Stopwatch.Frequency);
104 }
105
106 public static void ReportAverage(StringBuilder text) {
107 foreach (Metric metric in Metrics) {
108 ReportAverage(metric, text);
109 text.Append("\n");
110 }
111 }
112
113 public static void ReportFrame(StringBuilder text) {
114 foreach (Metric metric in Metrics) {
115 ReportFrame(metric, text);
116 text.Append("\n");
117 }
118 }
119
120 public static void FormatTimeSimple(StringBuilder text, float time) {
121 text.AppendNumber(time * 1000f, 3);
122 text.Append("ms");
123 }
124
125 public static void FormatTime(StringBuilder text, float time) {
126 if (time >= 1f) {
127 text.AppendNumber(time, 2);
128 text.Append("s");
129 }
130 else if (time >= 0.1f) {
131 text.AppendNumber(time * 1000f, 0);
132 text.Append("ms");
133 }
134 else if (time >= 0.01f) {
135 text.AppendNumber(time * 1000f, 1);
136 text.Append("ms");
137 }
138 else if (time >= 0.001f) {
139 text.AppendNumber(time * 1000f, 2);
140 text.Append("ms");
141 }
142 else if (time >= 0.0001f) {
143 text.AppendNumber(time * 1000000f, 0);
144 text.Append("us");
145 }
146 else if (time >= 1E-05f) {
147 text.AppendNumber(time * 1000000f, 1);
148 text.Append("us");
149 }
150 else if (time >= 1E-06f) {
151 text.AppendNumber(time * 1000000f, 2);
152 text.Append("us");
153 }
154 else if (time >= 1E-07f) {
155 text.AppendNumber(time * 1E+09f, 0);
156 text.Append("ns");
157 }
158 else if (time >= 1E-08f) {
159 text.AppendNumber(time * 1E+09f, 1);
160 text.Append("ns");
161 }
162 else {
163 text.AppendNumber(time * 1E+09f, 2);
164 text.Append("ns");
165 }
166 }
167 }
168}
static int Max(int x1, int x2)
readonly RunningAverage AverageHitCount
readonly RunningAverage AverageTime
void AddSample(float sample)
static void ReportFrame(StringBuilder text)
static List< Metric > m_sortedMetrics
Profiler(string name)
static ReadOnlyList< Metric > Metrics
static void FormatTime(StringBuilder text, float time)
static void FormatTimeSimple(StringBuilder text, float time)
static void ReportFrame(Metric metric, StringBuilder text)
static void Sample()
static bool m_sortNeeded
static void ReportAverage(StringBuilder text)
static int m_maxNameLength
static bool Enabled
static int MaxNameLength
static Dictionary< string, Metric > m_metrics
static void ReportAverage(Metric metric, StringBuilder text)