From 35591536fdc190971024bc09eb6d2c174993cbac Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 6 Apr 2023 18:48:36 +0900 Subject: [PATCH] bugfix - TickWatch --- src/DotRecast.Core/TickWatch.cs | 11 +++++++++++ src/DotRecast.Detour.Crowd/CrowdTelemetry.cs | 5 +++-- src/DotRecast.Recast.Demo/RecastDemo.cs | 6 +++--- .../Tools/CrowdProfilingTool.cs | 4 ++-- src/DotRecast.Recast.Demo/Tools/CrowdTool.cs | 4 ++-- src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs | 12 ++++++------ src/DotRecast.Recast/Telemetry.cs | 4 ++-- test/DotRecast.Detour.Test/RandomPointTest.cs | 7 ++++--- .../DotRecast.Detour.TileCache.Test/TileCacheTest.cs | 6 +++--- test/DotRecast.Recast.Test/RecastSoloMeshTest.cs | 6 +++--- test/DotRecast.Recast.Test/RecastTileMeshTest.cs | 6 +++--- 11 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 src/DotRecast.Core/TickWatch.cs diff --git a/src/DotRecast.Core/TickWatch.cs b/src/DotRecast.Core/TickWatch.cs new file mode 100644 index 0000000..ea49c2f --- /dev/null +++ b/src/DotRecast.Core/TickWatch.cs @@ -0,0 +1,11 @@ +using System; +using System.Diagnostics; + +namespace DotRecast.Core +{ + public static class TickWatch + { + public static readonly double Frequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency; + public static long Ticks => unchecked((long)(Stopwatch.GetTimestamp() * Frequency)); + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/CrowdTelemetry.cs b/src/DotRecast.Detour.Crowd/CrowdTelemetry.cs index 723c8ed..4e35972 100644 --- a/src/DotRecast.Detour.Crowd/CrowdTelemetry.cs +++ b/src/DotRecast.Detour.Crowd/CrowdTelemetry.cs @@ -20,6 +20,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using DotRecast.Core; namespace DotRecast.Detour.Crowd { @@ -65,12 +66,12 @@ namespace DotRecast.Detour.Crowd public void start(string name) { - _executionTimings.Add(name, Stopwatch.GetTimestamp()); + _executionTimings.Add(name, TickWatch.Ticks); } public void stop(string name) { - long duration = Stopwatch.GetTimestamp() - _executionTimings[name]; + long duration = TickWatch.Ticks - _executionTimings[name]; if (!_executionTimingSamples.TryGetValue(name, out var s)) { s = new List(); diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index 7b1c98a..ff352f6 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -470,7 +470,7 @@ public class RecastDemo cameraPos[1] += (float)((_moveUp - _moveDown) * keySpeed * dt); - long time = Stopwatch.GetTimestamp(); + long time = TickWatch.Ticks; prevFrameTime = time; // Update sample simulation. @@ -537,7 +537,7 @@ public class RecastDemo float m_detailSampleDist = settingsUI.getDetailSampleDist(); float m_detailSampleMaxError = settingsUI.getDetailSampleMaxError(); int m_tileSize = settingsUI.getTileSize(); - long t = Stopwatch.GetTimestamp(); + long t = TickWatch.Ticks; Tuple, NavMesh> buildResult; if (settingsUI.isTiled()) @@ -559,7 +559,7 @@ public class RecastDemo sample.update(sample.getInputGeom(), buildResult.Item1, buildResult.Item2); sample.setChanged(false); - settingsUI.setBuildTime((Stopwatch.GetTimestamp() - t) / TimeSpan.TicksPerMillisecond); + settingsUI.setBuildTime((TickWatch.Ticks - t) / TimeSpan.TicksPerMillisecond); settingsUI.setBuildTelemetry(buildResult.Item1.Select(x => x.getTelemetry()).ToList()); toolsUI.setSample(sample); } diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs index 31a8369..6f6742f 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs @@ -239,7 +239,7 @@ public class CrowdProfilingTool public void update(float dt) { - long startTime = Stopwatch.GetTimestamp(); + long startTime = TickWatch.Ticks; if (crowd != null) { crowd.config().pathQueueSize = pathQueueSize; @@ -247,7 +247,7 @@ public class CrowdProfilingTool crowd.update(dt, null); } - long endTime = Stopwatch.GetTimestamp(); + long endTime = TickWatch.Ticks; if (crowd != null) { NavMeshQuery navquery = new NavMeshQuery(navMesh); diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs index 210e907..24f716e 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdTool.cs @@ -678,9 +678,9 @@ public class CrowdTool : Tool if (nav == null) return; - long startTime = Stopwatch.GetTimestamp(); + long startTime = TickWatch.Ticks; crowd.update(dt, m_agentDebug); - long endTime = Stopwatch.GetTimestamp(); + long endTime = TickWatch.Ticks; // Update agent trails foreach (CrowdAgent ag in crowd.getActiveAgents()) diff --git a/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs b/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs index e654d65..127c7c3 100644 --- a/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs @@ -204,9 +204,9 @@ public class DynamicUpdateTool : Tool { Vector3f sp = Vector3f.Of(spos[0], spos[1] + 1.3f, spos[2]); Vector3f ep = Vector3f.Of(epos[0], epos[1] + 1.3f, epos[2]); - long t1 = Stopwatch.GetTimestamp(); + long t1 = TickWatch.Ticks; float? hitPos = dynaMesh.voxelQuery().raycast(sp, ep); - long t2 = Stopwatch.GetTimestamp(); + long t2 = TickWatch.Ticks; raycastTime = (t2 - t1) / TimeSpan.TicksPerMillisecond; raycastHit = hitPos.HasValue; raycastHitPos = hitPos.HasValue @@ -498,13 +498,13 @@ public class DynamicUpdateTool : Tool private void updateDynaMesh() { - long t = Stopwatch.GetTimestamp(); + long t = TickWatch.Ticks; try { bool updated = dynaMesh.update(executor).Result; if (updated) { - buildTime = (Stopwatch.GetTimestamp() - t) / TimeSpan.TicksPerMillisecond; + buildTime = (TickWatch.Ticks - t) / TimeSpan.TicksPerMillisecond; sample.update(null, dynaMesh.recastResults(), dynaMesh.navMesh()); sample.setChanged(false); } @@ -727,7 +727,7 @@ public class DynamicUpdateTool : Tool private void buildDynaMesh() { configDynaMesh(); - long t = Stopwatch.GetTimestamp(); + long t = TickWatch.Ticks; try { var _ = dynaMesh.build(executor).Result; @@ -737,7 +737,7 @@ public class DynamicUpdateTool : Tool Console.WriteLine(e); } - buildTime = (Stopwatch.GetTimestamp() - t) / TimeSpan.TicksPerMillisecond; + buildTime = (TickWatch.Ticks - t) / TimeSpan.TicksPerMillisecond; sample.update(null, dynaMesh.recastResults(), dynaMesh.navMesh()); } diff --git a/src/DotRecast.Recast/Telemetry.cs b/src/DotRecast.Recast/Telemetry.cs index 0655c66..bed10fb 100644 --- a/src/DotRecast.Recast/Telemetry.cs +++ b/src/DotRecast.Recast/Telemetry.cs @@ -33,14 +33,14 @@ namespace DotRecast.Recast public void startTimer(string name) { - timerStart.Value[name] = new AtomicLong(Stopwatch.GetTimestamp()); + timerStart.Value[name] = new AtomicLong(TickWatch.Ticks); } public void stopTimer(string name) { timerAccum .GetOrAdd(name, _ => new AtomicLong(0)) - .AddAndGet(Stopwatch.GetTimestamp() - timerStart.Value?[name].Read() ?? 0); + .AddAndGet(TickWatch.Ticks - timerStart.Value?[name].Read() ?? 0); } public void warn(string @string) diff --git a/test/DotRecast.Detour.Test/RandomPointTest.cs b/test/DotRecast.Detour.Test/RandomPointTest.cs index 57813fd..7c1bcf4 100644 --- a/test/DotRecast.Detour.Test/RandomPointTest.cs +++ b/test/DotRecast.Detour.Test/RandomPointTest.cs @@ -18,6 +18,7 @@ freely, subject to the following restrictions: using System; using System.Diagnostics; +using DotRecast.Core; using NUnit.Framework; using static DotRecast.Core.RecastMath; @@ -120,19 +121,19 @@ public class RandomPointTest : AbstractDetourTest query.findRandomPointWithinCircle(point.getRandomRef(), point.getRandomPt(), radius, filter, f); } - long t1 = Stopwatch.GetTimestamp(); + long t1 = TickWatch.Ticks; for (int i = 0; i < 10000; i++) { query.findRandomPointAroundCircle(point.getRandomRef(), point.getRandomPt(), radius, filter, f); } - long t2 = Stopwatch.GetTimestamp(); + long t2 = TickWatch.Ticks; for (int i = 0; i < 10000; i++) { query.findRandomPointWithinCircle(point.getRandomRef(), point.getRandomPt(), radius, filter, f); } - long t3 = Stopwatch.GetTimestamp(); + long t3 = TickWatch.Ticks; Console.WriteLine("Random point around circle: " + (t2 - t1) / TimeSpan.TicksPerMillisecond + "ms"); Console.WriteLine("Random point within circle: " + (t3 - t2) / TimeSpan.TicksPerMillisecond + "ms"); } diff --git a/test/DotRecast.Detour.TileCache.Test/TileCacheTest.cs b/test/DotRecast.Detour.TileCache.Test/TileCacheTest.cs index 3d417ef..d480aaa 100644 --- a/test/DotRecast.Detour.TileCache.Test/TileCacheTest.cs +++ b/test/DotRecast.Detour.TileCache.Test/TileCacheTest.cs @@ -188,20 +188,20 @@ public class TileCacheTest : AbstractTileCacheTest layerBuilder.build(order, cCompatibility, threads); } - long t1 = Stopwatch.GetTimestamp(); + long t1 = TickWatch.Ticks; List layers = null; for (int i = 0; i < 8; i++) { layers = layerBuilder.build(order, cCompatibility, 1); } - long t2 = Stopwatch.GetTimestamp(); + long t2 = TickWatch.Ticks; for (int i = 0; i < 8; i++) { layers = layerBuilder.build(order, cCompatibility, threads); } - long t3 = Stopwatch.GetTimestamp(); + long t3 = TickWatch.Ticks; Console.WriteLine(" Time ST : " + (t2 - t1) / TimeSpan.TicksPerMillisecond); Console.WriteLine(" Time MT : " + (t3 - t2) / TimeSpan.TicksPerMillisecond); TileCache tc = getTileCache(geom, order, cCompatibility); diff --git a/test/DotRecast.Recast.Test/RecastSoloMeshTest.cs b/test/DotRecast.Recast.Test/RecastSoloMeshTest.cs index 00d16cd..62f722f 100644 --- a/test/DotRecast.Recast.Test/RecastSoloMeshTest.cs +++ b/test/DotRecast.Recast.Test/RecastSoloMeshTest.cs @@ -96,7 +96,7 @@ public class RecastSoloMeshTest { m_partitionType = partitionType; InputGeomProvider geomProvider = ObjImporter.load(Loader.ToBytes(filename)); - long time = Stopwatch.GetTimestamp(); + long time = TickWatch.Ticks; Vector3f bmin = geomProvider.getMeshBoundsMin(); Vector3f bmax = geomProvider.getMeshBoundsMax(); Telemetry m_ctx = new Telemetry(); @@ -204,7 +204,7 @@ public class RecastSoloMeshTest // you use tiles) // * good choice to use for tiled navmesh with medium and small sized // tiles - long time3 = Stopwatch.GetTimestamp(); + long time3 = TickWatch.Ticks; if (m_partitionType == PartitionType.WATERSHED) { @@ -256,7 +256,7 @@ public class RecastSoloMeshTest Assert.That(m_dmesh.nmeshes, Is.EqualTo(expDetMeshes), "Mesh Detail Meshes"); Assert.That(m_dmesh.nverts, Is.EqualTo(expDetVerts), "Mesh Detail Verts"); Assert.That(m_dmesh.ntris, Is.EqualTo(expDetTris), "Mesh Detail Tris"); - long time2 = Stopwatch.GetTimestamp(); + long time2 = TickWatch.Ticks; Console.WriteLine(filename + " : " + partitionType + " " + (time2 - time) / TimeSpan.TicksPerMillisecond + " ms"); Console.WriteLine(" " + (time3 - time) / TimeSpan.TicksPerMillisecond + " ms"); saveObj(filename.Substring(0, filename.LastIndexOf('.')) + "_" + partitionType + "_detail.obj", m_dmesh); diff --git a/test/DotRecast.Recast.Test/RecastTileMeshTest.cs b/test/DotRecast.Recast.Test/RecastTileMeshTest.cs index f8f0182..633b032 100644 --- a/test/DotRecast.Recast.Test/RecastTileMeshTest.cs +++ b/test/DotRecast.Recast.Test/RecastTileMeshTest.cs @@ -106,19 +106,19 @@ public class RecastTileMeshTest build(geom, builder, cfg, 4, true); } - long t1 = Stopwatch.GetTimestamp(); + long t1 = TickWatch.Ticks; for (int i = 0; i < 4; i++) { build(geom, builder, cfg, 1, false); } - long t2 = Stopwatch.GetTimestamp(); + long t2 = TickWatch.Ticks; for (int i = 0; i < 4; i++) { build(geom, builder, cfg, 4, false); } - long t3 = Stopwatch.GetTimestamp(); + long t3 = TickWatch.Ticks; Console.WriteLine(" Time ST : " + (t2 - t1) / TimeSpan.TicksPerMillisecond); Console.WriteLine(" Time MT : " + (t3 - t2) / TimeSpan.TicksPerMillisecond); }