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