日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转]序列化悍将Protobuf-Net,入门动手实录

發(fā)布時間:2023/11/29 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]序列化悍将Protobuf-Net,入门动手实录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近在研究web api 2,看了一篇文章,講解如何提升性能的,

在序列化速度的跑分中,Protobuf一騎絕塵,序列化速度快,性能強(qiáng),體積小,所以打算了解下這個利器

?

1:安裝篇

谷歌官方?jīng)]有提供.net的實(shí)現(xiàn),所以在nuget上找了一個移植的

Nuget里搜索Protobuf-net,下載,自動添加到項(xiàng)目中

?

2:定義數(shù)據(jù)結(jié)構(gòu)?

using ProtoBuf;namespace ConsoleApplication1 {[ProtoContract]class Person{[ProtoMember(1)]public int Id { get; set; }[ProtoMember(2)]public string Name { get; set; }[ProtoMember(3)]public Address Address { get; set; }}[ProtoContract]class Address{[ProtoMember(1)]public string Line1 { get; set; }[ProtoMember(2)]public string Line2 { get; set; }} }

??

3:封裝簡單操作類

按照作者使用習(xí)慣,簡單提供了一個Helper類

using System.IO; using System.Text; using ProtoBuf;namespace ConsoleApplication1 {public class ProtobufHelper{/// <summary>/// 序列化/// </summary>/// <typeparam name="T"></typeparam>/// <param name="t"></param>/// <returns></returns>public static string Serialize<T>(T t){using (MemoryStream ms = new MemoryStream()){Serializer.Serialize<T>(ms, t);return Encoding.UTF8.GetString(ms.ToArray());}}/// <summary>/// 反序列化/// </summary>/// <typeparam name="T"></typeparam>/// <param name="content"></param>/// <returns></returns>public static T DeSerialize<T>(string content){using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content))){T t = Serializer.Deserialize<T>(ms);return t;}}} }

?

4:操作體驗(yàn)

代碼很簡單,就不分開貼了

using System; using System.Collections.Generic; using System.IO;namespace ConsoleApplication1 {class Program{static void Main(string[] args){var p1 = new Person{Id = 1,Name = "八百里開外",Address = new Address{Line1 = "Line1",Line2 = "Line2"}};var p2 = new Person{Id = 2,Name = "一槍",Address = new Address{Line1 = "Flat Line1",Line2 = "Flat Line2"}};List<Person> pSource = new List<Person>() { p1, p2 };string content = ProtobufHelper.Serialize<List<Person>>(pSource);Console.Write(content);//寫入文件File.WriteAllText("D://hello.txt", content);Console.WriteLine("\r\n****解析部分*****");List<Person> pResult = ProtobufHelper.DeSerialize<List<Person>>(content);foreach (Person p in pResult){Console.WriteLine(p.Name);}Console.Read();}} }

控制臺運(yùn)行結(jié)果

?

同樣的數(shù)據(jù),和Json所占用空間對比,高下立判

?

后記

protobuf雖然有千般好,但是我們是在 web api上使用的,前臺js解析不了Protobuf,所以只能用Json咯~!

StackService雖然Github上有2K多個Star,但是收費(fèi)的。。同樣的事情web api 2也能做到,所以也略過它。

最終作者選擇了跑分測試?yán)锩娴牡诙鸍il? https://github.com/kevin-montrose/Jil

?

?


?

1. With very minimal annotation on the class level

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)] // only required on the class level class PersonEntity {public string FirstName { get; set; }public string LastName { get; set; } }

?

2. Without any annotation (using RuntimeTypeModel)

static void InitializeProtobufRunTime() {var assembly = Assembly.GetAssembly(typeof(PlainEntities.PersonEntity));var types = assembly.GetTypes();foreach (var t in types.Where(x => x.Namespace.Contains("PlainEntities"))){Console.WriteLine("Processing {0}", t.FullName);var meta = RuntimeTypeModel.Default.Add(t, false);var index = 1;// find any derived class for the entityforeach (var d in types.Where(x => x.IsSubclassOf(t))){var i = index++;Console.WriteLine("\tSubtype: {0} - #{1}", d.Name, i);meta.AddSubType(i, d);}// then add the propertiesforeach (var p in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).Where(x => x.GetSetMethod() != null)){var i = index++;Console.WriteLine("\tProperty: {0} - #{1}", p.Name, i);meta.AddField(i, p.Name);}} }

?

And both the above works quite well without any performance differences.


------------------
TestBinaryEntities
------------------
Process: 100000 items, MemorySize: 7400705, Completed in: 3877 ms, Serialization took: 676 ms, Deserialization took: 2948 ms

----------------------------------
TestProtobufFullyAnnotatedEntities
----------------------------------
Process: 100000 items, MemorySize: 3983490, Completed in: 682 ms, Serialization took: 164 ms, Deserialization took: 253 ms

-------------------------------------
TestProtobufImplicitAnnotatedEntities
-------------------------------------
Process: 100000 items, MemorySize: 3983490, Completed in: 595 ms, Serialization took: 104 ms, Deserialization took: 210 ms

-------------------------------
TestProtobufRuntimeRegistration
-------------------------------
Processing ProtobufTestConsole.PlainEntities.BaseEntity
Subtype: PersonEntity - #1
Property: Id - #2
Property: Gender - #3
Processing ProtobufTestConsole.PlainEntities.PersonEntity
Property: FirstName - #1
Property: LastName - #2
Property: Age - #3
Process: 100000 items, MemorySize: 4083490, Completed in: 646 ms, Serialization took: 113 ms, Deserialization took: 232 ms

Looking forward to get this in :)

Also attached the sample project for reference

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhahost/p/5813627.html

總結(jié)

以上是生活随笔為你收集整理的[转]序列化悍将Protobuf-Net,入门动手实录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。