aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers.Test/CompatTests
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-02 12:04:06 -0500
committerrogerk <devnull@localhost>2011-06-02 12:04:06 -0500
commit27bfcc5e1a7a3aacd828475c1996e114e34055d2 (patch)
tree8a4d0adf6cab44a934c21a7656fc8226b81556fc /src/ProtocolBuffers.Test/CompatTests
parent45a93fad4d7123887d14135ee15ee3e9b0d4ca58 (diff)
downloadprotobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.tar.gz
protobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.tar.bz2
protobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.zip
Slight refactoring of Extensions to support lookup by name, added compatibility tests for text and binary formats.
Diffstat (limited to 'src/ProtocolBuffers.Test/CompatTests')
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/BinaryCompatibilityTests.cs22
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs172
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/TestResources.Designer.cs77
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/TestResources.resx127
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs60
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/google_message1.datbin0 -> 228 bytes
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/google_message2.datbin0 -> 84570 bytes
7 files changed, 458 insertions, 0 deletions
diff --git a/src/ProtocolBuffers.Test/CompatTests/BinaryCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/BinaryCompatibilityTests.cs
new file mode 100644
index 00000000..fe8a1e37
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/BinaryCompatibilityTests.cs
@@ -0,0 +1,22 @@
+using System;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers.CompatTests
+{
+ [TestFixture]
+ public class BinaryCompatibilityTests : CompatibilityTests
+ {
+ protected override string TestName { get { return "binary"; } }
+
+ protected override object SerializeMessage<TMessage, TBuilder>(TMessage message)
+ {
+ byte[] bresult = message.ToByteArray();
+ return bresult;
+ }
+
+ protected override TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
+ {
+ return builder.MergeFrom((byte[])message, registry);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
new file mode 100644
index 00000000..e74f2b08
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
@@ -0,0 +1,172 @@
+using System;
+using Google.ProtocolBuffers.TestProtos;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers.CompatTests
+{
+ public abstract class CompatibilityTests
+ {
+ protected abstract string TestName { get; }
+ protected abstract object SerializeMessage<TMessage, TBuilder>(TMessage message)
+ where TMessage : IMessageLite<TMessage, TBuilder>
+ where TBuilder : IBuilderLite<TMessage, TBuilder>;
+
+ protected abstract TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
+ where TMessage : IMessageLite<TMessage, TBuilder>
+ where TBuilder : IBuilderLite<TMessage, TBuilder>;
+
+ #region RunBenchmark
+
+ protected void RunBenchmark<TMessage, TBuilder>(byte[] buffer, bool write)
+ where TMessage : IMessageLite<TMessage, TBuilder>
+ where TBuilder : IBuilderLite<TMessage, TBuilder>, new()
+ {
+ TBuilder builder = new TBuilder();
+ TMessage message = new TBuilder().MergeFrom(buffer).Build();
+ System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
+ //simple warm-up
+ object content = SerializeMessage<TMessage, TBuilder>(message);
+ Assert.AreEqual(message, DeerializeMessage<TMessage, TBuilder>(content, new TBuilder(), ExtensionRegistry.Empty).Build());
+ //timming
+ long time = 0, sample = 1;
+ while (time < 100)
+ {
+ sample *= 10;
+ watch.Reset();
+ watch.Start();
+ if (write)
+ {
+ for (int i = 0; i < sample; i++)
+ SerializeMessage<TMessage, TBuilder>(message);
+ }
+ else
+ {
+ for (int i = 0; i < sample; i++)
+ DeerializeMessage<TMessage, TBuilder>(content, builder, ExtensionRegistry.Empty);
+ }
+ watch.Stop();
+ time = watch.ElapsedMilliseconds;
+ }
+
+ ulong rounds = (ulong)((100.0 / watch.ElapsedMilliseconds) * sample);
+ //test
+ watch.Reset();
+ watch.Start();
+
+ if (write)
+ {
+ for (ulong i = 0; i < rounds; i++)
+ SerializeMessage<TMessage, TBuilder>(message);
+ }
+ else
+ {
+ for (ulong i = 0; i < rounds; i++)
+ DeerializeMessage<TMessage, TBuilder>(content, builder, ExtensionRegistry.Empty);
+ }
+
+ watch.Stop();
+ System.Diagnostics.Trace.TraceInformation(
+ "\r\n{0} {4} {5} {3:n0} rps ({1:n0} rounds in {2:n0} ms)", typeof(TMessage).Name, rounds,
+ watch.ElapsedMilliseconds, (1000.0 / watch.ElapsedMilliseconds) * (double)rounds, TestName, write ? " write" : " read");
+ GC.GetTotalMemory(true);
+ GC.WaitForPendingFinalizers();
+ }
+
+ [Test]
+ public virtual void Message1OptimizeSizeWriterPerf()
+ {
+ RunBenchmark<SizeMessage1, SizeMessage1.Builder>(TestResources.google_message1, true);
+ }
+ [Test]
+ public virtual void Message1OptimizeSpeedWriterPerf()
+ {
+ RunBenchmark<SpeedMessage1, SpeedMessage1.Builder>(TestResources.google_message1, true);
+ }
+ [Test]
+ public virtual void Message2OptimizeSizeWriterPerf()
+ {
+ RunBenchmark<SizeMessage2, SizeMessage2.Builder>(TestResources.google_message2, true);
+ }
+ [Test]
+ public virtual void Message2OptimizeSpeedWriterPerf()
+ {
+ RunBenchmark<SpeedMessage2, SpeedMessage2.Builder>(TestResources.google_message2, true);
+ }
+
+ [Test]
+ public virtual void Message1OptimizeSizeReadPerf()
+ {
+ RunBenchmark<SizeMessage1, SizeMessage1.Builder>(TestResources.google_message1, false);
+ }
+ [Test]
+ public virtual void Message1OptimizeSpeedReadPerf()
+ {
+ RunBenchmark<SpeedMessage1, SpeedMessage1.Builder>(TestResources.google_message1, false);
+ }
+ [Test]
+ public virtual void Message2OptimizeSizeReadPerf()
+ {
+ RunBenchmark<SizeMessage2, SizeMessage2.Builder>(TestResources.google_message2, false);
+ }
+ [Test]
+ public virtual void Message2OptimizeSpeedReadPerf()
+ {
+ RunBenchmark<SpeedMessage2, SpeedMessage2.Builder>(TestResources.google_message2, false);
+ }
+
+ #endregion
+
+ [Test]
+ public virtual void RoundTripMessage1OptimizeSize()
+ {
+ SizeMessage1 msg = SizeMessage1.CreateBuilder().MergeFrom(TestResources.google_message1).Build();
+ object content = SerializeMessage<SizeMessage1, SizeMessage1.Builder>(msg);
+
+ SizeMessage1 copy = DeerializeMessage<SizeMessage1, SizeMessage1.Builder>(content, SizeMessage1.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ Assert.AreEqual(content, SerializeMessage<SizeMessage1,SizeMessage1.Builder>(copy));
+ Assert.AreEqual(TestResources.google_message1, copy.ToByteArray());
+ }
+
+ [Test]
+ public virtual void RoundTripMessage2OptimizeSize()
+ {
+ SizeMessage2 msg = SizeMessage2.CreateBuilder().MergeFrom(TestResources.google_message2).Build();
+ object content = SerializeMessage<SizeMessage2, SizeMessage2.Builder>(msg);
+
+ SizeMessage2 copy = DeerializeMessage<SizeMessage2, SizeMessage2.Builder>(content, SizeMessage2.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ Assert.AreEqual(content, SerializeMessage<SizeMessage2, SizeMessage2.Builder>(copy));
+ Assert.AreEqual(TestResources.google_message2, copy.ToByteArray());
+ }
+
+ [Test]
+ public virtual void RoundTripMessage1OptimizeSpeed()
+ {
+ SpeedMessage1 msg = SpeedMessage1.CreateBuilder().MergeFrom(TestResources.google_message1).Build();
+ object content = SerializeMessage<SpeedMessage1, SpeedMessage1.Builder>(msg);
+
+ SpeedMessage1 copy = DeerializeMessage<SpeedMessage1, SpeedMessage1.Builder>(content, SpeedMessage1.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ Assert.AreEqual(content, SerializeMessage<SpeedMessage1, SpeedMessage1.Builder>(copy));
+ Assert.AreEqual(TestResources.google_message1, copy.ToByteArray());
+ }
+
+ [Test]
+ public virtual void RoundTripMessage2OptimizeSpeed()
+ {
+ SpeedMessage2 msg = SpeedMessage2.CreateBuilder().MergeFrom(TestResources.google_message2).Build();
+ object content = SerializeMessage<SpeedMessage2, SpeedMessage2.Builder>(msg);
+
+ SpeedMessage2 copy = DeerializeMessage<SpeedMessage2, SpeedMessage2.Builder>(content, SpeedMessage2.CreateBuilder(), ExtensionRegistry.Empty).Build();
+
+ Assert.AreEqual(msg, copy);
+ Assert.AreEqual(content, SerializeMessage<SpeedMessage2, SpeedMessage2.Builder>(copy));
+ Assert.AreEqual(TestResources.google_message2, copy.ToByteArray());
+ }
+
+ }
+}
diff --git a/src/ProtocolBuffers.Test/CompatTests/TestResources.Designer.cs b/src/ProtocolBuffers.Test/CompatTests/TestResources.Designer.cs
new file mode 100644
index 00000000..8565ee80
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/TestResources.Designer.cs
@@ -0,0 +1,77 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+// Runtime Version:2.0.50727.5444
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace Google.ProtocolBuffers.CompatTests {
+ using System;
+
+
+ /// <summary>
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ /// </summary>
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class TestResources {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal TestResources() {
+ }
+
+ /// <summary>
+ /// Returns the cached ResourceManager instance used by this class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Google.ProtocolBuffers.CompatTests.TestResources", typeof(TestResources).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ /// <summary>
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+
+ internal static byte[] google_message1 {
+ get {
+ object obj = ResourceManager.GetObject("google_message1", resourceCulture);
+ return ((byte[])(obj));
+ }
+ }
+
+ internal static byte[] google_message2 {
+ get {
+ object obj = ResourceManager.GetObject("google_message2", resourceCulture);
+ return ((byte[])(obj));
+ }
+ }
+ }
+}
diff --git a/src/ProtocolBuffers.Test/CompatTests/TestResources.resx b/src/ProtocolBuffers.Test/CompatTests/TestResources.resx
new file mode 100644
index 00000000..0481d388
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/TestResources.resx
@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+ <!--
+ Microsoft ResX Schema
+
+ Version 2.0
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
+ associated with the data types.
+
+ Example:
+
+ ... ado.net/XML headers & schema ...
+ <resheader name="resmimetype">text/microsoft-resx</resheader>
+ <resheader name="version">2.0</resheader>
+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+ <value>[base64 mime encoded serialized .NET Framework object]</value>
+ </data>
+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+ <comment>This is a comment</comment>
+ </data>
+
+ There are any number of "resheader" rows that contain simple
+ name/value pairs.
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
+ mimetype set.
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
+ extensible. For a given mimetype the value must be set accordingly:
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
+ read any of the formats listed below.
+
+ mimetype: application/x-microsoft.net.object.binary.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.soap.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.bytearray.base64
+ value : The object must be serialized into a byte array
+ : using a System.ComponentModel.TypeConverter
+ : and then encoded with base64 encoding.
+ -->
+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+ <xsd:element name="root" msdata:IsDataSet="true">
+ <xsd:complexType>
+ <xsd:choice maxOccurs="unbounded">
+ <xsd:element name="metadata">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" />
+ </xsd:sequence>
+ <xsd:attribute name="name" use="required" type="xsd:string" />
+ <xsd:attribute name="type" type="xsd:string" />
+ <xsd:attribute name="mimetype" type="xsd:string" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="assembly">
+ <xsd:complexType>
+ <xsd:attribute name="alias" type="xsd:string" />
+ <xsd:attribute name="name" type="xsd:string" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="data">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="resheader">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" />
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:schema>
+ <resheader name="resmimetype">
+ <value>text/microsoft-resx</value>
+ </resheader>
+ <resheader name="version">
+ <value>2.0</value>
+ </resheader>
+ <resheader name="reader">
+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <resheader name="writer">
+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+ <data name="google_message1" type="System.Resources.ResXFileRef, System.Windows.Forms">
+ <value>google_message1.dat;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </data>
+ <data name="google_message2" type="System.Resources.ResXFileRef, System.Windows.Forms">
+ <value>google_message2.dat;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </data>
+</root> \ No newline at end of file
diff --git a/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs
new file mode 100644
index 00000000..5eba0d33
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs
@@ -0,0 +1,60 @@
+using System.ComponentModel;
+using System.IO;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers.CompatTests
+{
+ [TestFixture]
+ public class TextCompatibilityTests : CompatibilityTests
+ {
+ protected override string TestName { get { return "text"; } }
+
+ protected override object SerializeMessage<TMessage, TBuilder>(TMessage message)
+ {
+ StringWriter text = new StringWriter();
+ message.PrintTo(text);
+ return text.ToString();
+ }
+
+ protected override TBuilder DeerializeMessage<TMessage, TBuilder>(object message, TBuilder builder, ExtensionRegistry registry)
+ {
+ TextFormat.Merge(new StringReader((string)message), registry, (IBuilder)builder);
+ return builder;
+ }
+
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void Message2OptimizeSizeReadPerf()
+ {
+ base.Message2OptimizeSizeReadPerf();
+ }
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void Message2OptimizeSpeedReadPerf()
+ {
+ base.Message2OptimizeSpeedReadPerf();
+ }
+
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void RoundTripMessage2OptimizeSize()
+ {
+ base.RoundTripMessage2OptimizeSize();
+ }
+
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void RoundTripMessage2OptimizeSpeed()
+ {
+ base.RoundTripMessage2OptimizeSpeed();
+ }
+
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void Message2OptimizeSizeWriterPerf()
+ {
+ base.Message2OptimizeSizeWriterPerf();
+ }
+ [Test, Explicit, Description("This test can take a very long time to run.")]
+ public override void Message2OptimizeSpeedWriterPerf()
+ {
+ base.Message2OptimizeSpeedWriterPerf();
+ }
+
+ }
+} \ No newline at end of file
diff --git a/src/ProtocolBuffers.Test/CompatTests/google_message1.dat b/src/ProtocolBuffers.Test/CompatTests/google_message1.dat
new file mode 100644
index 00000000..bc0f064c
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/google_message1.dat
Binary files differ
diff --git a/src/ProtocolBuffers.Test/CompatTests/google_message2.dat b/src/ProtocolBuffers.Test/CompatTests/google_message2.dat
new file mode 100644
index 00000000..06c09441
--- /dev/null
+++ b/src/ProtocolBuffers.Test/CompatTests/google_message2.dat
Binary files differ