Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace Google.ProtocolBuffers.ProtoBench
  5. {
  6. /// <summary>
  7. /// Simple benchmarking of arbitrary messages.
  8. /// </summary>
  9. public sealed class Program {
  10. private static readonly TimeSpan MinSampleTime = TimeSpan.FromSeconds(2);
  11. private static readonly TimeSpan TargetTime = TimeSpan.FromSeconds(30);
  12. // Avoid a .NET 3.5 dependency
  13. delegate void Action();
  14. public static int Main(string[] args) {
  15. if (args.Length < 2 || (args.Length % 2) != 0) {
  16. Console.Error.WriteLine("Usage: ProtoBench <descriptor type name> <input data>");
  17. Console.Error.WriteLine("The descriptor type name is the fully-qualified message name,");
  18. Console.Error.WriteLine("including assembly - e.g. Google.ProtocolBuffers.BenchmarkProtos.Message1,ProtoBench");
  19. Console.Error.WriteLine("(You can specify multiple pairs of descriptor type name and input data.)");
  20. return 1;
  21. }
  22. bool success = true;
  23. for (int i = 0; i < args.Length; i += 2) {
  24. success &= RunTest(args[i], args[i + 1]);
  25. }
  26. return success ? 0 : 1;
  27. }
  28. /// <summary>
  29. /// Runs a single test. Error messages are displayed to Console.Error, and the return value indicates
  30. /// general success/failure.
  31. /// </summary>
  32. public static bool RunTest(string typeName, string file) {
  33. Console.WriteLine("Benchmarking {0} with file {1}", typeName, file);
  34. IMessage defaultMessage;
  35. try {
  36. defaultMessage = MessageUtil.GetDefaultMessage(typeName);
  37. } catch (ArgumentException e) {
  38. Console.Error.WriteLine(e.Message);
  39. return false;
  40. }
  41. try {
  42. byte[] inputData = File.ReadAllBytes(file);
  43. MemoryStream inputStream = new MemoryStream(inputData);
  44. ByteString inputString = ByteString.CopyFrom(inputData);
  45. IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString).WeakBuild();
  46. sampleMessage.ToByteString();
  47. Benchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString());
  48. Benchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray());
  49. Benchmark("Serialize to memory stream", inputData.Length, () => sampleMessage.WriteTo(new MemoryStream()));
  50. Benchmark("Deserialize from byte string", inputData.Length,
  51. () => defaultMessage.WeakCreateBuilderForType()
  52. .WeakMergeFrom(inputString)
  53. .WeakBuild()
  54. );
  55. Benchmark("Deserialize from byte array", inputData.Length,
  56. () => defaultMessage.WeakCreateBuilderForType()
  57. .WeakMergeFrom(CodedInputStream.CreateInstance(inputData))
  58. .WeakBuild()
  59. );
  60. Benchmark("Deserialize from memory stream", inputData.Length, () => {
  61. inputStream.Position = 0;
  62. defaultMessage.WeakCreateBuilderForType()
  63. .WeakMergeFrom(CodedInputStream.CreateInstance(inputStream))
  64. .WeakBuild();
  65. });
  66. return true;
  67. } catch (Exception e) {
  68. Console.Error.WriteLine("Error: {0}", e.Message);
  69. Console.Error.WriteLine();
  70. Console.Error.WriteLine("Detailed exception information: {0}", e);
  71. return false;
  72. }
  73. }
  74. private static void Benchmark(string name, long dataSize, Action action) {
  75. // Make sure it's JITted
  76. action();
  77. // Run it progressively more times until we've got a reasonable sample
  78. int iterations = 1;
  79. TimeSpan elapsed = TimeAction(action, iterations);
  80. while (elapsed < MinSampleTime) {
  81. iterations *= 2;
  82. elapsed = TimeAction(action, iterations);
  83. }
  84. // Upscale the sample to the target time. Do this in floating point arithmetic
  85. // to avoid overflow issues.
  86. iterations = (int) ((TargetTime.Ticks / (double)elapsed.Ticks) * iterations);
  87. elapsed = TimeAction(action, iterations);
  88. Console.WriteLine("{0}: {1} iterations in {2:f3}s; {3:f3}MB/s",
  89. name, iterations, elapsed.TotalSeconds,
  90. (iterations * dataSize) / (elapsed.TotalSeconds * 1024 * 1024));
  91. }
  92. private static TimeSpan TimeAction(Action action, int iterations) {
  93. Stopwatch sw = Stopwatch.StartNew();
  94. for (int i = 0; i < iterations; i++) {
  95. action();
  96. }
  97. sw.Stop();
  98. return sw.Elapsed;
  99. }
  100. }
  101. }