aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/TestBed/Program.cs
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/TestBed/Program.cs')
-rw-r--r--csharp/TestBed/Program.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/csharp/TestBed/Program.cs b/csharp/TestBed/Program.cs
new file mode 100644
index 00000000..4ba70cea
--- /dev/null
+++ b/csharp/TestBed/Program.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+
+namespace TestBed {
+
+ // Avoid using the .NET 3.5 System.Action delegate
+ delegate void Action();
+
+ class Program {
+
+ private static readonly TimeSpan TimeLimit = TimeSpan.FromMinutes(1);
+ private const int IterationsPerChunk = 50;
+
+ static void Main(string[] args) {
+
+ // Deserialize once to warm up the JIT and give us data use later
+ byte[] data = File.ReadAllBytes(args[0]);
+ Northwind.Database fast = Northwind.Database.ParseFrom(data);
+ SlowNorthwind.Database slow = SlowNorthwind.Database.ParseFrom(data);
+
+ Benchmark("Fast deserialize", () => Northwind.Database.ParseFrom(data));
+ Benchmark("Fast serialize", () => fast.ToByteArray());
+ Benchmark("Slow deserialize", () => SlowNorthwind.Database.ParseFrom(data));
+ Benchmark("Slow serialize", () => slow.ToByteArray());
+ //Console.ReadLine();
+ }
+
+ private static void Benchmark(string description, Action actionUnderTest) {
+ int totalIterations = 0;
+ Stopwatch sw = Stopwatch.StartNew();
+ while (sw.Elapsed < TimeLimit) {
+ for (int i = 0; i < IterationsPerChunk; i++) {
+ actionUnderTest();
+ }
+ totalIterations += IterationsPerChunk;
+ }
+ sw.Stop();
+ Console.WriteLine("{0}: {1} iterations in {2}ms; {3:f2}ms per iteration",
+ description, totalIterations, sw.ElapsedMilliseconds,
+ (double)sw.ElapsedMilliseconds / totalIterations);
+ }
+ }
+}