public void Run() { Console.WriteLine("=== Serializer benchmark ==="); //Test serializer performance Stopwatch stopwatch = new Stopwatch(); BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); NetDataWriter netDataWriter = new NetDataWriter(); SamplePacket samplePacket = new SamplePacket { SomeFloat = 0.3f, SomeString = "TEST", SomeIntArray = new [] { 1, 2, 3 }, SomeVector2 = new SomeVector2(1, 2), SomeVectors = new SomeVector2[20] }; for (int i = 0; i < samplePacket.SomeVectors.Length; i++) { samplePacket.SomeVectors[i] = new SomeVector2(i, i); } var netSerializer = new NetSerializer(); netSerializer.RegisterNestedType <SampleNetSerializable>(); netSerializer.RegisterNestedType(SomeVector2.Serialize, SomeVector2.Deserialize); //Prewarm cpu for (int i = 0; i < 10000000; i++) { double c = Math.Sin(i); } //Test binary formatter stopwatch.Start(); for (int i = 0; i < LoopLength; i++) { binaryFormatter.Serialize(memoryStream, samplePacket); } stopwatch.Stop(); Console.WriteLine("BinaryFormatter time: " + stopwatch.ElapsedMilliseconds + " ms"); Console.WriteLine("BinaryFormatter size: " + memoryStream.Position / LoopLength); DataWriterTest(netDataWriter, stopwatch, samplePacket); NetSerializerTest(netSerializer, netDataWriter, stopwatch, samplePacket); DataWriterTest(netDataWriter, stopwatch, samplePacket); NetSerializerTest(netSerializer, netDataWriter, stopwatch, samplePacket); DataWriterTest(netDataWriter, stopwatch, samplePacket); NetSerializerTest(netSerializer, netDataWriter, stopwatch, samplePacket); Console.ReadKey(); }
/// <summary> /// Register a type of message into the messaging system. /// </summary> /// <typeparam name="T">INetSerializable structure</typeparam> /// <returns>True - if register successful, false - if type already registered</returns> public bool RegisterNestedType <T>() where T : class, INetSerializable, new() { bool ok = _netSerializer.RegisterNestedType <T>(() => new T()); Events <T>(); //< Force creation of the event handlers return(ok); }
public void Run() { Console.WriteLine("=== Serializer benchmark ==="); const int LoopLength = 100000; //Test serializer performance Stopwatch stopwatch = new Stopwatch(); BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); NetDataWriter netDataWriter = new NetDataWriter(); SamplePacket samplePacket = new SamplePacket { SomeFloat = 0.3f, SomeString = "TEST", SomeIntArray = new [] { 1, 2, 3 }, SomeVector2 = new SomeVector2(1, 2), SomeVectors = new SomeVector2[20] }; for (int i = 0; i < samplePacket.SomeVectors.Length; i++) { samplePacket.SomeVectors[i] = new SomeVector2(i, i); } NetSerializer netSerializer = new NetSerializer(); netSerializer.RegisterNestedType <SampleNetSerializable>(); netSerializer.RegisterNestedType(SomeVector2.Serialize, SomeVector2.Deserialize); //Prewarm cpu for (int i = 0; i < 10000000; i++) { double c = Math.Sin(i); } //Test binary formatter stopwatch.Start(); for (int i = 0; i < LoopLength; i++) { binaryFormatter.Serialize(memoryStream, samplePacket); } stopwatch.Stop(); Console.WriteLine("BinaryFormatter time: " + stopwatch.ElapsedMilliseconds + " ms"); //Test NetSerializer stopwatch.Restart(); for (int i = 0; i < LoopLength; i++) { netSerializer.Serialize(netDataWriter, samplePacket); } stopwatch.Stop(); Console.WriteLine("NetSerializer first run time: " + stopwatch.ElapsedMilliseconds + " ms"); //Test NetSerializer netDataWriter.Reset(); stopwatch.Restart(); for (int i = 0; i < LoopLength; i++) { netSerializer.Serialize(netDataWriter, samplePacket); } stopwatch.Stop(); Console.WriteLine("NetSerializer second run time: " + stopwatch.ElapsedMilliseconds + " ms"); //Test RAW netDataWriter.Reset(); stopwatch.Restart(); for (int i = 0; i < LoopLength; i++) { netDataWriter.Put(samplePacket.SomeFloat); netDataWriter.Put(samplePacket.SomeString); netDataWriter.PutArray(samplePacket.SomeIntArray); netDataWriter.Put(samplePacket.SomeVector2.X); netDataWriter.Put(samplePacket.SomeVector2.Y); netDataWriter.Put(samplePacket.SomeVectors.Length); for (int j = 0; j < samplePacket.SomeVectors.Length; j++) { netDataWriter.Put(samplePacket.SomeVectors[j].X); netDataWriter.Put(samplePacket.SomeVectors[j].Y); } netDataWriter.Put(samplePacket.EmptyString); netDataWriter.Put(samplePacket.TestObj.Value); } stopwatch.Stop(); Console.WriteLine("DataWriter (raw put method calls) time: " + stopwatch.ElapsedMilliseconds + " ms"); }