From 78a62ddf98c39eed99048f1796463d202e6eca13 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20K=C3=B6llner?= <christian.koellner.80@gmail.com>
Date: Tue, 21 Apr 2020 19:02:18 +0200
Subject: [PATCH] perf. profiling

---
 Benchmarking/CapnpProfile/CapnpProfile.csproj    |  3 ++-
 Benchmarking/CapnpProfile/Program.cs             | 16 ++++++++++++++++
 .../Util/StrictlyOrderedAwaitTask.cs             |  3 +++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/Benchmarking/CapnpProfile/CapnpProfile.csproj b/Benchmarking/CapnpProfile/CapnpProfile.csproj
index f3757eb..3f857cb 100644
--- a/Benchmarking/CapnpProfile/CapnpProfile.csproj
+++ b/Benchmarking/CapnpProfile/CapnpProfile.csproj
@@ -9,11 +9,12 @@
     <DebugType>full</DebugType>
     <DebugSymbols>true</DebugSymbols>
     <PlatformTarget>x64</PlatformTarget>
+    <DefineConstants>SOTASK_PERF</DefineConstants>
   </PropertyGroup>
 
   <ItemGroup>
     <PackageReference Include="Capnp.Net.Runtime" Version="1.3.89-gdebe76be89" />
-    <PackageReference Include="CapnpC.CSharp.MsBuild.Generation" Version="1.3.89-gdebe76be89" />
+    <PackageReference Include="CapnpC.CSharp.MsBuild.Generation" Version="1.3.90-g65e87e5aa9" />
   </ItemGroup>
 
 </Project>
diff --git a/Benchmarking/CapnpProfile/Program.cs b/Benchmarking/CapnpProfile/Program.cs
index 274bb97..f2ddd00 100644
--- a/Benchmarking/CapnpProfile/Program.cs
+++ b/Benchmarking/CapnpProfile/Program.cs
@@ -21,11 +21,27 @@ namespace CapnpProfile
             var payload = new byte[20];
             new Random().NextBytes(payload);
 
+#if SOTASK_PERF
+            int counter = 0;
+#endif
+
             while (true)
             {
                 var result = await echoer.Echo(payload);
                 if (result.Count != payload.Length)
                     throw new InvalidOperationException("Echo server malfunction");
+
+#if SOTASK_PERF
+                if (++counter == 1000)
+                {
+                    counter = 0;
+
+                    Console.WriteLine($"StrictlyOrderedTask performance statistics:");
+                    Console.WriteLine($"AwaitInternal: max. {Capnp.Util.StrictlyOrderedTaskExtensions.Stats.AwaitInternalMaxOuterIterations} outer iterations");
+                    Console.WriteLine($"AwaitInternal: max. {Capnp.Util.StrictlyOrderedTaskExtensions.Stats.AwaitInternalMaxInnerIterations} inner iterations");
+                    Console.WriteLine($"OnCompleted: max. {Capnp.Util.StrictlyOrderedTaskExtensions.Stats.OnCompletedMaxSpins} iterations");
+                }
+#endif
             }
         }
     }
diff --git a/Capnp.Net.Runtime/Util/StrictlyOrderedAwaitTask.cs b/Capnp.Net.Runtime/Util/StrictlyOrderedAwaitTask.cs
index f86cf53..c5137c9 100644
--- a/Capnp.Net.Runtime/Util/StrictlyOrderedAwaitTask.cs
+++ b/Capnp.Net.Runtime/Util/StrictlyOrderedAwaitTask.cs
@@ -240,6 +240,9 @@ namespace Capnp.Util
             }
         }
 
+        /// <summary>
+        /// Performance profiling statistics
+        /// </summary>
         public static readonly Statistics Stats = new Statistics();
 #endif
         /// <summary>