Skip to content

Commit 014e33d

Browse files
committed
.NET: Prettier RE2 bench
1 parent 72c3566 commit 014e33d

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

csharp-api/test/Test/TestRE2.cs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
public class TestRE2Plugin {
1313
static bool IsRunningRE2 => Environment.ProcessPath.Contains("re2", StringComparison.CurrentCultureIgnoreCase);
14+
static System.Diagnostics.Stopwatch imguiStopwatch = new();
1415

1516
[REFrameworkNET.Attributes.PluginEntryPoint]
1617
public static void Main() {
@@ -24,10 +25,38 @@ public static void Main() {
2425
RE2HookBenchmark.InstallHook();
2526

2627
ImGuiRender.Pre += () => {
28+
var deltaSeconds = imguiStopwatch.Elapsed.TotalMilliseconds / 1000.0;
29+
imguiStopwatch.Restart();
30+
2731
if (ImGui.Begin("Test Window")) {
2832
ImGui.Text("RE2");
2933
ImGui.Separator();
3034

35+
36+
if (ImGui.TreeNode("Player")) {
37+
var playerManager = API.GetManagedSingletonT<app.ropeway.PlayerManager>();
38+
var player = playerManager.get_CurrentPlayer();
39+
if (player != null) {
40+
ImGui.Text("Player is not null");
41+
} else {
42+
ImGui.Text("Player is null");
43+
}
44+
ImGui.TreePop();
45+
}
46+
47+
ImGui.End();
48+
}
49+
50+
// ROund the window
51+
ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding, 10.0f);
52+
53+
ImGui.SetNextWindowSize(new System.Numerics.Vector2(500, 500), ImGuiCond.FirstUseEver);
54+
if (ImGui.Begin("RE2 Bench")) {
55+
56+
ImGui.PushStyleColor(ImGuiCol.Text, new System.Numerics.Vector4(0.5f, 1f, 0.4f, 1.0f));
57+
ImGui.PlotLines("Overall Benchmark", ref RE2HookBenchmark.BenchmarkData[0], 1000, RE2HookBenchmark.MeasureCount % 1000, RE2HookBenchmark.RunningAvg.ToString("0.000") + " µs", 0, (float)RE2HookBenchmark.RunningAvg * 2.0f, new System.Numerics.Vector2(0, 40));
58+
ImGui.PopStyleColor();
59+
3160
System.Collections.Generic.List<long> threadRanks = new();
3261

3362
foreach(var tdata in RE2HookBenchmark.threadData) {
@@ -43,34 +72,36 @@ public static void Main() {
4372

4473
var totalThreadRanks = threadRanks.Count;
4574

75+
System.Collections.Generic.List<RE2HookBenchmark.ThreadData> threadData = new();
76+
4677
foreach(var tdata in RE2HookBenchmark.threadData) {
47-
var rank = threadRanks.IndexOf(tdata.Value.threadID) + 1;
48-
var greenColor = 1.0f - (float)rank / (float)totalThreadRanks;
49-
var redColor = (float)rank / (float)totalThreadRanks;
78+
threadData.Add(tdata.Value);
79+
}
80+
81+
threadData.Sort((a, b) => {
82+
return a.lerp.CompareTo(b.lerp);
83+
});
84+
85+
foreach(var tdata in threadData) {
86+
var rank = threadRanks.IndexOf(tdata.threadID) + 1;
87+
var rankRatio = (double)rank / (double)totalThreadRanks;
88+
var towards = Math.Max(80.0 * rankRatio, 20.0);
89+
tdata.lerp = towards * deltaSeconds + tdata.lerp * (1.0 - deltaSeconds);
90+
var lerpRatio = tdata.lerp / 80.0;
91+
92+
var greenColor = 1.0 - lerpRatio;
93+
var redColor = lerpRatio;
5094

5195
//ImGui.Text("Thread ID: " + tdata.Value.threadID + " Avg: " + tdata.Value.runningAvg.ToString("0.000") + " µs");
52-
ImGui.PushStyleColor(ImGuiCol.Text, new System.Numerics.Vector4(redColor, greenColor, 0f, 1.0f));
53-
ImGui.PlotLines("Thread " + tdata.Value.threadID, ref tdata.Value.benchmarkData[0], 1000, tdata.Value.callCount % 1000, tdata.Value.runningAvg.ToString("0.000") + " µs", 0, (float)tdata.Value.runningAvg * 2.0f, new System.Numerics.Vector2(0, 30));
96+
ImGui.PushStyleColor(ImGuiCol.Text, new System.Numerics.Vector4((float)redColor, (float)greenColor, 0f, 1.0f));
97+
ImGui.PlotLines("Thread " + tdata.threadID, ref tdata.benchmarkData[0], 1000, tdata.callCount % 1000, tdata.runningAvg.ToString("0.000") + " µs", (float)tdata.runningAvg, (float)tdata.runningAvg * 2.0f, new System.Numerics.Vector2(0, (int)tdata.lerp));
5498
ImGui.PopStyleColor();
5599
}
56100

57-
ImGui.PushStyleColor(ImGuiCol.Text, new System.Numerics.Vector4(0.5f, 1f, 0.4f, 1.0f));
58-
ImGui.PlotLines("Overall Benchmark", ref RE2HookBenchmark.BenchmarkData[0], 1000, RE2HookBenchmark.MeasureCount % 1000, RE2HookBenchmark.RunningAvg.ToString("0.000") + " µs", 0, (float)RE2HookBenchmark.RunningAvg * 2.0f, new System.Numerics.Vector2(0, 40));
59-
ImGui.PopStyleColor();
60-
61-
if (ImGui.TreeNode("Player")) {
62-
var playerManager = API.GetManagedSingletonT<app.ropeway.PlayerManager>();
63-
var player = playerManager.get_CurrentPlayer();
64-
if (player != null) {
65-
ImGui.Text("Player is not null");
66-
} else {
67-
ImGui.Text("Player is null");
68-
}
69-
ImGui.TreePop();
70-
}
71-
72101
ImGui.End();
73102
}
103+
104+
ImGui.PopStyleVar();
74105
};
75106

76107
// Benchmarking the effects of threading on invoking game code
@@ -160,6 +191,7 @@ internal class ThreadData {
160191
internal double highestMicros;
161192
internal double runningAvg;
162193
internal float[] benchmarkData = new float[1000];
194+
internal double lerp = 40.0;
163195
};
164196

165197
internal static System.Collections.Concurrent.ConcurrentDictionary<long, ThreadData> threadData = new(Environment.ProcessorCount * 2, 8192);

0 commit comments

Comments
 (0)