From 27cbfe8f4c3130548d08da5fb0e3ad1ea64f6997 Mon Sep 17 00:00:00 2001
From: Jury Soldatenkov <yurysold@gmail.com>
Date: Wed, 11 Mar 2020 23:57:43 +0300
Subject: [PATCH] Use oakton to issue different commands for benchmark project
 (relates to #9)

---
 src/Benchmark/Benchmark.csproj           |  1 +
 src/Benchmark/Commands/BenchCommand.cs   | 14 +++++++++++
 src/Benchmark/Commands/BenchInput.cs     |  6 +++++
 src/Benchmark/Commands/ProfileCommand.cs | 31 ++++++++++++++++++++++++
 src/Benchmark/Commands/ProfileInput.cs   |  7 ++++++
 src/Benchmark/Program.cs                 | 14 ++++++++++-
 6 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 src/Benchmark/Commands/BenchCommand.cs
 create mode 100644 src/Benchmark/Commands/BenchInput.cs
 create mode 100644 src/Benchmark/Commands/ProfileCommand.cs
 create mode 100644 src/Benchmark/Commands/ProfileInput.cs

diff --git a/src/Benchmark/Benchmark.csproj b/src/Benchmark/Benchmark.csproj
index 582fcbf..735fc7e 100644
--- a/src/Benchmark/Benchmark.csproj
+++ b/src/Benchmark/Benchmark.csproj
@@ -8,6 +8,7 @@
 
   <ItemGroup>
     <PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
+    <PackageReference Include="Oakton" Version="2.0.3" />
   </ItemGroup>
 
   <ItemGroup>
diff --git a/src/Benchmark/Commands/BenchCommand.cs b/src/Benchmark/Commands/BenchCommand.cs
new file mode 100644
index 0000000..50c7387
--- /dev/null
+++ b/src/Benchmark/Commands/BenchCommand.cs
@@ -0,0 +1,14 @@
+using BenchmarkDotNet.Running;
+using Oakton;
+
+namespace Benchmark.Commands
+{
+    public class BenchCommand : OaktonCommand<BenchInput>
+    {
+        public override bool Execute(BenchInput input)
+        {
+            BenchmarkRunner.Run<PingPongBenchmark>();
+            return true;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/Benchmark/Commands/BenchInput.cs b/src/Benchmark/Commands/BenchInput.cs
new file mode 100644
index 0000000..548d8c3
--- /dev/null
+++ b/src/Benchmark/Commands/BenchInput.cs
@@ -0,0 +1,6 @@
+namespace Benchmark.Commands
+{
+    public class BenchInput
+    {
+    }
+}
\ No newline at end of file
diff --git a/src/Benchmark/Commands/ProfileCommand.cs b/src/Benchmark/Commands/ProfileCommand.cs
new file mode 100644
index 0000000..38e7599
--- /dev/null
+++ b/src/Benchmark/Commands/ProfileCommand.cs
@@ -0,0 +1,31 @@
+using System;
+using Oakton;
+
+namespace Benchmark.Commands
+{
+    public class ProfileCommand : OaktonCommand<ProfileInput>
+    {
+        public override bool Execute(ProfileInput input)
+        {
+            var benchmark = new PingPongBenchmark();
+            try
+            {
+                Console.WriteLine("Starting setup");
+                benchmark.GlobalSetup();
+                benchmark.IterationSetup();
+
+                Console.WriteLine("Starting profiling");
+                benchmark.Run_ping_pong_benchmark();
+
+                Console.WriteLine("Press any key to exit");
+                Console.ReadKey();
+            }
+            finally
+            {
+                benchmark.GlobalCleanup();
+            }
+
+            return true;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/Benchmark/Commands/ProfileInput.cs b/src/Benchmark/Commands/ProfileInput.cs
new file mode 100644
index 0000000..81c4877
--- /dev/null
+++ b/src/Benchmark/Commands/ProfileInput.cs
@@ -0,0 +1,7 @@
+namespace Benchmark.Commands
+{
+    public class ProfileInput
+    {
+
+    }
+}
\ No newline at end of file
diff --git a/src/Benchmark/Program.cs b/src/Benchmark/Program.cs
index 8425fd0..f82fe6e 100644
--- a/src/Benchmark/Program.cs
+++ b/src/Benchmark/Program.cs
@@ -1,9 +1,21 @@
+using System.Reflection;
 using BenchmarkDotNet.Running;
+using Oakton;
 
 namespace Benchmark
 {
     internal class Program
     {
-        private static void Main() => BenchmarkRunner.Run<PingPongBenchmark>();
+        private static int Main(string[] args)
+        {
+            var executor = CommandExecutor.For(_ =>
+            {
+                // Find and apply all command classes discovered
+                // in this assembly
+                _.RegisterCommands(typeof(Program).GetTypeInfo().Assembly);
+            });
+
+            return executor.Execute(args);
+        }
     }
 }
\ No newline at end of file