blob: c6b526483540e4e5591cb108bd7637a8c36cb1b9 [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();
Jon Skeet8f8186a2009-01-16 10:57:40 +000051 Benchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString());
52 Benchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray());
53 Benchmark("Serialize to memory stream", inputData.Length, () => sampleMessage.WriteTo(new MemoryStream()));
Jon Skeeta3767342009-01-16 18:06:56 +000054 Benchmark("Deserialize from byte string", inputData.Length,
55 () => defaultMessage.WeakCreateBuilderForType()
56 .WeakMergeFrom(inputString)
57 .WeakBuild()
Jon Skeet79c72a92009-01-16 13:19:31 +000058 );
Jon Skeeta3767342009-01-16 18:06:56 +000059 Benchmark("Deserialize from byte array", inputData.Length,
60 () => defaultMessage.WeakCreateBuilderForType()
61 .WeakMergeFrom(CodedInputStream.CreateInstance(inputData))
62 .WeakBuild()
Jon Skeet8f8186a2009-01-16 10:57:40 +000063 );
Jon Skeeta3767342009-01-16 18:06:56 +000064 Benchmark("Deserialize from memory stream", inputData.Length, () => {
65 inputStream.Position = 0;
Jon Skeet8f8186a2009-01-16 10:57:40 +000066 defaultMessage.WeakCreateBuilderForType()
Jon Skeeta3767342009-01-16 18:06:56 +000067 .WeakMergeFrom(CodedInputStream.CreateInstance(inputStream))
68 .WeakBuild();
69 });
Jon Skeet75f42682009-03-05 14:22:28 +000070 Console.WriteLine();
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) {
Jon Skeet75f42682009-03-05 14:22:28 +0000101 GC.Collect();
102 GC.WaitForPendingFinalizers();
Jon Skeet8f8186a2009-01-16 10:57:40 +0000103 Stopwatch sw = Stopwatch.StartNew();
104 for (int i = 0; i < iterations; i++) {
105 action();
106 }
107 sw.Stop();
108 return sw.Elapsed;
109 }
110 }
Jon Skeeta3767342009-01-16 18:06:56 +0000111}