Improve PerfTimer output
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace OpenRA.Support
|
namespace OpenRA.Support
|
||||||
{
|
{
|
||||||
@@ -16,27 +17,53 @@ namespace OpenRA.Support
|
|||||||
{
|
{
|
||||||
readonly Stopwatch sw = new Stopwatch();
|
readonly Stopwatch sw = new Stopwatch();
|
||||||
readonly string Name;
|
readonly string Name;
|
||||||
|
|
||||||
|
// Hacks to give the output a tree-like structure
|
||||||
static System.Threading.ThreadLocal<int> depth = new System.Threading.ThreadLocal<int>();
|
static System.Threading.ThreadLocal<int> depth = new System.Threading.ThreadLocal<int>();
|
||||||
|
static System.Threading.ThreadLocal<string> prevHeader = new System.Threading.ThreadLocal<string>();
|
||||||
|
|
||||||
public PerfTimer(string name)
|
public PerfTimer(string name)
|
||||||
{
|
{
|
||||||
|
if (prevHeader.Value != null)
|
||||||
|
{
|
||||||
|
Log.Write("perf", prevHeader.Value);
|
||||||
|
prevHeader.Value = null;
|
||||||
|
}
|
||||||
|
|
||||||
this.Name = name;
|
this.Name = name;
|
||||||
|
|
||||||
|
prevHeader.Value = string.Format("{0}{1}", Indentation, this.Name);
|
||||||
depth.Value++;
|
depth.Value++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string Indentation
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var d = depth.Value;
|
||||||
|
if (d == 1)
|
||||||
|
return "| ";
|
||||||
|
else if (d <= 0)
|
||||||
|
return string.Empty;
|
||||||
|
else
|
||||||
|
return string.Concat(Enumerable.Repeat("| ", depth.Value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
string indentation;
|
string format;
|
||||||
|
if (prevHeader.Value == null)
|
||||||
if (--depth.Value >= 0)
|
{
|
||||||
indentation = new string('\t', depth.Value);
|
format = "{0}: {2} ms";
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
depth.Value = 0;
|
format = "{0}{1}: {2} ms";
|
||||||
indentation = string.Empty;
|
prevHeader.Value = null;
|
||||||
}
|
}
|
||||||
|
depth.Value--;
|
||||||
Log.Write("perf", "{0}{1}: {2} ms", indentation, this.Name, Math.Round(this.sw.Elapsed.TotalMilliseconds));
|
Log.Write("perf", format, Indentation, this.Name, Math.Round(this.sw.Elapsed.TotalMilliseconds));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user