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,19 +133,23 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -132,19 +133,23 @@ 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;
using (MemoryStream temp = new MemoryStream()) byte[] jsonBytes = null, xmlBytes = null;/*no pun intended, well... maybe for xml*/
if (OtherFormats)
{ {
XmlFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage); using (MemoryStream temp = new MemoryStream())
xmlBytes = temp.ToArray(); {
XmlFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage);
xmlBytes = temp.ToArray();
}
using (MemoryStream temp = new MemoryStream())
{
JsonFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage);
jsonBytes = temp.ToArray();
}
dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
new DictionaryWriter(dictionary).WriteMessage(sampleMessage);
} }
using (MemoryStream temp = new MemoryStream())
{
JsonFormatWriter.CreateInstance(temp).WriteMessage(sampleMessage);
jsonBytes = temp.ToArray();
}
IDictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
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,23 +157,25 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -152,23 +157,25 @@ 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()));
RunBenchmark("Serialize to xml", xmlBytes.Length, () => if (OtherFormats)
{
XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage);
} );
RunBenchmark("Serialize to json", jsonBytes.Length, () =>
{ {
JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage); RunBenchmark("Serialize to xml", xmlBytes.Length, () =>
}); {
RunBenchmark("Serialize to json via xml", jsonBytes.Length, XmlFormatWriter.CreateInstance(new MemoryStream(), Encoding.UTF8).WriteMessage(sampleMessage);
() => });
XmlFormatWriter.CreateInstance(JsonReaderWriterFactory.CreateJsonWriter(new MemoryStream(), Encoding.UTF8)) RunBenchmark("Serialize to json", jsonBytes.Length, () =>
.SetOptions(XmlWriterOptions.OutputJsonTypes) {
.WriteMessage(sampleMessage) JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage);
); });
RunBenchmark("Serialize to json via xml", jsonBytes.Length,
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 //Deserializers
if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length, if (!FastTest) RunBenchmark("Deserialize from byte string", inputData.Length,
() => defaultMessage.WeakCreateBuilderForType() () => defaultMessage.WeakCreateBuilderForType()
...@@ -189,14 +196,16 @@ namespace Google.ProtocolBuffers.ProtoBench ...@@ -189,14 +196,16 @@ namespace Google.ProtocolBuffers.ProtoBench
.WeakBuild(); .WeakBuild();
}); });
RunBenchmark("Deserialize from xml", xmlBytes.Length, () => XmlFormatReader.CreateInstance(xmlBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); if (OtherFormats)
RunBenchmark("Deserialize from json", jsonBytes.Length, () => JsonFormatReader.CreateInstance(jsonBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); {
RunBenchmark("Deserialize from json via xml", jsonBytes.Length, RunBenchmark("Deserialize from xml", xmlBytes.Length, () => XmlFormatReader.CreateInstance(xmlBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
() => XmlFormatReader.CreateInstance(JsonReaderWriterFactory.CreateJsonReader(jsonBytes, System.Xml.XmlDictionaryReaderQuotas.Max)) RunBenchmark("Deserialize from json", jsonBytes.Length, () => JsonFormatReader.CreateInstance(jsonBytes).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild());
.SetOptions(XmlReaderOptions.ReadNestedArrays).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); RunBenchmark("Deserialize from json via xml", jsonBytes.Length,
() => XmlFormatReader.CreateInstance(JsonReaderWriterFactory.CreateJsonReader(jsonBytes, System.Xml.XmlDictionaryReaderQuotas.Max))
RunBenchmark("Deserialize from dictionary", sampleMessage.SerializedSize, () => new DictionaryReader(dictionary).Merge(defaultMessage.WeakCreateBuilderForType()).WeakBuild()); .SetOptions(XmlReaderOptions.ReadNestedArrays).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