private void Button_Click_1(object sender, RoutedEventArgs e) { var ser = new MySerializer(); var obj = new MetroDto.TestClass { EnumVal = MetroDto.MyEnum.Bar }; var clone2 = (MetroDto.TestClass)ser.DeepClone(obj); var clone3 = Serializer.DeepClone(obj); // runtime ReadProto(((Button)sender)); //((Button)sender).Content = clone.Foo + ", " + clone.Bar; }
private async void ReadProto(Button button) { var path = @"nwind.proto.bin"; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; var file = await folder.GetFileAsync(path); var readStream = await file.OpenReadAsync(); IInputStream inputSteam = readStream.GetInputStreamAt(0); DataReader dataReader = new DataReader(inputSteam); uint numBytesLoaded = await dataReader.LoadAsync((uint)readStream.Size); byte[] raw = new byte[readStream.Size]; dataReader.ReadBytes(raw); var ms = new MemoryStream(raw); var ser = new MySerializer(); var dal = (DAL.DatabaseCompat)ser.Deserialize(ms, null, typeof(DAL.DatabaseCompat)); StringBuilder perfStats = new StringBuilder(); perfStats.AppendLine(".NET for Metro style apps"); perfStats.AppendFormat("{0} orders from NWind", dal.Orders.Count).AppendLine(); var dcs = new DataContractSerializer(typeof(DAL.DatabaseCompat)); var xs = new XmlSerializer(typeof(DAL.DatabaseCompat)); using (var buffer = new MemoryStream()) { const int loop = 50; var watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); dcs.WriteObject(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("DataContractSerializer:").AppendFormat("WriteObject x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; dcs.ReadObject(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("ReadObject x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); xs.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("XmlSerializer:").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; xs.Deserialize(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); ser.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("protobuf-net").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; ser.Deserialize(buffer, null, typeof(DAL.DatabaseCompat)); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); Serializer.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("protobuf-net (runtime)").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; Serializer.Deserialize <DAL.DatabaseCompat>(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); } button.Content = perfStats.ToString(); // test SM2Stats bool isSer = ser.CanSerializeContractType(typeof(SM2Stats)); SM2Stats stats = new SM2Stats { acqreps = 1, difficultyhistory = { new DiffHistPair { date = 123, values ={ 3.4f } } } }; var clone = (SM2Stats)ser.DeepClone(stats); // checked by eye; is the same }
private async void ReadProto(Button button) { var path = @"nwind.proto.bin"; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; var file = await folder.GetFileAsync(path); var readStream = await file.OpenReadAsync(); IInputStream inputSteam = readStream.GetInputStreamAt(0); DataReader dataReader = new DataReader(inputSteam); uint numBytesLoaded = await dataReader.LoadAsync((uint)readStream.Size); byte[] raw = new byte[readStream.Size]; dataReader.ReadBytes(raw); var ms = new MemoryStream(raw); var ser = new MySerializer(); var dal = (DAL.DatabaseCompat)ser.Deserialize(ms, null, typeof(DAL.DatabaseCompat)); StringBuilder perfStats = new StringBuilder(); perfStats.AppendLine(".NET for Metro style apps"); perfStats.AppendFormat("{0} orders from NWind", dal.Orders.Count).AppendLine(); var dcs = new DataContractSerializer(typeof(DAL.DatabaseCompat)); var xs = new XmlSerializer(typeof(DAL.DatabaseCompat)); using (var buffer = new MemoryStream()) { const int loop = 50; var watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); dcs.WriteObject(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("DataContractSerializer:").AppendFormat("WriteObject x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; dcs.ReadObject(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("ReadObject x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); xs.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("XmlSerializer:").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; xs.Deserialize(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); ser.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("protobuf-net").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; ser.Deserialize(buffer, null, typeof(DAL.DatabaseCompat)); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.SetLength(0); Serializer.Serialize(buffer, dal); } watch.Stop(); perfStats.AppendLine().AppendLine().AppendLine("protobuf-net (runtime)").AppendFormat("Serialize x {0}: {1:###,###}ms, {2:###,###} bytes", loop, watch.ElapsedMilliseconds, buffer.Length); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { buffer.Position = 0; Serializer.Deserialize<DAL.DatabaseCompat>(buffer); } watch.Stop(); perfStats.AppendLine().AppendFormat("Deserialize x {0}: {1:###,###}ms", loop, watch.ElapsedMilliseconds); } button.Content = perfStats.ToString(); // test SM2Stats bool isSer = ser.CanSerializeContractType(typeof(SM2Stats)); SM2Stats stats = new SM2Stats { acqreps = 1, difficultyhistory = { new DiffHistPair { date = 123, values = { 3.4f }} } }; var clone = (SM2Stats)ser.DeepClone(stats); // checked by eye; is the same }