blob: 91604f3c1d53c2ea1476b344d47705277056711f [file] [log] [blame]
Jon Skeet8f8186a2009-01-16 10:57:40 +00001using System;
2using System.Diagnostics;
3using System.IO;
Jon Skeet8f8186a2009-01-16 10:57:40 +00004
Jon Skeeta3767342009-01-16 18:06:56 +00005namespace Google.ProtocolBuffers.ProtoBench
6{
Jon Skeet8f8186a2009-01-16 10:57:40 +00007 /// <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 Skeeta3767342009-01-16 18:06:56 +000055 Benchmark("Deserialize from byte string", inputData.Length,
56 () => defaultMessage.WeakCreateBuilderForType()
57 .WeakMergeFrom(inputString)
58 .WeakBuild()
Jon Skeet79c72a92009-01-16 13:19:31 +000059 );
Jon Skeeta3767342009-01-16 18:06:56 +000060 Benchmark("Deserialize from byte array", inputData.Length,
61 () => defaultMessage.WeakCreateBuilderForType()
62 .WeakMergeFrom(CodedInputStream.CreateInstance(inputData))
63 .WeakBuild()
Jon Skeet8f8186a2009-01-16 10:57:40 +000064 );
Jon Skeeta3767342009-01-16 18:06:56 +000065 Benchmark("Deserialize from memory stream", inputData.Length, () => {
66 inputStream.Position = 0;
Jon Skeet8f8186a2009-01-16 10:57:40 +000067 defaultMessage.WeakCreateBuilderForType()
Jon Skeeta3767342009-01-16 18:06:56 +000068 .WeakMergeFrom(CodedInputStream.CreateInstance(inputStream))
69 .WeakBuild();
70 });
Jon Skeet79c72a92009-01-16 13:19:31 +000071 return true;
Jon Skeet8f8186a2009-01-16 10:57:40 +000072 } catch (Exception e) {
73 Console.Error.WriteLine("Error: {0}", e.Message);
74 Console.Error.WriteLine();
75 Console.Error.WriteLine("Detailed exception information: {0}", e);
Jon Skeet79c72a92009-01-16 13:19:31 +000076 return false;
Jon Skeet8f8186a2009-01-16 10:57:40 +000077 }
78 }
79
Jon Skeet79c72a92009-01-16 13:19:31 +000080 private static void Benchmark(string name, long dataSize, Action action) {
Jon Skeet8f8186a2009-01-16 10:57:40 +000081 // Make sure it's JITted
82 action();
83 // Run it progressively more times until we've got a reasonable sample
84
85 int iterations = 1;
86 TimeSpan elapsed = TimeAction(action, iterations);
87 while (elapsed < MinSampleTime) {
88 iterations *= 2;
89 elapsed = TimeAction(action, iterations);
90 }
91 // Upscale the sample to the target time. Do this in floating point arithmetic
92 // to avoid overflow issues.
93 iterations = (int) ((TargetTime.Ticks / (double)elapsed.Ticks) * iterations);
94 elapsed = TimeAction(action, iterations);
95 Console.WriteLine("{0}: {1} iterations in {2:f3}s; {3:f3}MB/s",
Jon Skeeta3767342009-01-16 18:06:56 +000096 name, iterations, elapsed.TotalSeconds,
97 (iterations * dataSize) / (elapsed.TotalSeconds * 1024 * 1024));
Jon Skeet8f8186a2009-01-16 10:57:40 +000098 }
99
100 private static TimeSpan TimeAction(Action action, int iterations) {
101 Stopwatch sw = Stopwatch.StartNew();
102 for (int i = 0; i < iterations; i++) {
103 action();
104 }
105 sw.Stop();
106 return sw.Elapsed;
107 }
108 }
Jon Skeeta3767342009-01-16 18:06:56 +0000109}