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
{ {
@@ -158,32 +159,37 @@ namespace OpenRa.Game
int dt = t - lastTime; int dt = t - lastTime;
if( dt >= timestep ) if( dt >= timestep )
{ {
sw.Reset(); using (new PerfSample("tick_time"))
PathToPathTime = 0;
NormalPathTime = 0;
PathToPathCount = 0;
NormalPathCount = 0;
lastTime += timestep;
if( orderManager.Tick() )
{ {
if( controller.orderGenerator != null ) sw.Reset();
controller.orderGenerator.Tick(); PathToPathTime = 0;
NormalPathTime = 0;
PathToPathCount = 0;
NormalPathCount = 0;
lastTime += timestep;
if (--oreTicks == 0) if (orderManager.Tick())
{ {
var oresw = new Stopwatch(); if (controller.orderGenerator != null)
map.GrowOre(SharedRandom); controller.orderGenerator.Tick();
OreTime = oresw.ElapsedTime();
oreTicks = oreFrequency; if (--oreTicks == 0)
{
var oresw = new Stopwatch();
map.GrowOre(SharedRandom);
OreTime = oresw.ElapsedTime();
oreTicks = oreFrequency;
}
world.Tick();
UnitInfluence.Tick();
foreach (var player in players.Values)
player.Tick();
} }
world.Tick();
UnitInfluence.Tick(); TickTime = sw.ElapsedTime();
foreach( var player in players.Values )
player.Tick();
} }
TickTime = sw.ElapsedTime(); PerfHistory.Tick();
} }
sw.Reset(); sw.Reset();

View File

@@ -3,7 +3,8 @@ using System.Linq;
using System.Windows.Forms; 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
{ {
@@ -109,7 +110,9 @@ namespace OpenRa.Game.Graphics
Game.LocalPlayer.Cash, Game.LocalPlayer.Cash,
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
{ {
@@ -28,16 +29,19 @@ namespace OpenRa.Game
public List<int2> FindUnitPath(int2 src, int2 dest, UnitMovementType umt) public List<int2> FindUnitPath(int2 src, int2 dest, UnitMovementType umt)
{ {
var sw = new Stopwatch(); using (new PerfSample("find_unit_path"))
/*if (passableCost[(int)umt][dest.X, dest.Y] == double.PositiveInfinity) {
return new List<int2>(); var sw = new Stopwatch();
if (!Game.BuildingInfluence.CanMoveHere(dest)) /*if (passableCost[(int)umt][dest.X, dest.Y] == double.PositiveInfinity)
return new List<int2>();*/ return new List<int2>();
if (!Game.BuildingInfluence.CanMoveHere(dest))
return new List<int2>();*/
var result = FindUnitPath(src, DefaultEstimator(dest), umt); var result = FindUnitPath(src, DefaultEstimator(dest), umt);
Game.NormalPathTime += sw.ElapsedTime(); Game.NormalPathTime += sw.ElapsedTime();
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,30 +57,33 @@ 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 queue = new PriorityQueue<PathDistance>();
var estimator = DefaultEstimator( from );
var cost = 0.0;
var prev = path[ 0 ];
for( int i = 0 ; i < path.Count ; i++ )
{ {
var sl = path[ i ];
if( /*i == 0 || */(Game.BuildingInfluence.CanMoveHere(path[i]) && Game.UnitInfluence.GetUnitAt( path[ i ] ) == null) ) var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>();
var estimator = DefaultEstimator(from);
var cost = 0.0;
var prev = path[0];
for (int i = 0; i < path.Count; i++)
{ {
queue.Add( new PathDistance( estimator( sl ), sl ) ); var sl = path[i];
cellInfo[ sl.X, sl.Y ] = new CellInfo( cost, prev, false ); if ( /*i == 0 || */(Game.BuildingInfluence.CanMoveHere(path[i]) && Game.UnitInfluence.GetUnitAt(path[i]) == null))
{
queue.Add(new PathDistance(estimator(sl), sl));
cellInfo[sl.X, sl.Y] = new CellInfo(cost, prev, false);
}
var d = sl - prev;
cost += ((d.X * d.Y != 0) ? 1.414213563 : 1.0) * passableCost[(int)umt][sl.X, sl.Y];
prev = sl;
} }
var d = sl - prev; var ret = FindPath(cellInfo, queue, estimator, umt, true);
cost += ( ( d.X * d.Y != 0 ) ? 1.414213563 : 1.0 ) * passableCost[ (int)umt ][ sl.X, sl.Y ]; ret.Reverse();
prev = sl; Game.PathToPathTime += sw.ElapsedTime();
Game.PathToPathCount++;
return ret;
} }
var ret = FindPath( cellInfo, queue, estimator, umt, true );
ret.Reverse();
Game.PathToPathTime += sw.ElapsedTime();
Game.PathToPathCount++;
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);
}
}
}