perf graph

This commit is contained in:
Chris Forbes
2009-11-06 19:01:46 +13:00
parent 50b2b3f17e
commit 105792612c
5 changed files with 178 additions and 51 deletions

View File

@@ -9,6 +9,7 @@ using OpenRa.FileFormats;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
using OpenRa.Game.Traits; using OpenRa.Game.Traits;
using OpenRa.Game.Support;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -157,6 +158,8 @@ namespace OpenRa.Game
int t = Environment.TickCount; int t = Environment.TickCount;
int dt = t - lastTime; int dt = t - lastTime;
if( dt >= timestep ) if( dt >= timestep )
{
using (new PerfSample("tick_time"))
{ {
sw.Reset(); sw.Reset();
PathToPathTime = 0; PathToPathTime = 0;
@@ -186,6 +189,9 @@ namespace OpenRa.Game
TickTime = sw.ElapsedTime(); TickTime = sw.ElapsedTime();
} }
PerfHistory.Tick();
}
sw.Reset(); sw.Reset();
++RenderFrame; ++RenderFrame;
viewport.cursor = controller.ChooseCursor(); viewport.cursor = controller.ChooseCursor();

View File

@@ -4,6 +4,7 @@ using System.Windows.Forms;
using IjwFramework.Types; using IjwFramework.Types;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRa.Game.Traits; using OpenRa.Game.Traits;
using OpenRa.Game.Support;
namespace OpenRa.Game.Graphics namespace OpenRa.Game.Graphics
{ {
@@ -110,6 +111,8 @@ namespace OpenRa.Game.Graphics
Game.PathToPathCount, Game.PathToPathCount,
Game.NormalPathCount Game.NormalPathCount
), new int2(5, 5), Color.White); ), new int2(5, 5), Color.White);
PerfHistory.Render(renderer, lineRenderer);
} }
void DrawSelectionBox(Actor selectedUnit, Color c, bool drawHealthBar) void DrawSelectionBox(Actor selectedUnit, Color c, bool drawHealthBar)

View File

@@ -79,6 +79,7 @@
<Compile Include="OrderManager.cs" /> <Compile Include="OrderManager.cs" />
<Compile Include="Ore.cs" /> <Compile Include="Ore.cs" />
<Compile Include="Stopwatch.cs" /> <Compile Include="Stopwatch.cs" />
<Compile Include="Support\PerfHistory.cs" />
<Compile Include="Traits\AcceptsOre.cs" /> <Compile Include="Traits\AcceptsOre.cs" />
<Compile Include="Traits\Activities\Activity.cs" /> <Compile Include="Traits\Activities\Activity.cs" />
<Compile Include="Traits\Activities\DeliverOre.cs" /> <Compile Include="Traits\Activities\DeliverOre.cs" />

View File

@@ -4,6 +4,7 @@ using IjwFramework.Collections;
using System.Linq; using System.Linq;
using OpenRa.FileFormats; using OpenRa.FileFormats;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
using OpenRa.Game.Support;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -27,6 +28,8 @@ namespace OpenRa.Game
} }
public List<int2> FindUnitPath(int2 src, int2 dest, UnitMovementType umt) public List<int2> FindUnitPath(int2 src, int2 dest, UnitMovementType umt)
{
using (new PerfSample("find_unit_path"))
{ {
var sw = new Stopwatch(); var sw = new Stopwatch();
/*if (passableCost[(int)umt][dest.X, dest.Y] == double.PositiveInfinity) /*if (passableCost[(int)umt][dest.X, dest.Y] == double.PositiveInfinity)
@@ -39,6 +42,7 @@ namespace OpenRa.Game
Game.NormalPathCount++; Game.NormalPathCount++;
return result; return result;
} }
}
public List<int2> FindUnitPathToRange(int2 src, int2 dest, UnitMovementType umt, int range) public List<int2> FindUnitPathToRange(int2 src, int2 dest, UnitMovementType umt, int range)
{ {
@@ -53,6 +57,8 @@ namespace OpenRa.Game
public List<int2> FindPathToPath( int2 from, List<int2> path, UnitMovementType umt ) public List<int2> FindPathToPath( int2 from, List<int2> path, UnitMovementType umt )
{ {
var sw = new Stopwatch(); var sw = new Stopwatch();
using (new PerfSample("find_path_to_path"))
{
var cellInfo = InitCellInfo(); var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>(); var queue = new PriorityQueue<PathDistance>();
@@ -78,6 +84,7 @@ namespace OpenRa.Game
Game.PathToPathCount++; Game.PathToPathCount++;
return ret; return ret;
} }
}
public List<int2> FindUnitPath( int2 unitLocation, Func<int2,double> estimator, UnitMovementType umt ) public List<int2> FindUnitPath( int2 unitLocation, Func<int2,double> estimator, UnitMovementType umt )
{ {

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IjwFramework.Collections;
using System.Drawing;
using OpenRa.Game.Graphics;
namespace OpenRa.Game.Support
{
static class PerfHistory
{
static readonly Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Orange, Color.Fuchsia, Color.Lime, Color.LightBlue };
static int nextColor;
static Cache<string, PerfItem> items = new Cache<string, PerfItem>(
s =>
{
var x = new PerfItem(s, colors[nextColor++]);
if (nextColor > colors.Length) nextColor = 0;
return x;
});
public static void Increment( string item, double x )
{
items[item].val += x;
}
public static void Tick()
{
foreach (var item in items.Values)
item.Tick();
}
public static void Render(Renderer r, LineRenderer lr)
{
float2 origin = Game.viewport.Location + new float2(330, Game.viewport.Height - 30);
float2 basis = new float2(-3, -3);
lr.DrawLine(origin, origin + new float2(100, 0) * basis, Color.White, Color.White);
lr.DrawLine(origin + new float2(100,0) * basis, origin + new float2(100,70) * basis, Color.White, Color.White);
foreach (var item in items.Values)
{
int n = 0;
item.Samples().Aggregate((a, b) =>
{
lr.DrawLine(
origin + new float2(n, (float)a) * basis,
origin + new float2(n+1, (float)b) * basis,
item.c, item.c);
++n;
return b;
});
}
lr.Flush();
}
}
class PerfItem
{
public readonly Color c;
public readonly string Name;
public double[] samples = new double[100];
public double val = 0.0;
int head = 1, tail = 0;
public PerfItem(string name, Color c)
{
Name = name;
this.c = c;
}
public void Tick()
{
samples[head++] = val;
if (head == samples.Length) head = 0;
if (head == tail && ++tail == samples.Length) tail = 0;
val = 0.0;
}
public IEnumerable<double> Samples()
{
int n = head;
while (n != tail)
{
--n;
if (n < 0) n = samples.Length - 1;
yield return samples[n];
}
}
}
class PerfSample : IDisposable
{
readonly Stopwatch sw = new Stopwatch();
readonly string Item;
public PerfSample(string item)
{
Item = item;
}
public void Dispose()
{
PerfHistory.Increment(Item, sw.ElapsedTime() * 1000);
}
}
}