public static void TestCase1() { //Guid guid = new Guid("e92b8e30-a6e5-41f6-a6b9-188230a23dd2"); //byte[] buffer = guid.ToByteArray(); //Console.WriteLine(BitConverter.ToString(buffer)); Dictionary <int, string> v = new Dictionary <int, string>(); v.Add(1, null); TestBaseConfig.Seed = 1; Int32Class a = ShiboSerializer.Initialize <Int32Class>();// Int32Class.Init(); string s1 = ShiboSerializer.Serialize(a); string s2 = null; byte[] buffer = ShiboSerializer.BinarySerialize(a); a = ShiboSerializer.BinaryDeserialize <Int32Class>(buffer); s2 = ShiboSerializer.Serialize(a); Console.WriteLine(BitConverter.ToString(buffer)); Console.WriteLine(s1 == s2); //Console.ReadLine(); Test(a); //Int32FieldClass a = Int32FieldClass.Init(); //byte[] buffer = ShiboSerializer.BinSerialize(a); ////byte[] b = new byte[400]; ////Buffer.BlockCopy(buffer, 0, b, 0, 4); //a = ShiboSerializer.BinDeserialize<Int32FieldClass>(buffer); //Console.WriteLine(BitConverter.ToString(buffer)); //Console.WriteLine(ShiboSerializer.Serialize(a)); Console.ReadLine(); }
public static void TestCase14() { TestBaseConfig.Seed = 1; Int32Class a = ShiboSerializer.Initialize <Int32Class>(53456158); byte[] bytes = ShiboSerializer.BinarySerialize(a); Int32Class b = ShiboSerializer.BinaryDeserialize <Int32Class>(bytes); //Console.WriteLine(ShiboComparer.Compare<List<string>>(a, b)); Console.WriteLine(JsonConvert.SerializeObject(a) == JsonConvert.SerializeObject(b)); ObjectWriter bf = new ObjectWriter(50); Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { //bytes = ShiboSerializer.BinarySerialize(a); //b = ShiboSerializer.BinaryDeserialize<List<string>>(bytes); bf.Reset(); ShiboSerializer.BinarySerialize(bf, a); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds); //Console.WriteLine(JsonConvert.SerializeObject(b.Count)); }
public static void MultipleTypeMethodTest() { PrimitiveTypeClass v = ShiboSerializer.Initialize <PrimitiveTypeClass>(4578424); string json = ShiboSerializer.Serialize(v); byte[] bytes = ShiboSerializer.BinarySerialize(v); Console.WriteLine(json == JsonConvert.SerializeObject(v)); v = ShiboSerializer.BinaryDeserialize <PrimitiveTypeClass>(bytes); Console.WriteLine(json == JsonConvert.SerializeObject(v)); }
public static void TestCase6() { TestBaseConfig.Seed = 1; ArraySegmentClass a = new ArraySegmentClass(); byte[] buffer = ShiboSerializer.BinarySerialize(a); Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { buffer = ShiboSerializer.BinarySerialize(a); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds); Console.WriteLine(buffer); Console.WriteLine(JsonConvert.SerializeObject(a)); }
public static void TestCase4() { TestBaseConfig.Seed = 1; MixClass a = MixClass.Init(); byte[] buffer = ShiboSerializer.BinarySerialize(a); MixClass b = ShiboSerializer.BinaryDeserialize <MixClass>(buffer); Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { buffer = ShiboSerializer.BinarySerialize(a); //b = ShiboSerializer.BinDeserialize<MixClass>(buffer); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds); Console.WriteLine(JsonConvert.SerializeObject(a) == JsonConvert.SerializeObject(b)); }
public static void TestCase13() { TestBaseConfig.Seed = 1; byte[][] a = ShiboSerializer.Initialize <byte[][]>(53456158); byte[] bytes = ShiboSerializer.BinarySerialize(a); byte[][] b = ShiboSerializer.BinaryDeserialize <byte[][]>(bytes); //Console.WriteLine(ShiboComparer.Compare<List<string>>(a, b)); Console.WriteLine(JsonConvert.SerializeObject(a) == JsonConvert.SerializeObject(b)); //Stopwatch w = Stopwatch.StartNew(); //for (int i = 0; i < 10000; i++) //{ // //bytes = ShiboSerializer.BinarySerialize(a); // b = ShiboSerializer.BinaryDeserialize<List<string>>(bytes); //} //w.Stop(); //Console.WriteLine(w.ElapsedMilliseconds); //Console.WriteLine(JsonConvert.SerializeObject(b.Count)); }
/// <summary> /// /// </summary> public static void TestCase10() { TestBaseConfig.Seed = 1; Dictionary <int, bool> a = ShiboSerializer.Initialize <Dictionary <int, bool> >(53456158); byte[] bytes = ShiboSerializer.BinarySerialize(a); Dictionary <int, bool> b = ShiboSerializer.BinaryDeserialize <Dictionary <int, bool> >(bytes); Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 10000; i++) { //bytes = ShiboSerializer.BinarySerialize(a); b = ShiboSerializer.BinaryDeserialize <Dictionary <int, bool> >(bytes); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds); Console.WriteLine(JsonConvert.SerializeObject(b.Count)); }
public static void TestCase9() { TestBaseConfig.Seed = 1; int[] ints = new int[1000]; //new FastRandom().NextInts(ints, 0, ints.Length); byte[] bytes = ShiboSerializer.BinarySerialize(ints); int[] b = ShiboSerializer.BinaryDeserialize <int[]>(bytes); //int ints = 45456; //byte[] bytes = ShiboSerializer.BinarySerialize(ints); //int b = ShiboSerializer.BinaryDeserialize<int>(bytes); Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 1000; i++) { bytes = ShiboSerializer.BinarySerialize(ints); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds); Console.WriteLine(JsonConvert.SerializeObject(b)); }
public static void TestCase12() { TestBaseConfig.Seed = 1; IList <string> a = null; byte[] bytes = ShiboSerializer.BinarySerialize(a); //bytes = ShiboCompression.Compress(bytes); //ShiboDecompress de = new ShiboDecompress(); //bytes = de.Decompress(bytes,0); //string[] b = ShiboSerializer.BinaryDeserialize<string[]>(bytes); ////Console.WriteLine(ShiboComparer.Compare<List<string>>(a, b)); //Console.WriteLine(JsonConvert.SerializeObject(a) == JsonConvert.SerializeObject(b)); //Stopwatch w = Stopwatch.StartNew(); //for (int i = 0; i < 10000; i++) //{ // //bytes = ShiboSerializer.BinarySerialize(a); // b = ShiboSerializer.BinaryDeserialize<List<string>>(bytes); //} //w.Stop(); //Console.WriteLine(w.ElapsedMilliseconds); //Console.WriteLine(JsonConvert.SerializeObject(b.Count)); }
//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); }