近期有个性能调优工作。通过dottrace 分析,发现几处问题,其中json.net 在序列化和反序列化的时候也比较耗性能,所以考虑能不能通过其它序列化方式来提高性能。
1 object 序列化代码
public class ObjectConvert { /// <summary> /// 将一个object对象序列化,返回一个byte[] /// </summary> /// <param name="obj">能序列化的对象</param> /// <returns></returns> public static byte[] ObjectToBytes(object obj) { using (MemoryStream ms = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms,obj); return ms.GetBuffer(); } } /// <summary> /// 将一个序列化后的byte[]数组还原 /// </summary> /// <param name="Bytes"></param> /// <returns></returns> public static object BytesToObject(byte[] Bytes) { using (MemoryStream ms = new MemoryStream(Bytes)) { IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms); } } }
2 Test 方法,用了 老赵写的codetimer
[TestClass] public class SerializeTestor { private List<UserInfo> InitData(int count) { UserInfo user = null; List<UserInfo> users = new List<UserInfo>(); for (int i = 0; i < count; i++) { user = new UserInfo(); user.Age = new Random().Next(10,100); user.Name = "yuan"; user.CreateTime = DateTime.Now; users.Add(user); } return users; } private List<UserInfo> InitBigStringData(int count) { UserInfo user = null; List<UserInfo> users = new List<UserInfo>(); for (int i = 0; i < count; i++) { user = new UserInfo(); user.Age = new Random().Next(10,100); //user.Name = "yuan"; user.Name = "yudddddddddddddddddddddddddddyudddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddanyudddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddanyudddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddanyudddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddanddddddddddddddddddddddddddddddddddddddddddddddddddan"; user.CreateTime = DateTime.Now; users.Add(user); } return users; } [TestMethod] public void Serialize_十万次_Test() { var users = InitData(100000); // object序列化 CodeTimer.Time("object序列化",1,() => { var temps = ObjectConvert.ObjectToBytes(users); }); // newtonsoft.json CodeTimer.Time("json.net序列化",() => { var temps = JsonConvert.SerializeObject(users); }); } [TestMethod] public void Serialize_百万次_Test() { var users = InitData(1000000); // object序列化 CodeTimer.Time("object序列化",() => { var temps = JsonConvert.SerializeObject(users); }); } [TestMethod] public void SerializeBigString_十万次_Test() { var users = InitBigStringData(100000); // object序列化 CodeTimer.Time("object序列化",() => { var temps = JsonConvert.SerializeObject(users); }); } [TestMethod] public void SerializeBigString_百万次_Test() { var users = InitBigStringData(1000000); // object序列化 CodeTimer.Time("object序列化",() => { var temps = JsonConvert.SerializeObject(users); }); } }
3 测试结果:
Serialize_十万次_Test: object序列化 Time Elapsed: 188ms Time Elapsed (one time):188ms cpu time: 187,500,000ns cpu time (one time): 187,000ns Gen 0: 1 Gen 1: 0 Gen 2: 0 json.net序列化 Time Elapsed: 138ms Time Elapsed (one time):138ms cpu time: 140,625,000ns cpu time (one time): 140,000ns Gen 0: 3 Gen 1: 1 Gen 2: 0 ================================================ Serialize_百万次_Test object序列化 Time Elapsed: 2,073ms Time Elapsed (one time):2,073ms cpu time: 2,078,125,000ns cpu time (one time): 2,000ns Gen 0: 23 Gen 1: 1 Gen 2: 0 json.net序列化 Time Elapsed: 1,449ms Time Elapsed (one time):1,449ms cpu time: 1,437,000ns cpu time (one time): 1,000ns Gen 0: 28 Gen 1: 27 Gen 2: 0 =================================================== SerializeBigString_十万次_Test object序列化 Time Elapsed: 187ms Time Elapsed (one time):187ms cpu time: 187,000ns Gen 0: 3 Gen 1: 2 Gen 2: 2 json.net序列化 Time Elapsed: 339ms Time Elapsed (one time):339ms cpu time: 328,000ns cpu time (one time): 328,000ns Gen 0: 19 Gen 1: 14 Gen 2: 3 ====================================================== SerializeBigString_百万次_Test object序列化 Time Elapsed: 2,099ms Time Elapsed (one time):2,099ms cpu time: 2,046,875,000ns Gen 0: 26 Gen 1: 25 Gen 2: 3 json.net序列化 System.OutOfMemoryException: 引发类型为“System.OutOfMemoryException”的异常。
4 结论:
JSON.NET 在小对象处理时,性能比BinaryFormatter好
JSON.NET 比BinaryFormatter 耗内存
JSON.NET GC中带回收的对象会比较影响整体性能
原文链接:https://www.f2er.com/json/289024.html