From 8dc7b830a3b2e11348e8e498bce02b97095833e6 Mon Sep 17 00:00:00 2001 From: csharptest Date: Thu, 14 Jul 2011 13:06:34 -0500 Subject: [PATCH] Reformatted to include braces --- src/ProtoBench/Program.cs | 244 ++++++++++++++++++++++++++------------ 1 file changed, 166 insertions(+), 78 deletions(-) diff --git a/src/ProtoBench/Program.cs b/src/ProtoBench/Program.cs index f929f7d552..90d9438427 100644 --- a/src/ProtoBench/Program.cs +++ b/src/ProtoBench/Program.cs @@ -41,6 +41,7 @@ using System.IO; using System.Runtime.Serialization.Json; using System.Text; using System.Threading; +using System.Xml; using Google.ProtocolBuffers.Serialization; using Google.ProtocolBuffers.TestProtos; @@ -70,7 +71,9 @@ namespace Google.ProtocolBuffers.ProtoBench OtherFormats = temp.Remove("/formats") || temp.Remove("-formats"); if (true == (FastTest = (temp.Remove("/fast") || temp.Remove("-fast")))) + { TargetTime = TimeSpan.FromSeconds(10); + } RunBenchmark = BenchmarkV1; if (temp.Remove("/v2") || temp.Remove("-v2")) @@ -81,8 +84,10 @@ namespace Google.ProtocolBuffers.ProtoBench } if (temp.Remove("/all") || temp.Remove("-all")) { - if(FastTest) + if (FastTest) + { TargetTime = TimeSpan.FromSeconds(5); + } foreach (KeyValuePair item in MakeTests()) { temp.Add(item.Key); @@ -95,7 +100,8 @@ namespace Google.ProtocolBuffers.ProtoBench { Console.Error.WriteLine("Usage: ProtoBench [/fast] "); Console.Error.WriteLine("The descriptor type name is the fully-qualified message name,"); - Console.Error.WriteLine("including assembly - e.g. Google.ProtocolBuffers.BenchmarkProtos.Message1,ProtoBench"); + Console.Error.WriteLine( + "including assembly - e.g. Google.ProtocolBuffers.BenchmarkProtos.Message1,ProtoBench"); Console.Error.WriteLine("(You can specify multiple pairs of descriptor type name and input data.)"); return 1; } @@ -131,10 +137,11 @@ namespace Google.ProtocolBuffers.ProtoBench inputData = inputData ?? File.ReadAllBytes(file); MemoryStream inputStream = new MemoryStream(inputData); ByteString inputString = ByteString.CopyFrom(inputData); - IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild(); + IMessage sampleMessage = + defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild(); IDictionary dictionary = null; - byte[] jsonBytes = null, xmlBytes = null;/*no pun intended, well... maybe for xml*/ + byte[] jsonBytes = null, xmlBytes = null; /*no pun intended, well... maybe for xml*/ if (OtherFormats) { using (MemoryStream temp = new MemoryStream()) @@ -152,59 +159,84 @@ namespace Google.ProtocolBuffers.ProtoBench } //Serializers - if(!FastTest) RunBenchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString()); + if (!FastTest) + { + RunBenchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString()); + } RunBenchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray()); - if (!FastTest) RunBenchmark("Serialize to memory stream", inputData.Length, - () => sampleMessage.WriteTo(new MemoryStream())); + if (!FastTest) + { + RunBenchmark("Serialize to memory stream", inputData.Length, + () => sampleMessage.WriteTo(new MemoryStream())); + } if (OtherFormats) { - RunBenchmark("Serialize to xml", xmlBytes.Length, () => - { - XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage); - }); - RunBenchmark("Serialize to json", jsonBytes.Length, () => - { - JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage); - }); + RunBenchmark("Serialize to xml", xmlBytes.Length, + () => + { + XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage); + }); + RunBenchmark("Serialize to json", jsonBytes.Length, + () => { JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage); }); RunBenchmark("Serialize to json via xml", jsonBytes.Length, - () => - XmlFormatWriter.CreateInstance(JsonReaderWriterFactory.CreateJsonWriter(new MemoryStream(), Encoding.UTF8)) - .SetOptions(XmlWriterOptions.OutputJsonTypes) - .WriteMessage(sampleMessage) - ); - - RunBenchmark("Serialize to dictionary", sampleMessage.SerializedSize, () => new DictionaryWriter().WriteMessage(sampleMessage)); + () => + XmlFormatWriter.CreateInstance( + JsonReaderWriterFactory.CreateJsonWriter(new MemoryStream(), Encoding.UTF8)) + .SetOptions(XmlWriterOptions.OutputJsonTypes) + .WriteMessage(sampleMessage) + ); + + RunBenchmark("Serialize to dictionary", sampleMessage.SerializedSize, + () => new DictionaryWriter().WriteMessage(sampleMessage)); } //Deserializers - if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length, - () => defaultMessage.WeakCreateBuilderForType() - .WeakMergeFrom(inputString, registry) - .WeakBuild() - ); + if (!FastTest) + { + RunBenchmark("Deserialize from byte string", inputData.Length, + () => defaultMessage.WeakCreateBuilderForType() + .WeakMergeFrom(inputString, registry) + .WeakBuild() + ); + } RunBenchmark("Deserialize from byte array", inputData.Length, - () => defaultMessage.WeakCreateBuilderForType() - .WeakMergeFrom(CodedInputStream.CreateInstance(inputData), registry) - .WeakBuild() + () => defaultMessage.WeakCreateBuilderForType() + .WeakMergeFrom(CodedInputStream.CreateInstance(inputData), registry) + .WeakBuild() ); - if (!FastTest) RunBenchmark("Deserialize from memory stream", inputData.Length, - () => { - inputStream.Position = 0; - defaultMessage.WeakCreateBuilderForType().WeakMergeFrom( - CodedInputStream.CreateInstance(inputStream), registry) - .WeakBuild(); - }); + if (!FastTest) + { + RunBenchmark("Deserialize from memory stream", inputData.Length, + () => + { + inputStream.Position = 0; + defaultMessage.WeakCreateBuilderForType().WeakMergeFrom( + CodedInputStream.CreateInstance(inputStream), registry) + .WeakBuild(); + }); + } if (OtherFormats) { - RunBenchmark("Deserialize from xml", xmlBytes.Length, () => XmlFormatReader.CreateInstance(xmlBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); - RunBenchmark("Deserialize from json", jsonBytes.Length, () => JsonFormatReader.CreateInstance(jsonBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); + RunBenchmark("Deserialize from xml", xmlBytes.Length, + () => + XmlFormatReader.CreateInstance(xmlBytes).Merge( + defaultMessage.WeakCreateBuilderForType()).WeakBuild()); + RunBenchmark("Deserialize from json", jsonBytes.Length, + () => + JsonFormatReader.CreateInstance(jsonBytes).Merge( + defaultMessage.WeakCreateBuilderForType()).WeakBuild()); RunBenchmark("Deserialize from json via xml", jsonBytes.Length, - () => XmlFormatReader.CreateInstance(JsonReaderWriterFactory.CreateJsonReader(jsonBytes, System.Xml.XmlDictionaryReaderQuotas.Max)) - .SetOptions(XmlReaderOptions.ReadNestedArrays).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); - - RunBenchmark("Deserialize from dictionary", sampleMessage.SerializedSize, () => new DictionaryReader(dictionary).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); + () => + XmlFormatReader.CreateInstance(JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max)) + .SetOptions(XmlReaderOptions.ReadNestedArrays).Merge( + defaultMessage.WeakCreateBuilderForType()).WeakBuild()); + + RunBenchmark("Deserialize from dictionary", sampleMessage.SerializedSize, + () => + new DictionaryReader(dictionary).Merge(defaultMessage.WeakCreateBuilderForType()). + WeakBuild()); } Console.WriteLine(); return true; @@ -240,25 +272,31 @@ namespace Google.ProtocolBuffers.ProtoBench TimeSpan target = TimeSpan.FromSeconds(1); elapsed = TimeAction(action, iterations); - iterations = (int)((target.Ticks * iterations) / (double)elapsed.Ticks); + iterations = (int) ((target.Ticks*iterations)/(double) elapsed.Ticks); elapsed = TimeAction(action, iterations); - iterations = (int)((target.Ticks * iterations) / (double)elapsed.Ticks); + iterations = (int) ((target.Ticks*iterations)/(double) elapsed.Ticks); elapsed = TimeAction(action, iterations); - iterations = (int)((target.Ticks * iterations) / (double)elapsed.Ticks); + iterations = (int) ((target.Ticks*iterations)/(double) elapsed.Ticks); - double first = (iterations * dataSize) / (elapsed.TotalSeconds * 1024 * 1024); - if (Verbose) Console.WriteLine("Round ---: Count = {1,6}, Bps = {2,8:f3}", 0, iterations, first); + double first = (iterations*dataSize)/(elapsed.TotalSeconds*1024*1024); + if (Verbose) + { + Console.WriteLine("Round ---: Count = {1,6}, Bps = {2,8:f3}", 0, iterations, first); + } elapsed = TimeSpan.Zero; - int max = (int)TargetTime.TotalSeconds; + int max = (int) TargetTime.TotalSeconds; while (runs < max) { TimeSpan cycle = TimeAction(action, iterations); // Accumulate and scale for next cycle. - - double bps = (iterations * dataSize) / (cycle.TotalSeconds * 1024 * 1024); - if (Verbose) Console.WriteLine("Round {1,3}: Count = {2,6}, Bps = {3,8:f3}", - 0, runs, iterations, bps); + + double bps = (iterations*dataSize)/(cycle.TotalSeconds*1024*1024); + if (Verbose) + { + Console.WriteLine("Round {1,3}: Count = {2,6}, Bps = {3,8:f3}", + 0, runs, iterations, bps); + } best = Math.Max(best, bps); worst = Math.Min(worst, bps); @@ -270,9 +308,10 @@ namespace Google.ProtocolBuffers.ProtoBench } Thread.EndThreadAffinity(); - Console.WriteLine("{1}: averages {2} per {3:f3}s for {4} runs; avg: {5:f3}mbps; best: {6:f3}mbps; worst: {7:f3}mbps", - 0, name, totalCount / runs, elapsed.TotalSeconds / runs, runs, - (totalCount * dataSize) / (elapsed.TotalSeconds * 1024 * 1024), best, worst); + Console.WriteLine( + "{1}: averages {2} per {3:f3}s for {4} runs; avg: {5:f3}mbps; best: {6:f3}mbps; worst: {7:f3}mbps", + 0, name, totalCount/runs, elapsed.TotalSeconds/runs, runs, + (totalCount*dataSize)/(elapsed.TotalSeconds*1024*1024), best, worst); } private static void BenchmarkV1(string name, long dataSize, Action action) @@ -321,13 +360,19 @@ namespace Google.ProtocolBuffers.ProtoBench //Discrete Tests foreach (KeyValuePair> item in MakeTestAllTypes()) - yield return MakeWorkItem(item.Key, new[] { item }); + { + yield return MakeWorkItem(item.Key, new[] {item}); + } foreach (KeyValuePair> item in MakeRepeatedTestAllTypes(100)) - yield return MakeWorkItem(item.Key, new[] { item }); + { + yield return MakeWorkItem(item.Key, new[] {item}); + } foreach (KeyValuePair> item in MakeTestPackedTypes(100)) - yield return MakeWorkItem(item.Key, new[] { item }); + { + yield return MakeWorkItem(item.Key, new[] {item}); + } } private static IEnumerable>> MakeTestAllTypes() @@ -349,11 +394,26 @@ namespace Google.ProtocolBuffers.ProtoBench yield return MakeItem("float", 1, x => x.SetOptionalFloat(1001.1001f)); yield return MakeItem("double", 1, x => x.SetOptionalDouble(1001.1001)); yield return MakeItem("bool", 1, x => x.SetOptionalBool(true)); - yield return MakeItem("string", 1, x => x.SetOptionalString("this is a string value")); - yield return MakeItem("bytes", 1, x => x.SetOptionalBytes(ByteString.CopyFromUtf8("this is an array of bytes"))); - yield return MakeItem("group", 1, x => x.SetOptionalGroup(new TestAllTypes.Types.OptionalGroup.Builder().SetA(1001))); - yield return MakeItem("message", 1, x => x.SetOptionalNestedMessage(new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))); - yield return MakeItem("enum", 1, x => x.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO)); + yield return MakeItem("string", 1, x => x.SetOptionalString("this is a string value")) + ; + yield return + MakeItem("bytes", 1, + x => + x.SetOptionalBytes(ByteString.CopyFromUtf8("this is an array of bytes"))) + ; + yield return + MakeItem("group", 1, + x => + x.SetOptionalGroup( + new TestAllTypes.Types.OptionalGroup.Builder().SetA(1001))); + yield return + MakeItem("message", 1, + x => + x.SetOptionalNestedMessage( + new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))); + yield return + MakeItem("enum", 1, + x => x.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO)); } private static IEnumerable>> MakeRepeatedTestAllTypes(int size) @@ -372,11 +432,27 @@ namespace Google.ProtocolBuffers.ProtoBench yield return MakeItem("repeated-float", size, x => x.AddRepeatedFloat(1001.1001f)); yield return MakeItem("repeated-double", size, x => x.AddRepeatedDouble(1001.1001)); yield return MakeItem("repeated-bool", size, x => x.AddRepeatedBool(true)); - yield return MakeItem("repeated-string", size, x => x.AddRepeatedString("this is a string value")); - yield return MakeItem("repeated-bytes", size, x => x.AddRepeatedBytes(ByteString.CopyFromUtf8("this is an array of bytes"))); - yield return MakeItem("repeated-group", size, x => x.AddRepeatedGroup(new TestAllTypes.Types.RepeatedGroup.Builder().SetA(1001))); - yield return MakeItem("repeated-message", size, x => x.AddRepeatedNestedMessage(new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))); - yield return MakeItem("repeated-enum", size, x => x.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.FOO)); + yield return + MakeItem("repeated-string", size, + x => x.AddRepeatedString("this is a string value")); + yield return + MakeItem("repeated-bytes", size, + x => + x.AddRepeatedBytes(ByteString.CopyFromUtf8("this is an array of bytes"))) + ; + yield return + MakeItem("repeated-group", size, + x => + x.AddRepeatedGroup( + new TestAllTypes.Types.RepeatedGroup.Builder().SetA(1001))); + yield return + MakeItem("repeated-message", size, + x => + x.AddRepeatedNestedMessage( + new TestAllTypes.Types.NestedMessage.Builder().SetBb(1001))); + yield return + MakeItem("repeated-enum", size, + x => x.AddRepeatedNestedEnum(TestAllTypes.Types.NestedEnum.FOO)); } private static IEnumerable>> MakeTestPackedTypes(int size) @@ -395,35 +471,47 @@ namespace Google.ProtocolBuffers.ProtoBench yield return MakeItem("packed-float", size, x => x.AddPackedFloat(1001.1001f)); yield return MakeItem("packed-double", size, x => x.AddPackedDouble(1001.1001)); yield return MakeItem("packed-bool", size, x => x.AddPackedBool(true)); - yield return MakeItem("packed-enum", size, x => x.AddPackedEnum(ForeignEnum.FOREIGN_FOO)); + yield return + MakeItem("packed-enum", size, x => x.AddPackedEnum(ForeignEnum.FOREIGN_FOO)); } - private static KeyValuePair> MakeItem(string name, int repeated, Action build) where T : IBuilderLite, new() + private static KeyValuePair> MakeItem(string name, int repeated, Action build) + where T : IBuilderLite, new() { if (repeated == 1) + { return new KeyValuePair>(name, build); + } return new KeyValuePair>( String.Format("{0}[{1}]", name, repeated), x => - { - for (int i = 0; i < repeated; i++) - build(x); - } - ); + { + for (int i = 0; i < repeated; i++) + { + build(x); + } + } + ); } - private static KeyValuePair MakeWorkItem(string name, IEnumerable>> builders) where T : IBuilderLite, new() + private static KeyValuePair MakeWorkItem(string name, + IEnumerable>> + builders) where T : IBuilderLite, new() { T builder = new T(); foreach (KeyValuePair> item in builders) + { item.Value(builder); + } IMessageLite msg = builder.WeakBuild(); string fname = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "unittest_" + name + ".dat"); File.WriteAllBytes(fname, msg.ToByteArray()); - return new KeyValuePair(String.Format("{0},{1}", msg.GetType().FullName, msg.GetType().Assembly.GetName().Name), fname); + return + new KeyValuePair( + String.Format("{0},{1}", msg.GetType().FullName, msg.GetType().Assembly.GetName().Name), fname); } } } \ No newline at end of file