几个常用Json组件的性能测试
作者:网络转载 发布时间:[ 2015/4/10 14:30:13 ] 推荐标签:性能测试 Json组件 测试用例
//测试FastJson性能
static void TestFastJson(int count, object obj)
{
Stopwatch sw = new Stopwatch();
JSONParameters p = new JSONParameters();
p.EnableAnonymousTypes
= p.IgnoreCaseOnDeserialize
= p.ShowReadOnlyProperties
= p.UseEscapedUnicode
= p.UseExtensions
= p.UseFastGuid
= p.UseOptimizedDatasetSchema
= p.UseUTCDateTime
= p.UsingGlobalTypes
= false;
Console.WriteLine();
Console.WriteLine("fastJSON.NET 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Restart();
for (int i = 0; i < count; i++)
{
fastJSON.JSON.Instance.ToJSON(obj, p);
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
Console.WriteLine();
Console.WriteLine("结果:" + fastJSON.JSON.Instance.ToJSON(obj, p));
Console.WriteLine("==============================================");
}
//测试JayrockJson性能
static void TestJayrockJson(int count, object obj)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine();
Console.WriteLine("Jayrock.Json 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Restart();
for (int i = 0; i < count; i++)
{
var writer = new Jayrock.Json.JsonTextWriter();
Jayrock.Json.Conversion.JsonConvert.Export(obj, writer);
string str = writer.ToString();
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
var w = new Jayrock.Json.JsonTextWriter();
Jayrock.Json.Conversion.JsonConvert.Export(obj, w);
Console.WriteLine();
Console.WriteLine("结果:" + w.ToString());
Console.WriteLine("==============================================");
}
static void TestNewtonsoftJson(int count, object obj)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine();
Console.WriteLine("Newtonsoft.Json 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Restart();
for (int i = 0; i < count; i++)
{
Newtonsoft.Json.JsonConvert.SerializeObject(obj);
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
var w = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
Console.WriteLine();
Console.WriteLine("结果:" + w.ToString());
Console.WriteLine("==============================================");
}
static void TestJavaScriptSerializer(int count, object obj)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine();
Console.WriteLine("JavaScriptSerializer 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Restart();
for (int i = 0; i < count; i++)
{
new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
var w = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
Console.WriteLine();
Console.WriteLine("结果:" + w.ToString());
Console.WriteLine("==============================================");
}
//测试JsonBuilder性能
static void JsonBuilder(int count, object obj)
{
Stopwatch sw = new Stopwatch();
Console.WriteLine();
Console.WriteLine("QuickJsonBuilder 每次" + count + " 共10次");
for (int j = 0; j < 10; j++)
{
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
{
JsonBuilder jb = new JsonBuilder();
jb.ToJsonString(obj);
}
sw.Stop();
Console.Write(sw.ElapsedMilliseconds + "ms | ");
}
Console.WriteLine();
Console.WriteLine("结果:" + new JsonBuilder().ToJsonString(obj));
Console.WriteLine("==============================================");
}
}
}
测试方案1:微软自带的的 JavaScriptSerializer
引用组件:System.Web.Extensions(微软自带了,引用下好)
string str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(User.TestUser());
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"/Date(530964000000)/","Sex":0,"IsDeleted":false,"LoginHistory":["/Date(1377129600000)/","/Date(1377137410000)/","/Date(1377146036000)/","/Date(1377163518000)/","/Date(1377184019000)/"],"Info":{"Address":"广东省广州市","Phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
测试方案2:Newtonsoft.Json
引用组件:Newtonsoft.Json.Net35.dll
string str = Newtonsoft.Json.JsonConvert.SerializeObject(User.TestUser());
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"/Date(530964000000+0800)/","Sex":0,"IsDeleted":false,"LoginHistory":["/Date(1377129600000+0800)/","/Date(1377137410000+0800)/","/Date(1377146036000+0800)/","/Date(1377163518000+0800)/","/Date(1377184019000+0800)/"],"Info":{"Address":"广东省广州市","Phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
测试方案3:Jayrock.Json
引用组件:Jayrock.Json.dll
JsonTextWriter jtw = new Jayrock.Json.JsonTextWriter();
Jayrock.Json.Conversion.JsonConvert.Export(User.TestUser(), jtw);
string str = jtw.ToString();
//{"uID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","name":"blqw","birthday":"1986-10-29T18:00:00.0000000+08:00","sex":"Male","isDeleted":false,"loginHistory":["2013-08-22T08:00:00.0000000+08:00","2013-08-22T10:10:10.0000000+08:00","2013-08-22T12:33:56.0000000+08:00","2013-08-22T17:25:18.0000000+08:00","2013-08-22T23:06:59.0000000+08:00"],"info":{"address":"广东省广州市","phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"zipCode":510000},"double":-Infinity}
测试方案4:fastJSON.NET
引用组件:fastJSON.dll
JSONParameters p = new JSONParameters();
p.EnableAnonymousTypes
= p.IgnoreCaseOnDeserialize
= p.ShowReadOnlyProperties
= p.UseEscapedUnicode
= p.UseExtensions
= p.UseFastGuid
= p.UseOptimizedDatasetSchema
= p.UseUTCDateTime
= p.UsingGlobalTypes
= false;//所有参数为false,执行速度快
string str = fastJSON.JSON.Instance.ToJSON(User.TestUser(), p);
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"广东省广州市","Phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
测试方案5,6:QuickJsonBuilder,JsonBuilder
ps:以上几种类型都不支持循环引用,如果有,直接抛出异常
说明:QuickJsonBuilder是我上篇中介绍的JsonBuilder的形态
QuickJsonBuilder jb = new QuickJsonBuilder();
jb.ToJson(User.TestUser());
//{"Self":null,"UID":"6f50b429-5c13-4875-a29a-e4bd8d7b2772","Name":"blqw","Birthday":"1986-10-29 18:00:00","Sex":"Male","IsDeleted":false,"LoginHistory":["2013-08-22 08:00:00","2013-08-22 10:10:10","2013-08-22 12:33:56","2013-08-22 17:25:18","2013-08-22 23:06:59"],"Info":{"Address":"广东省广州市","Phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:当Self指向自己本身的时候,将返回 "Self":undefined 用以区分空对象
测试方案7:ServiceStack.Text
引用组件:ServiceStack.Text.dll 抱歉我只找到4.0的
这个是后来补上的,测试代码中没有这个...
User.TestUser().ToJson();
//{"UID":"48d4624ad0c8475094d00fc769c7129f","Name":"blqw","Birthday":"/Date(530964000000+0800)/","Sex":"Male","IsDeleted":false,"LoginHistory":["/Date(1377129600000+0800)/","/Date(1377137410000+0800)/","/Date(1377146036000+0800)/","/Date(1377163518000+0800)/","/Date(1377184019000+0800)/"],"Info":{"Address":"广东省广州市","Phone":{"手机":"18688888888","电话":"82580000","短号":"10086","QQ":"21979018"},"ZipCode":510000},"Double":-Infinity}
ps:依然不支持循环引用,抛出堆栈异常
测试结果(单位 "毫秒")
测试结果仅供参考
总结
可以看出基本所有组件在第一轮用时都是长的,应该是在处理一些初始化和缓存的工作。
所以呢,我特别给出了1次的循环结果,
JavaScriptSerializer 可以看出在100次以内的时候微软的还是有一些优势的,随着次数增加性能渐渐跟不上了
Newtonsoft.Json一直表现平平啊,没有特别突出地方,跟JsonBuilder比性能也没有好到那里去
Jayrock.Json 是昨天一位博友告诉我的,据说是淘宝sdk在用的,性能只能说,好差。。。
fastJSON.NET 确实是所有组件中快的,QuickJsonBuilder也从中借鉴了一些处理方式
ServiceStack.Text 中规中矩吧比Newtonsoft.Json好一点点.是我只找到4.0的拓展方法,不知道2.0下有没有相应的组件
好吧 我自己的组件我不做评价了
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南