Jon Skeet | 8f8186a | 2009-01-16 10:57:40 +0000 | [diff] [blame] | 1 | using System; |
| 2 | using System.Diagnostics; |
| 3 | using System.IO; |
| 4 | using Google.ProtocolBuffers; |
| 5 | |
| 6 | namespace 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) { |
| 19 | if (args.Length != 2) { |
| 20 | Console.Error.WriteLine("Usage: ProtoBecnh <descriptor type name> <input data>"); |
| 21 | 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"); |
| 23 | return 1; |
| 24 | } |
| 25 | IMessage defaultMessage; |
| 26 | try { |
| 27 | defaultMessage = MessageUtil.GetDefaultMessage(args[0]); |
| 28 | } catch (ArgumentException e) { |
| 29 | Console.Error.WriteLine(e.Message); |
| 30 | return 1; |
| 31 | } |
| 32 | try { |
| 33 | IBuilder builder = defaultMessage.WeakCreateBuilderForType(); |
| 34 | byte[] inputData = File.ReadAllBytes(args[1]); |
| 35 | ByteString inputString = ByteString.CopyFrom(inputData); |
| 36 | IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString).WeakBuild(); |
| 37 | sampleMessage.ToByteString(); |
| 38 | Console.WriteLine("Benchmarking {0} with file {1}", sampleMessage.GetType().Name, args[1]); |
| 39 | Benchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString()); |
| 40 | Benchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray()); |
| 41 | Benchmark("Serialize to memory stream", inputData.Length, () => sampleMessage.WriteTo(new MemoryStream())); |
| 42 | Benchmark("Deserialize from byte array", inputData.Length, () => |
| 43 | defaultMessage.WeakCreateBuilderForType() |
| 44 | .WeakMergeFrom(CodedInputStream.CreateInstance(inputData)) |
| 45 | .WeakBuild() |
| 46 | ); |
| 47 | Benchmark("Deserialize from byte array", inputData.Length, () => |
| 48 | defaultMessage.WeakCreateBuilderForType() |
| 49 | .WeakMergeFrom(inputString) |
| 50 | .WeakBuild() |
| 51 | ); |
| 52 | return 0; |
| 53 | } catch (Exception e) { |
| 54 | Console.Error.WriteLine("Error: {0}", e.Message); |
| 55 | Console.Error.WriteLine(); |
| 56 | Console.Error.WriteLine("Detailed exception information: {0}", e); |
| 57 | return 1; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static void Benchmark(string name, int dataSize, Action action) { |
| 62 | // Make sure it's JITted |
| 63 | action(); |
| 64 | // Run it progressively more times until we've got a reasonable sample |
| 65 | |
| 66 | int iterations = 1; |
| 67 | TimeSpan elapsed = TimeAction(action, iterations); |
| 68 | while (elapsed < MinSampleTime) { |
| 69 | iterations *= 2; |
| 70 | elapsed = TimeAction(action, iterations); |
| 71 | } |
| 72 | // Upscale the sample to the target time. Do this in floating point arithmetic |
| 73 | // to avoid overflow issues. |
| 74 | iterations = (int) ((TargetTime.Ticks / (double)elapsed.Ticks) * iterations); |
| 75 | elapsed = TimeAction(action, iterations); |
| 76 | Console.WriteLine("{0}: {1} iterations in {2:f3}s; {3:f3}MB/s", |
| 77 | name, iterations, elapsed.TotalSeconds, |
| 78 | (iterations * dataSize) / (elapsed.TotalSeconds * 1024 * 1024)); |
| 79 | } |
| 80 | |
| 81 | private static TimeSpan TimeAction(Action action, int iterations) { |
| 82 | Stopwatch sw = Stopwatch.StartNew(); |
| 83 | for (int i = 0; i < iterations; i++) { |
| 84 | action(); |
| 85 | } |
| 86 | sw.Stop(); |
| 87 | return sw.Elapsed; |
| 88 | } |
| 89 | } |
| 90 | } |