private void TestValueSeralize(ValueType value) { byte[] bs = BitSerializer.Seralize(value); Debug.WriteLine(value + "序列化后字节数" + bs.Length); object newvalue = BitSerializer.Deseralize(value.GetType(), bs); Assert.AreEqual(value, newvalue); }
private void TestStringSeralize(string value) { byte[] bs = BitSerializer.Seralize(value); object obj = BitSerializer.Deseralize(value.GetType(), bs); string newvalue = obj as string; Assert.AreEqual(value, newvalue); }
private void TestList(IList list) { byte[] bs = BitSerializer.Seralize(list); IList newlist = BitSerializer.Deseralize(list.GetType(), bs) as IList; for (int i = 0; i < list.Count; i++) { Assert.AreEqual(list[i], newlist[i]); } }
private void TestDictionary(IDictionary dic) { byte[] bs = BitSerializer.Seralize(dic); Dictionary <int, string> obj = BitSerializer.Deseralize(dic.GetType(), bs) as Dictionary <int, string>; Assert.AreEqual(obj.Count, dic.Count); foreach (KeyValuePair <int, string> pair in obj) { Assert.AreEqual(dic[pair.Key], pair.Value); } }
private void TestArray(Array array) { byte[] bs = BitSerializer.Seralize(array); object obj = BitSerializer.Deseralize(array.GetType(), bs); Array newarray = obj as Array; Assert.AreEqual(array.Length, newarray.Length); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(array.GetValue(i), newarray.GetValue(i)); } }
public void TestSeralize() { Class1 class1 = new Class1(); class1.ArrayValue = new int[] { 1, 2, 3, 4, 5 }; class1.Class2.s = "classsssssss"; // class1.Dictionary[1] = class1; class1.b = true; class1.i = 12312; class1.f = 5783f; class1.d = 231f; class1.Dictionary[1] = "dsasdasd"; class1.Dictionary[2] = "ddscccxz"; // class1.listObject.Add(class1); class1.listValue.Add(132); // class1.obj = class1; class1.s = "class111111"; byte[] bytes = BitSerializer.Seralize(class1); Class1 class11 = BitSerializer.Deseralize(typeof(Class1), bytes) as Class1; Assert.IsTrue(class1.Equals(class11)); }
public void TestSeralizePerformance() { int dataCount = 1000; int iteration = 1000; Class1 class1 = new Class1(); class1.ArrayValue = new int[] { 1, 2, 3, 4, 5 }; class1.Class2.s = "classsedwqdsavdddddddddddddddddsdasdasddddddddddddddddd" + "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + "sdasddddddddddddddddd" + "asddddddddddddddd" + "dasddddddddddddddddddddddxcvxcvxcvxcv" + "cxvcxxxxxxxxxxxxxxxxxxxxxxxxczxcsadasdasdasdssssss"; class1.b = true; class1.i = 12312; for (int i = 0; i < dataCount; i++) { class1.listValue.Add(i); } /* class1.Dictionary[1] = "dsasdasd"; * class1.Dictionary[2] = "ddscccxz";*/ class1.s = "192.168.1.37"; Iterations iterations = new Iterations(); for (int i = 0; i < iteration; i++) { iterations.class1s.Add(class1); } #region 序列化反序列化性能 Stopwatch watch = new Stopwatch(); byte[] bytes = null; Iterations iterations1; byte[] CompBbytes; { watch.Restart(); bytes = BitSerializer.Seralize(iterations); watch.Stop(); UnityEngine.Debug.Log("Bit seralizeTime:" + watch.ElapsedMilliseconds); // string s = ProtocolSerialize.DebugSeralizeStr(bytes); watch.Restart(); iterations1 = BitSerializer.Deseralize(typeof(Iterations), bytes) as Iterations; watch.Stop(); UnityEngine.Debug.Log("Bit DeseralizeTime:" + watch.ElapsedMilliseconds); #region 大小 UnityEngine.Debug.Log("Protocol序列化:" + bytes.Length); CompBbytes = CompressBase.CompressBytes(bytes); UnityEngine.Debug.Log("压缩后:" + CompBbytes.Length); #endregion } { watch.Restart(); ExtendProtocolSerialize.Enable(); bytes = ProtocolSerialize.Seralize(iterations); watch.Stop(); UnityEngine.Debug.Log("seralizeTime:" + watch.ElapsedMilliseconds); // string s = ProtocolSerialize.DebugSeralizeStr(bytes); watch.Restart(); iterations1 = ProtocolSerialize.Deseralize(typeof(Iterations), bytes) as Iterations; watch.Stop(); UnityEngine.Debug.Log("DeseralizeTime:" + watch.ElapsedMilliseconds); #region 大小 UnityEngine.Debug.Log("Protocol序列化:" + bytes.Length); CompBbytes = CompressBase.CompressBytes(bytes); UnityEngine.Debug.Log("压缩后:" + CompBbytes.Length); #endregion } { watch.Restart(); var json = UnityEngine.JsonUtility.ToJson(iterations); watch.Stop(); UnityEngine.Debug.Log("json seralizeTime:" + watch.ElapsedMilliseconds); // string s = ProtocolSerialize.DebugSeralizeStr(bytes); watch.Restart(); iterations1 = UnityEngine.JsonUtility.FromJson <Iterations>(json); watch.Stop(); UnityEngine.Debug.Log("json DeseralizeTime:" + watch.ElapsedMilliseconds); } #endregion }