Program.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace TestBed {
  5. // Avoid using the .NET 3.5 System.Action delegate
  6. delegate void Action();
  7. class Program {
  8. private static readonly TimeSpan TimeLimit = TimeSpan.FromMinutes(1);
  9. private const int IterationsPerChunk = 50;
  10. static void Main(string[] args) {
  11. // Deserialize once to warm up the JIT and give us data use later
  12. byte[] data = File.ReadAllBytes(args[0]);
  13. Northwind.Database fast = Northwind.Database.ParseFrom(data);
  14. SlowNorthwind.Database slow = SlowNorthwind.Database.ParseFrom(data);
  15. Benchmark("Fast deserialize", () => Northwind.Database.ParseFrom(data));
  16. Benchmark("Fast serialize", () => fast.ToByteArray());
  17. Benchmark("Slow deserialize", () => SlowNorthwind.Database.ParseFrom(data));
  18. Benchmark("Slow serialize", () => slow.ToByteArray());
  19. //Console.ReadLine();
  20. }
  21. private static void Benchmark(string description, Action actionUnderTest) {
  22. int totalIterations = 0;
  23. Stopwatch sw = Stopwatch.StartNew();
  24. while (sw.Elapsed < TimeLimit) {
  25. for (int i = 0; i < IterationsPerChunk; i++) {
  26. actionUnderTest();
  27. }
  28. totalIterations += IterationsPerChunk;
  29. }
  30. sw.Stop();
  31. Console.WriteLine("{0}: {1} iterations in {2}ms; {3:f2}ms per iteration",
  32. description, totalIterations, sw.ElapsedMilliseconds,
  33. (double)sw.ElapsedMilliseconds / totalIterations);
  34. }
  35. }
  36. }