public static TestResult AdvancedTest(object graph) { while (true) { int len = testCount; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); JsonSerializerSettings nset = new JsonSerializerSettings(); //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; DataContractJsonSerializer ser = new DataContractJsonSerializer(graph.GetType()); BinaryFormatter binserializer = new BinaryFormatter(); MemoryStream msBin = new MemoryStream(); binserializer.Serialize(msBin, graph); //JavaScriptSerializer serializer = new JavaScriptSerializer(); int size = 0; string s = string.Empty; if (isString) { JsonString os = new JsonString(capacity); if (isInfo) { ShiboSerializer.Serialize(os, graph, info, sets); } else { ShiboSerializer.Serialize(os, graph, sets); } s = os.ToString(); } else { //JsonStream os = new JsonStream(); //ShiboSerializer.Serialize(os, graph, sets); //s = os.ToString(); } //fastJSON.JSONParameters fp = new fastJSON.JSONParameters() { UsingGlobalTypes = false, UseExtensions = false }; MemoryStream msPub = new MemoryStream(); if (isConsole) { Console.WriteLine(s); Console.WriteLine(); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)); Console.WriteLine(); if (isPub == true) { Serializer.Serialize(msPub, graph); Console.WriteLine("PB:" + msPub.Length.ToString()); Console.WriteLine(); } Console.WriteLine(ServiceStack.Text.JsonSerializer.SerializeToString(graph)); Console.WriteLine(); //Console.WriteLine(fastJSON.JSON.Instance.ToJSON(graph, fp)); //Console.WriteLine(); //Console.WriteLine(serializer.Serialize(graph)); Console.WriteLine(); Console.WriteLine(TesterUitls.JsonSerializer(ser, graph)); Console.WriteLine(); } else { Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); if (isPub == true) { Serializer.Serialize(msPub, graph); } ServiceStack.Text.JsonSerializer.SerializeToString(graph); //fastJSON.JSON.Instance.ToJSON(graph, fp); //serializer.Serialize(graph); TesterUitls.JsonSerializer(ser, graph); } Console.WriteLine(); if (s != Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)) { Console.WriteLine("may error --Json.Net "); } else { Console.WriteLine("ok --Json.Net"); } Console.WriteLine(); if (s != ServiceStack.Text.JsonSerializer.SerializeToString(graph)) { Console.WriteLine("may error ----ServiceStack.Text"); } else { Console.WriteLine("ok --ServiceStack.Text"); } Stopwatch w = Stopwatch.StartNew(); w.Start(); w = System.Diagnostics.Stopwatch.StartNew(); w.Start(); if (isString) { if (isInfo) { for (int i = 0; i < len; i++) { JsonString os = new JsonString(capacity); ShiboSerializer.Serialize(os, graph, info, sets); if (toString == false) { size += os.Position; } else { size += os.ToString().Length; } } } else { for (int i = 0; i < len; i++) { JsonString os = new JsonString(capacity); ShiboSerializer.Serialize(os, graph, sets); if (toString == false) { size += os.Position; } else { size += os.ToString().Length; } } } } else { for (int i = 0; i < len; i++) { //JsonStream os = new JsonStream(capacity); //ShiboSerializer.Serialize(os, graph, sets); //if (toString == false) // size += os.Position; //else // size += os.ToString().Length; } } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Fastest.Json Serializer= " + w.ElapsedMilliseconds + " v:" + size); size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time JsonConvert Serializer= " + w.ElapsedMilliseconds + " v:" + size); if (isPub) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { msPub = new MemoryStream(); Serializer.Serialize(msPub, graph); size += (int)msPub.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Protocol Buffer Serializer= " + w.ElapsedMilliseconds + " v:" + size); } size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = ServiceStack.Text.JsonSerializer.SerializeToString(graph); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ServiceStack Serializer= " + w.ElapsedMilliseconds + " v:" + size); size = 0; w.Restart(); for (int i = 0; i < len; i++) { size += graph.ToString().Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time StringBuilder Serializer= " + w.ElapsedMilliseconds + " v:" + size); size = 0; w.Restart(); for (int i = 0; i < len; i++) { MemoryStream stream = new MemoryStream(); binserializer.Serialize(stream, graph); size += (int)stream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time BinaryFormatter Serializer= " + w.ElapsedMilliseconds + " v:" + size); //size = 0; //w.Restart(); //for (int i = 0; i < len; i++) //{ // string st = serializer.Serialize(graph); // size += st.Length; //} //w.Stop(); //Console.WriteLine(); //Console.WriteLine("Time JavaScriptSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = TesterUitls.JsonSerializer(ser, graph); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time DataContractJsonSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); break; } return(new TestResult()); }
//public static TestResult Test(string json) //{ // TestResult result = new TestResult(); // //result.Code = JsonClassGenerator.GenerateString(json, CodeLanguage.CSharp); // Stopwatch w = Stopwatch.StartNew(); // Type type = TestUtils.Compile(result.Code, json, ref result); // w.Stop(); // result.CompileTime = (int)w.ElapsedMilliseconds; // Console.WriteLine("动态编译耗时:" + w.ElapsedMilliseconds); // object graph = TestUtils.GetGraph(type, json); // Took socTook = new Took(); // Took fastestJsonTook = new Took(); // Took newtonsoftTook = new Took(); // Took serviceStackTook = new Took(); // Took fastJsonTook = new Took(); // Took javaScriptTook = new Took(); // Took dataContractTook = new Took(); // Took binTook = new Took(); // Took protobufTook = new Took(); // Took msgPackTook = new Took(); // while (true) // { // #region 参数 // int len = testCount; // JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); // JsonSerializerSettings nset = new JsonSerializerSettings(); // //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; // DataContractJsonSerializer ser = new DataContractJsonSerializer(graph.GetType()); // BinaryFormatter binserializer = new BinaryFormatter(); // MemoryStream msBin = new MemoryStream(); // JavaScriptSerializer serializer = new JavaScriptSerializer(); // #endregion // #region 启动 // int size = 0; // string s = string.Empty; // JsonString os = null; // if (isBuffer) // os = new JsonString(jsonBuffer); // else // os = new JsonString(); // if (TestBaseConfig.Fastest) // { // ShiboSerializer.Serialize(os, graph, sets); // fastestJsonTook.Json = os.ToString(); // } // if (TestBaseConfig.Newtonsoft) // newtonsoftTook.Json = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); // if (TestBaseConfig.ServiceStack) // serviceStackTook.Json = ServiceStack.Text.JsonSerializer.SerializeToString(graph); // if (TestBaseConfig.BinaryFormatter) // binserializer.Serialize(msBin, graph); // if (TestBaseConfig.JavaScriptSerializer) // javaScriptTook.Json = serializer.Serialize(graph); // if (TestBaseConfig.DataContractJsonSerializer) // dataContractTook.Json = TesterUitls.JsonSerializer(ser, graph); // if (TestBaseConfig.Fastjson) // fastJsonTook.Json = fastJSON.JSON.ToJSON(graph); // #endregion // #region 检查结果 // s = os.ToString(); // if (TestBaseConfig.Newtonsoft) // { // Console.WriteLine(); // if (s != Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)) // Console.WriteLine("may error --Json.Net "); // else // Console.WriteLine("ok --Json.Net"); // } // if (TestBaseConfig.ServiceStack) // { // Console.WriteLine(); // if (s != ServiceStack.Text.JsonSerializer.SerializeToString(graph)) // Console.WriteLine("may error ----ServiceStack.Text"); // else // Console.WriteLine("ok --ServiceStack.Text"); // } // if (TestBaseConfig.Fastjson) // { // Console.WriteLine(); // if (s != fastJSON.JSON.ToJSON(graph)) // Console.WriteLine("may error ----fastJSON"); // else // Console.WriteLine("ok --fastJSON"); // } // #endregion // #region 测试 // JsonStringContext context = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); // w.Restart(); // for (int i = 0; i < len; i++) // { // if (isBuffer) // os = new JsonString(jsonBuffer); // else // os = new JsonString(); // if (isInfo) // ShiboSerializer.Serialize(os, graph, context, sets); // else // ShiboSerializer.Serialize(os, graph, sets); // //os = ShiboSerializer.SerializeToBuffer(graph); // if (toString) // size += os.ToString().Length; // else // size += os.Position; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time Fastest.Json Serializer= " + w.ElapsedMilliseconds + " v:" + size); // if (TestBaseConfig.Newtonsoft) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time JsonConvert Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // if (TestBaseConfig.ServiceStack) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = ServiceStack.Text.JsonSerializer.SerializeToString(graph); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time ServiceStack Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // if (TestBaseConfig.Fastjson) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = fastJSON.JSON.ToJSON(graph); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time fastJSON Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // //v = 0; // //w.Restart(); // //for (int i = 0; i < len; i++) // //{ // // v += graph.ToString().Length; // //} // //w.Stop(); // //Console.WriteLine(); // //Console.WriteLine("Time StringBuilder Serializer= " + w.ElapsedMilliseconds + " v:" + v); // if (TestBaseConfig.BinaryFormatter) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // MemoryStream stream = new MemoryStream(); // binserializer.Serialize(stream, graph); // size += (int)stream.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time BinaryFormatter Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // if (TestBaseConfig.JavaScriptSerializer) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = serializer.Serialize(graph); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time JavaScriptSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // if (TestBaseConfig.DataContractJsonSerializer) // { // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = TesterUitls.JsonSerializer(ser, graph); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time DataContractJsonSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); // } // #endregion // break; // } // return result; //} public static TestResult TestWithProto(object graph) { TestResult result = new TestResult(); while (true) { #region 参数 int len = testCount; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); JsonSerializerSettings nset = new JsonSerializerSettings(); //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; DataContractJsonSerializer ser = new DataContractJsonSerializer(graph.GetType()); BinaryFormatter binserializer = new BinaryFormatter(); MemoryStream msBin = new MemoryStream(); binserializer.Serialize(msBin, graph); //JavaScriptSerializer serializer = new JavaScriptSerializer(); #endregion #region 启动 int v = 0; string s = string.Empty; JsonString os = new JsonString(capacity); ShiboSerializer.Serialize(os, graph, sets); s = os.ToString(); MemoryStream msPub = new MemoryStream(); Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); if (isPub == true) { Serializer.Serialize(msPub, graph); } ServiceStack.Text.JsonSerializer.SerializeToString(graph); //serializer.Serialize(graph); TesterUitls.JsonSerializer(ser, graph); #endregion #region 检查结果 Console.WriteLine(); if (s != Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)) { Console.WriteLine("may error --Json.Net "); } else { Console.WriteLine("ok --Json.Net"); } Console.WriteLine(); if (s != ServiceStack.Text.JsonSerializer.SerializeToString(graph)) { Console.WriteLine("may error ----ServiceStack.Text"); } else { Console.WriteLine("ok --ServiceStack.Text"); } #endregion #region 测试 Stopwatch w = System.Diagnostics.Stopwatch.StartNew(); w.Start(); for (int i = 0; i < len; i++) { os = new JsonString(capacity); ShiboSerializer.Serialize(os, graph, sets); } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Fastest.Json Serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time JsonConvert Serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = ServiceStack.Text.JsonSerializer.SerializeToString(graph); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ServiceStack Serializer= " + w.ElapsedMilliseconds + " v:" + v); //v = 0; //w.Restart(); //for (int i = 0; i < len; i++) //{ // v += graph.ToString().Length; //} //w.Stop(); //Console.WriteLine(); //Console.WriteLine("Time StringBuilder Serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { MemoryStream stream = new MemoryStream(); binserializer.Serialize(stream, graph); v += (int)stream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time BinaryFormatter Serializer= " + w.ElapsedMilliseconds + " v:" + v); //v = 0; //w.Restart(); //for (int i = 0; i < len; i++) //{ // string st = serializer.Serialize(graph); // v += st.Length; //} //w.Stop(); //Console.WriteLine(); //Console.WriteLine("Time JavaScriptSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = TesterUitls.JsonSerializer(ser, graph); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time DataContractJsonSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + v); #endregion break; } return(result); }
//static int capacity = 1000000; //static char[] jsonBuffer = new char[capacity]; //static bool isCapacity = true; //static bool isBuffer = true; //static int defaultSize = 500; //static bool isPub = false; //static bool isString = true; //static int testCount = 100; //static bool toString = false; //static bool isInfo = false; //static bool isConsole = true; //static SerializerSettings sets = new SerializerSettings(); #endregion #region 序列化 public static TestResult Test(object graph) { TestResult result = new TestResult(); while (true) { #region 参数 int len = testCount; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); JsonSerializerSettings nset = new JsonSerializerSettings(); //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; DataContractJsonSerializer ser = new DataContractJsonSerializer(graph.GetType()); BinaryFormatter binserializer = new BinaryFormatter(); MemoryStream msBin = new MemoryStream(); //JavaScriptSerializer serializer = new JavaScriptSerializer(); #endregion #region 启动 int size = 0; string s = string.Empty; JsonString os = null; if (isBuffer) { os = new JsonString(jsonBuffer); } else { os = new JsonString(); } ShiboSerializer.Serialize(os, graph, sets); if (TestBaseConfig.Newtonsoft) { Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); } if (TestBaseConfig.ServiceStack) { ServiceStack.Text.JsonSerializer.SerializeToString(graph); } if (TestBaseConfig.BinaryFormatter) { binserializer.Serialize(msBin, graph); } //if (TestBaseConfig.JavaScriptSerializer) // serializer.Serialize(graph); if (TestBaseConfig.DataContractJsonSerializer) { TesterUitls.JsonSerializer(ser, graph); } #endregion #region 检查结果 s = os.ToString(); if (TestBaseConfig.Newtonsoft) { Console.WriteLine(); if (s != Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)) { Console.WriteLine("may error --Json.Net "); } else { Console.WriteLine("ok --Json.Net"); } } if (TestBaseConfig.ServiceStack) { Console.WriteLine(); if (s != ServiceStack.Text.JsonSerializer.SerializeToString(graph)) { Console.WriteLine("may error ----ServiceStack.Text"); } else { Console.WriteLine("ok --ServiceStack.Text"); } } if (TestBaseConfig.Fastjson) { Console.WriteLine(); if (s != fastJSON.JSON.ToJSON(graph)) { Console.WriteLine("may error ----fastJSON"); } else { Console.WriteLine("ok --fastJSON"); } } #endregion #region 测试 JsonStringContext context = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); Stopwatch w = System.Diagnostics.Stopwatch.StartNew(); w.Start(); for (int i = 0; i < len; i++) { if (isBuffer) { os = new JsonString(jsonBuffer); } else { os = new JsonString(); } if (isInfo) { ShiboSerializer.Serialize(os, graph, context, sets); } else { ShiboSerializer.Serialize(os, graph, sets); } //os = ShiboSerializer.SerializeToBuffer(graph); if (toString) { size += os.ToString().Length; } else { size += os.Position; } } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Fastest.Json Serializer= " + w.ElapsedMilliseconds + " v:" + size); if (TestBaseConfig.Newtonsoft) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time JsonConvert Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.ServiceStack) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = ServiceStack.Text.JsonSerializer.SerializeToString(graph); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ServiceStack Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.Fastjson) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = fastJSON.JSON.ToJSON(graph); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time fastJSON Serializer= " + w.ElapsedMilliseconds + " v:" + size); } //v = 0; //w.Restart(); //for (int i = 0; i < len; i++) //{ // v += graph.ToString().Length; //} //w.Stop(); //Console.WriteLine(); //Console.WriteLine("Time StringBuilder Serializer= " + w.ElapsedMilliseconds + " v:" + v); if (TestBaseConfig.BinaryFormatter) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { MemoryStream stream = new MemoryStream(); binserializer.Serialize(stream, graph); size += (int)stream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time BinaryFormatter Serializer= " + w.ElapsedMilliseconds + " v:" + size); } //if (TestBaseConfig.JavaScriptSerializer) //{ // size = 0; // w.Restart(); // for (int i = 0; i < len; i++) // { // string st = serializer.Serialize(graph); // size += st.Length; // } // w.Stop(); // Console.WriteLine(); // Console.WriteLine("Time JavaScriptSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); //} if (TestBaseConfig.DataContractJsonSerializer) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = TesterUitls.JsonSerializer(ser, graph); size += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time DataContractJsonSerializer Serializer= " + w.ElapsedMilliseconds + " v:" + size); } #endregion break; } return(result); }
private static void serialize(JsonResult result, int count) { string s = ""; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(result.GetType()); Console.WriteLine("序列化开始({0}),单位毫秒", count); System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch(); Console.WriteLine("Fastest.Json"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { JsonString stream = new JsonString(defaultSize); if (isInfo) { ShiboSerializer.Serialize(stream, result, info, sets); } else { ShiboSerializer.Serialize(stream, result, sets); } s = stream.ToString(); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); Console.WriteLine("\r\nServiceStack.Text"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = ServiceStack.Text.JsonSerializer.SerializeToString <JsonResult>(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); Console.WriteLine("\r\nNewtonsoft.Json"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = Newtonsoft.Json.JsonConvert.SerializeObject(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); //Console.WriteLine("\r\nJavaScriptSerializer"); //JavaScriptSerializer serializer = new JavaScriptSerializer(); //for (int i = 0; i < 5; i++) //{ // w.Start(); // for (int j = 0; j < count; j++) // { // s = serializer.Serialize(result); // } // w.Stop(); // Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); // w.Reset(); //} //Console.WriteLine(s); Console.WriteLine("\r\nDataContractJsonSerializer"); for (int i = 0; i < 5; i++) { w.Start(); for (int j = 0; j < count; j++) { s = JsonSerializer(result); } w.Stop(); Console.Write("{0} ", (w.ElapsedMilliseconds / (count * 1.0)).ToString()); w.Reset(); } Console.WriteLine(s); }
public static void JsonTest(object graph) { while (true) { int len = testCount; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); JsonSerializerSettings nset = new JsonSerializerSettings(); //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; DataContractJsonSerializer ser = new DataContractJsonSerializer(graph.GetType()); BinaryFormatter binserializer = new BinaryFormatter(); MemoryStream msBin = new MemoryStream(); binserializer.Serialize(msBin, graph); //JavaScriptSerializer serializer = new JavaScriptSerializer(); int v = 0; string s = string.Empty; if (isString) { JsonString os = new JsonString(buffer); if (isInfo) { ShiboSerializer.Serialize(os, graph, info, sets); } else { ShiboSerializer.Serialize(os, graph, sets); } s = os.ToString(); } else { //JsonStream os = new JsonStream(); //ShiboSerializer.Serialize(os, graph, sets); //s = os.ToString(); } //fastJSON.JSONParameters fp = new fastJSON.JSONParameters() { UsingGlobalTypes = false, UseExtensions = false }; MemoryStream msPub = new MemoryStream(); if (isConsole) { Console.WriteLine(s); Console.WriteLine(); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)); Console.WriteLine(); if (isPub == true) { Serializer.Serialize(msPub, graph); Console.WriteLine("PB:" + msPub.Length.ToString()); Console.WriteLine(); } Console.WriteLine(ServiceStack.Text.JsonSerializer.SerializeToString(graph)); Console.WriteLine(); //Console.WriteLine(fastJSON.JSON.Instance.ToJSON(graph, fp)); //Console.WriteLine(); //Console.WriteLine(serializer.Serialize(graph)); Console.WriteLine(); Console.WriteLine(JsonSerializer(ser, graph)); Console.WriteLine(); } else { Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); if (isPub == true) { Serializer.Serialize(msPub, graph); } ServiceStack.Text.JsonSerializer.SerializeToString(graph); //fastJSON.JSON.Instance.ToJSON(graph, fp); //serializer.Serialize(graph); JsonSerializer(ser, graph); } Console.WriteLine(); if (s != Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset)) { Console.WriteLine("may error --Json.Net "); } else { Console.WriteLine("ok --Json.Net"); } Console.WriteLine(); if (s != ServiceStack.Text.JsonSerializer.SerializeToString(graph)) { Console.WriteLine("may error ----ServiceStack.Text"); } else { Console.WriteLine("ok --ServiceStack.Text"); } //Console.WriteLine(); //if (s != fastJSON.JSON.Instance.ToJSON(graph, fp)) // Console.WriteLine("结果不一样,有错误! ----fastJSON"); //else // Console.WriteLine("和 fastJSON 正确"); //if (s.Length != fastJSON.JSON.Instance.ToJSON(graph, fp).Length) // Console.WriteLine("长度不一样,有错误! ----fastJSON"); //else // Console.WriteLine("和 fastJSON 长度正确"); Stopwatch w = Stopwatch.StartNew(); w.Start(); w = System.Diagnostics.Stopwatch.StartNew(); w.Start(); if (isString) { if (isInfo) { for (int i = 0; i < len; i++) { JsonString os = new JsonString(buffer); ShiboSerializer.Serialize(os, graph, info, sets); if (toString == false) { v += os.Position; } else { v += os.ToString().Length; } } } else { for (int i = 0; i < len; i++) { JsonString os = new JsonString(buffer); ShiboSerializer.Serialize(os, graph, sets); if (toString == false) { v += os.Position; } else { v += os.ToString().Length; } } } } else { for (int i = 0; i < len; i++) { //JsonStream os = new JsonStream(capacity); //ShiboSerializer.Serialize(os, graph, sets); //if (toString == false) // v += os.Position; //else // v += os.ToString().Length; } } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Fastest.Json serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = Newtonsoft.Json.JsonConvert.SerializeObject(graph, nset); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time JsonConvert serializer= " + w.ElapsedMilliseconds + " v:" + v); if (isPub) { v = 0; w.Restart(); for (int i = 0; i < len; i++) { msPub = new MemoryStream(); Serializer.Serialize(msPub, graph); v += (int)msPub.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Protocol Buffer serializer= " + w.ElapsedMilliseconds + " v:" + v); } v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = ServiceStack.Text.JsonSerializer.SerializeToString(graph); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ServiceStack serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { //StringBuilder sb = new StringBuilder(120); //sb.Append('['); //string[] strs = (string[])graph; //for (int n = 0; n < strs.Length; n++) //{ // sb.Append('"'); // if (strs[n] == null) // sb.Append("null"); // else if (strs[n].Length == 0) // sb.Append("\"\""); // else // sb.Append(strs[n]); // sb.Append('"'); // sb.Append(','); //} //sb.Length--; //sb.Append(']'); //v += sb.ToString().Length; v += graph.ToString().Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time StringBuilder serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { MemoryStream stream = new MemoryStream(); binserializer.Serialize(stream, graph); v += (int)stream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time BinaryFormatter serializer= " + w.ElapsedMilliseconds + " v:" + v); //v = 0; //w.Restart(); //for (int i = 0; i < len; i++) //{ // string st = serializer.Serialize(graph); // v += st.Length; //} //w.Stop(); //Console.WriteLine(); //Console.WriteLine("Time JavaScriptSerializer serializer= " + w.ElapsedMilliseconds + " v:" + v); v = 0; w.Restart(); for (int i = 0; i < len; i++) { string st = JsonSerializer(ser, graph); v += st.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time DataContractJsonSerializer serializer= " + w.ElapsedMilliseconds + " v:" + v); break; } }
//static byte[] socBuffer = new byte[64]; //static char[] cbuffer = new char[128]; //static int capacity = 256; //static int defaultSize = 500; //static bool isPub = false; //static bool isString = true; //static int testCount = 1000000; //static bool toString = true; //static bool isInfo = false; //static bool isConsole = false; //static SerializerSettings sets = new SerializerSettings(); #endregion public static TestResult Test(object graph) { TestResult result = new TestResult(); while (true) { #region 参数 int len = testCount; JsonStringContext info = ShiboSerializer.GetJsonStringTypeInfos(graph.GetType()); JsonSerializerSettings nset = new JsonSerializerSettings(); //nset.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; BinaryFormatter binserializer = new BinaryFormatter(); //var serializer = MessagePackSerializer.Create(graph.GetType()); MemoryStream msBin = new MemoryStream(); MemoryStream socStream = new MemoryStream(); MemoryStream msgStream = new MemoryStream(); MemoryStream protopufStream = new MemoryStream(); #endregion #region 启动 int size = 0; string s = string.Empty; ObjectStreamWriter socOStream = new ObjectStreamWriter(socStream); //ShiboSerializer.BinSerialize(os, graph, sets); //ShiboSerializer.Serialize(graph); //ShiboSerializer.BinSerialize(graph); if (TestBaseConfig.Fastest) { Console.WriteLine(ShiboSerializer.Serialize(graph)); } if (TestBaseConfig.Soc) { ShiboSerializer.BinarySerialize(graph); } //if (TestBaseConfig.MsgPack) // serializer.PackSingleObject(graph); if (TestBaseConfig.SocStream) { ShiboSerializer.BinarySerialize(socOStream, graph); } if (TestBaseConfig.Newtonsoft) { JsonConvert.SerializeObject(graph); } if (TestBaseConfig.BinaryFormatter) { binserializer.Serialize(msBin, graph); } if (TestBaseConfig.ServiceStack) { ServiceStack.Text.JsonSerializer.SerializeToString(graph); } //if (TestBaseConfig.JavaScriptSerializer) // serializer.Serialize(graph); //if (TestBaseConfig.DataContractJsonSerializer) // JsonSerializer(ser, graph); #endregion #region 测试 Stopwatch w = System.Diagnostics.Stopwatch.StartNew(); if (TestBaseConfig.Soc) { w.Restart(); for (int i = 0; i < len; i++) { //byte[] bytes = ShiboSerializer.BinSerialize(graph); //size += bytes.Length; ObjectWriter stream = new ObjectWriter(socBuffer); ShiboSerializer.BinarySerialize(stream, graph); size += stream.Position; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Soc Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.MsgPack) { size = 0; w.Restart(); //for (int i = 0; i < len; i++) //{ // byte[] bytes = serializer.PackSingleObject(graph); // size += bytes.Length; //} w.Stop(); Console.WriteLine(); Console.WriteLine("Time MsgPack Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.ProtoBuf) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { //protopufStream.Position = 0; protopufStream = new MemoryStream(100); Serializer.Serialize(protopufStream, graph); size += (int)protopufStream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ProtoBuf Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.Fastest) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { JsonString stream = new JsonString(jsonBuffer); ShiboSerializer.Serialize(stream, graph); size += stream.Position; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Fastest Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.Newtonsoft) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string json = JsonConvert.SerializeObject(graph); size += json.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time Newtonsoft Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.ServiceStack) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { string json = ServiceStack.Text.JsonSerializer.SerializeToString(graph); size += json.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time ServiceStack Serializer= " + w.ElapsedMilliseconds + " v:" + size); } if (TestBaseConfig.BinaryFormatter) { size = 0; w.Restart(); for (int i = 0; i < len; i++) { MemoryStream stream = new MemoryStream(); binserializer.Serialize(stream, graph); size += (int)stream.Length; } w.Stop(); Console.WriteLine(); Console.WriteLine("Time BinaryFormatter Serializer= " + w.ElapsedMilliseconds + " v:" + size); } #endregion break; } return(result); }