Commit 57fa7fdd authored by csharptest's avatar csharptest Committed by rogerk

Added option to enable format benchmarks

parent f292523d
...@@ -53,7 +53,7 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -53,7 +53,7 @@ namespace Google.ProtocolBuffers.ProtoBench
{ {
private static TimeSpan MinSampleTime = TimeSpan.FromSeconds(2); private static TimeSpan MinSampleTime = TimeSpan.FromSeconds(2);
private static TimeSpan TargetTime = TimeSpan.FromSeconds(30); private static TimeSpan TargetTime = TimeSpan.FromSeconds(30);
private static bool Verbose = false, FastTest = false; private static bool Verbose = false, FastTest = false, OtherFormats = false;
// Avoid a .NET 3.5 dependency // Avoid a .NET 3.5 dependency
private delegate void Action(); private delegate void Action();
...@@ -67,6 +67,7 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -67,6 +67,7 @@ namespace Google.ProtocolBuffers.ProtoBench
List<string> temp = new List<string>(args); List<string> temp = new List<string>(args);
Verbose = temp.Remove("/verbose") || temp.Remove("-verbose"); Verbose = temp.Remove("/verbose") || temp.Remove("-verbose");
OtherFormats = temp.Remove("/formats") || temp.Remove("-formats");
if (true == (FastTest = (temp.Remove("/fast") || temp.Remove("-fast")))) if (true == (FastTest = (temp.Remove("/fast") || temp.Remove("-fast"))))
TargetTime = TimeSpan.FromSeconds(10); TargetTime = TimeSpan.FromSeconds(10);
...@@ -132,7 +133,10 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -132,7 +133,10 @@ namespace Google.ProtocolBuffers.ProtoBench
ByteString inputString = ByteString.CopyFrom(inputData); ByteString inputString = ByteString.CopyFrom(inputData);
IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild(); IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString, registry).WeakBuild();
byte[] jsonBytes, xmlBytes;/*no pun intended, well... maybe for xml*/ IDictionary<string, object> dictionary = null;
byte[] jsonBytes = null, xmlBytes = null;/*no pun intended, well... maybe for xml*/
if (OtherFormats)
{
using (MemoryStream temp = new MemoryStream()) using (MemoryStream temp = new MemoryStream())
{ {
XmlFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage); XmlFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage);
...@@ -143,8 +147,9 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -143,8 +147,9 @@ namespace Google.ProtocolBuffers.ProtoBench
JsonFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage); JsonFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage);
jsonBytes = temp.ToArray(); jsonBytes = temp.ToArray();
} }
IDictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.Ordinal); dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
new DictionaryWriter(dictionary).WriteMessage(sampleMessage); new DictionaryWriter(dictionary).WriteMessage(sampleMessage);
}
//Serializers //Serializers
if(!FastTest) RunBenchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString()); if(!FastTest) RunBenchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString());
...@@ -152,10 +157,12 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -152,10 +157,12 @@ namespace Google.ProtocolBuffers.ProtoBench
if (!FastTest) RunBenchmark("Serialize to memory stream", inputData.Length, if (!FastTest) RunBenchmark("Serialize to memory stream", inputData.Length,
() => sampleMessage.WriteTo(new MemoryStream())); () => sampleMessage.WriteTo(new MemoryStream()));
if (OtherFormats)
{
RunBenchmark("Serialize to xml", xmlBytes.Length, () => RunBenchmark("Serialize to xml", xmlBytes.Length, () =>
{ {
XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage); XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage);
} ); });
RunBenchmark("Serialize to json", jsonBytes.Length, () => RunBenchmark("Serialize to json", jsonBytes.Length, () =>
{ {
JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage); JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage);
...@@ -168,7 +175,7 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -168,7 +175,7 @@ namespace Google.ProtocolBuffers.ProtoBench
); );
RunBenchmark("Serialize to dictionary", sampleMessage.SerializedSize, () => new DictionaryWriter().WriteMessage(sampleMessage)); RunBenchmark("Serialize to dictionary", sampleMessage.SerializedSize, () => new DictionaryWriter().WriteMessage(sampleMessage));
}
//Deserializers //Deserializers
if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length, if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length,
() => defaultMessage.WeakCreateBuilderForType() () => defaultMessage.WeakCreateBuilderForType()
...@@ -189,6 +196,8 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -189,6 +196,8 @@ namespace Google.ProtocolBuffers.ProtoBench
.WeakBuild(); .WeakBuild();
}); });
if (OtherFormats)
{
RunBenchmark("Deserialize from xml", xmlBytes.Length, () => XmlFormatReader.CreateInstance(xmlBytes).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", jsonBytes.Length, () => JsonFormatReader.CreateInstance(jsonBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
RunBenchmark("Deserialize from json via xml", jsonBytes.Length, RunBenchmark("Deserialize from json via xml", jsonBytes.Length,
...@@ -196,7 +205,7 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -196,7 +205,7 @@ namespace Google.ProtocolBuffers.ProtoBench
.SetOptions(XmlReaderOptions.ReadNestedArrays).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); .SetOptions(XmlReaderOptions.ReadNestedArrays).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
RunBenchmark("Deserialize from dictionary", sampleMessage.SerializedSize, () => new DictionaryReader(dictionary).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); RunBenchmark("Deserialize from dictionary", sampleMessage.SerializedSize, () => new DictionaryReader(dictionary).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
}
Console.WriteLine(); Console.WriteLine();
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment