lith há 4 anos atrás
pai
commit
3affa9eb18

+ 10 - 7
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/RpcContextData.cs

@@ -18,18 +18,20 @@ namespace Sers.Core.Module.Rpc
 
         static RpcContextData()
         {
+            // 可不指定。RpcData序列化模式。默认为Text。
+            // 可为 Newtonsoft、Text、MessagePack、BytePointor。
+            // 效率依次递增。MessagePack 和 BytePointor直接序列化为二进制数据而不是json字符串
             string rpcDataSerializeMode = ConfigurationManager.Instance.GetByPath<string>("Sers.RpcDataSerializeMode");
 
 
             switch (rpcDataSerializeMode) 
             {
-                case "BytePointor": Serialization = BytePointor_RpcContextData.Instance; break;
-
-
-                //case "Newtonsoft": Serialization = Vit.Core.Module.Serialization.Serialization_Newtonsoft.Instance; break;
-                case "MessagePack": Serialization = MessagePack_RpcContextData.Instance; break;
-                case "StringBuilder": Serialization = StringBuilder_RpcContextData.Instance; break;
+                case "Newtonsoft": Serialization = Newtonsoft_RpcContextData.Instance; break;
                 case "Text": Serialization = Text_RpcContextData.Instance; break;
+                case "MessagePack": Serialization = MessagePack_RpcContextData.Instance; break;
+                case "BytePointor": Serialization = BytePointor_RpcContextData.Instance; break;              
+            
+                //case "StringBuilder": Serialization = StringBuilder_RpcContextData.Instance; break;              
                 default: Serialization = Text_RpcContextData.Instance; break;
             }
         }
@@ -184,9 +186,10 @@ namespace Sers.Core.Module.Rpc
         #endregion
 
 
+ 
         public object error;
 
-        [MessagePack.MessagePackFormatter(typeof(Vit.Core.Module.Serialization.MessagePack_Newtonsoft_Object))]
+        //[MessagePack.MessagePackFormatter(typeof(Vit.Core.Module.Serialization.MessagePack_Newtonsoft_Object))]
         public object user;
 
 

+ 111 - 29
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Fast/BytePointor_RpcContextData.cs

@@ -1,7 +1,10 @@
 
+using Newtonsoft.Json.Linq;
 using System;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System.Text;
+using Vit.Core.Module.Serialization;
 using Vit.Extensions;
 
 namespace Sers.Core.Module.Rpc.Serialization.Fast
@@ -16,12 +19,15 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
 
 
 
+        private static ISerialization Serialization = Serialization_Text.Instance;
+
+
         #region SerializeToBytes  
 
         [ThreadStatic]
         static byte[] fileContent;
+ 
 
-  
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public unsafe byte[] SerializeToBytes(RpcContextData data)
@@ -29,30 +35,31 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
             if (fileContent == null) fileContent = new byte[102400];
             int t = 0;
             fixed (byte* bytes = fileContent)
-            {
+            { 
+
                 //(x.1)route
                 if (data.route != null)
                 {
-                    bytes[t++] = (byte)ERpcKey.route;
-                    bytes[t++] = (byte)data.route.Length;
-                    t += StringToBytes(data.route, bytes + t);
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.route, data.route);               
                 }
 
                 #region (x.2)caller             
                 //(x.x.1)caller_rid
                 if (data.caller.rid != null)
                 {
-                    bytes[t++] = (byte)ERpcKey.caller_rid;
-                    bytes[t++] = (byte)data.caller.rid.Length;
-                    t += StringToBytes(data.caller.rid, bytes + t);
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.caller_rid, data.caller.rid);
+                }
+
+                //(x.x.2)caller_callStack
+                if (data.caller.callStack != null)
+                {
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.caller_callStack, Serialization.SerializeToString(data.caller.callStack));
                 }
 
-                //(x.x.2)caller_source
+                //(x.x.3)caller_source
                 if (data.caller.source != null)
                 {
-                    bytes[t++] = (byte)ERpcKey.caller_source;
-                    bytes[t++] = (byte)data.caller.source.Length;
-                    t += StringToBytes(data.caller.source, bytes + t);
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.caller_source, data.caller.source);
                 }
                 #endregion
 
@@ -61,20 +68,51 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
                 //(x.x.1)http_url
                 if (data.http.url != null)
                 {
-                    bytes[t++] = (byte)ERpcKey.http_url;
-                    bytes[t++] = (byte)data.http.url.Length;
-                    t += StringToBytes(data.http.url, bytes + t);
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.http_url, data.http.url);
                 }
 
                 //(x.x.2)http_method
                 if (data.http.method != null)
                 {
-                    bytes[t++] = (byte)ERpcKey.http_method;
-                    bytes[t++] = (byte)data.http.method.Length;
-                    t += StringToBytes(data.http.method, bytes + t);
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.http_method, data.http.method);
+                }
+
+                //(x.x.3)http_statusCode
+                if (data.http.statusCode != null)
+                {
+                    bytes[t] = (byte)ERpcPropertyName.http_statusCode;
+                    ((short*)(bytes + t + 1))[0] = (short)4;
+                    ((int*)(bytes + t + 3))[0] = data.http.statusCode.Value;
+                    t += 7;
+                }
+
+                //(x.x.4)http_protocol
+                if (data.http.protocol != null)
+                {
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.http_protocol, data.http.protocol);
+                }
+
+                //(x.x.5)http_headers
+                if (data.http.headers != null)
+                {
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.http_headers, Serialization.SerializeToString(data.http.headers));
+                }
+
+                #endregion
+
+
+                //(x.4)error
+                if (data.error != null)
+                {
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.error, Serialization.SerializeToString(data.error));
                 }
-                #endregion     
-            
+
+                //(x.5)user
+                if (data.user != null)
+                {
+                    AppendStringValue(bytes, ref t, ERpcPropertyName.user, Serialization.SerializeToString(data.user));
+                }
+
             }
 
             return fileContent.Clone(0, t);
@@ -85,6 +123,23 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
         #endregion
 
 
+        #region AppendStringValue
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        unsafe void AppendStringValue(byte* bytes, ref int t, ERpcPropertyName property, string value)
+        {
+            bytes[t] = (byte)property;
+
+            int valueByteCount = StringToBytes(value, bytes + t + 3);
+
+            ((short*)(bytes + t + 1))[0] = (short)valueByteCount;
+
+            t += valueByteCount + 3;
+        }
+
+        #endregion
+
+
+
 
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -95,31 +150,58 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
             var result = new RpcContextData();
 
             int t = 0;
-            ERpcKey rpcKey;
+            ERpcPropertyName rpcKey;
             fixed (byte* bytes = data.AsSpan())
             {
                 while (t < data.Count)
                 {
-                    rpcKey = (ERpcKey)bytes[t++];
-                    int len = (int)bytes[t++];
-
+                    rpcKey = (ERpcPropertyName)bytes[t];
+                    short len = ((short*)(bytes+t+1))[0];
+                    t += 3;
                     switch (rpcKey)
                     {
-                        case ERpcKey.route:
+                        //(x.1)
+                        case ERpcPropertyName.route:
                             result.route = BytesToString(bytes + t, len);
                             break;
-                        case ERpcKey.caller_rid:
+
+                        //(x.2)
+                        case ERpcPropertyName.caller_rid:
                             result.caller.rid = BytesToString(bytes + t, len);
+                            break;                     
+                        case ERpcPropertyName.caller_callStack:
+                            result.caller.callStack = Serialization.DeserializeFromArraySegmentByte<List<string>>(data.Slice(t, len));
                             break;
-                        case ERpcKey.caller_source:
+                        case ERpcPropertyName.caller_source:
                             result.caller.source = BytesToString(bytes + t, len);
                             break;
-                        case ERpcKey.http_url:
+
+                        //(x.3)
+                        case ERpcPropertyName.http_url:
                             result.http.url = BytesToString(bytes + t, len);
                             break;
-                        case ERpcKey.http_method:
+                        case ERpcPropertyName.http_method:
                             result.http.method = BytesToString(bytes + t, len);
                             break;
+                        case ERpcPropertyName.http_statusCode:
+                            result.http.statusCode = ((int*)(bytes + t))[0];
+                            break;
+                        case ERpcPropertyName.http_protocol:
+                            result.http.protocol = BytesToString(bytes + t, len);
+                            break;
+                        case ERpcPropertyName.http_headers:
+                            result.http.headers = Serialization.DeserializeFromArraySegmentByte<Dictionary<string, string>>(data.Slice(t, len));
+                            break;
+
+                        //(x.4)
+                        case ERpcPropertyName.error:
+                            result.error = Serialization.DeserializeFromArraySegmentByte<JObject>(data.Slice(t, len));
+                            break;
+
+                        //(x.5)
+                        case ERpcPropertyName.user:
+                            result.user = Serialization.DeserializeFromArraySegmentByte<JObject>(data.Slice(t, len));
+                            break;
                     }
                     t += len;
                 }

+ 5 - 5
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Fast/Bytes_RpcContextData.cs

@@ -26,18 +26,18 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
             int t = 0;
 
             //(x.1)route
-            fileContent[t++] = (byte)ERpcKey.route;
+            fileContent[t++] = (byte)ERpcPropertyName.route;
             fileContent[t++] = (byte)data.route.Length;
             t += StringToByte(data.route, fileContent.AsSpan(t));
 
             #region (x.2)caller             
             //(x.x.1)caller_rid
-            fileContent[t++] = (byte)ERpcKey.caller_rid;
+            fileContent[t++] = (byte)ERpcPropertyName.caller_rid;
             fileContent[t++] = (byte)data.caller.rid.Length;
             t += StringToByte(data.caller.rid, fileContent.AsSpan(t));
 
             //(x.x.2)caller_source
-            fileContent[t++] = (byte)ERpcKey.caller_source;
+            fileContent[t++] = (byte)ERpcPropertyName.caller_source;
             fileContent[t++] = (byte)data.caller.source.Length;
             t += StringToByte(data.caller.source, fileContent.AsSpan(t));
 
@@ -46,12 +46,12 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
 
             #region (x.3)http             
             //(x.x.1)http_url
-            fileContent[t++] = (byte)ERpcKey.http_url;
+            fileContent[t++] = (byte)ERpcPropertyName.http_url;
             fileContent[t++] = (byte)data.http.url.Length;
             t += StringToByte(data.http.url, fileContent.AsSpan(t));
 
             //(x.x.2)http_method
-            fileContent[t++] = (byte)ERpcKey.http_method;
+            fileContent[t++] = (byte)ERpcPropertyName.http_method;
             fileContent[t++] = (byte)data.http.method.Length;
             t += StringToByte(data.http.method, fileContent.AsSpan(t));
 

+ 0 - 17
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Fast/ERpcKey.cs

@@ -1,17 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace Sers.Core.Module.Rpc.Serialization.Fast
-{
-    enum ERpcKey : byte
-    {
-        route,
-
-        caller_rid,
-        caller_source,
-
-        http_url,
-        http_method,
-    }
-}

+ 24 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Fast/ERpcPropertyName.cs

@@ -0,0 +1,24 @@
+
+
+namespace Sers.Core.Module.Rpc.Serialization.Fast
+{
+
+    enum ERpcPropertyName : byte
+    {
+        route = 10,
+
+        caller_rid = 20,
+        caller_callStack =21,
+        caller_source=22,
+
+        http_url = 30,
+        http_method = 31,
+        http_statusCode = 32,
+        http_protocol = 33,
+        http_headers = 34,
+
+
+        error = 40,
+        user = 50,
+    }
+}

+ 5 - 5
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Fast/MemoryStream_RpcContextData.cs

@@ -31,7 +31,7 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
                 byte[] bytes;
 
                 //(x.1)route
-                stream.WriteByte((byte)ERpcKey.route);
+                stream.WriteByte((byte)ERpcPropertyName.route);
                 stream.WriteByte((byte)data.route.Length);
                 bytes = Encoding.UTF8.GetBytes(data.route);
                 stream.Write(bytes, 0, bytes.Length);
@@ -39,13 +39,13 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
 
                 #region (x.2)caller             
                 //(x.x.1)caller_rid
-                stream.WriteByte((byte)ERpcKey.caller_rid);
+                stream.WriteByte((byte)ERpcPropertyName.caller_rid);
                 stream.WriteByte((byte)data.caller.rid.Length);
                 bytes = Encoding.UTF8.GetBytes(data.caller.rid);
                 stream.Write(bytes, 0, bytes.Length);
 
                 //(x.x.2)caller_source
-                stream.WriteByte((byte)ERpcKey.caller_source);
+                stream.WriteByte((byte)ERpcPropertyName.caller_source);
                 stream.WriteByte((byte)data.caller.source.Length);
                 bytes = Encoding.UTF8.GetBytes(data.caller.source);
                 stream.Write(bytes, 0, bytes.Length);
@@ -54,13 +54,13 @@ namespace Sers.Core.Module.Rpc.Serialization.Fast
 
                 #region (x.3)http             
                 //(x.x.1)http_url
-                stream.WriteByte((byte)ERpcKey.http_url);
+                stream.WriteByte((byte)ERpcPropertyName.http_url);
                 stream.WriteByte((byte)data.http.url.Length);
                 bytes = Encoding.UTF8.GetBytes(data.http.url);
                 stream.Write(bytes, 0, bytes.Length);
 
                 //(x.x.2)http_method
-                stream.WriteByte((byte)ERpcKey.http_method);
+                stream.WriteByte((byte)ERpcPropertyName.http_method);
                 stream.WriteByte((byte)data.http.method.Length);
                 bytes = Encoding.UTF8.GetBytes(data.http.method);
                 stream.Write(bytes, 0, bytes.Length);

+ 30 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/Newtonsoft_RpcContextData.cs

@@ -0,0 +1,30 @@
+using System;
+using System.Runtime.CompilerServices;
+using Vit.Core.Module.Serialization;
+
+namespace Sers.Core.Module.Rpc.Serialization
+{
+    public class Newtonsoft_RpcContextData : IRpcSerialize
+    {
+
+        public static readonly Newtonsoft_RpcContextData Instance = new Newtonsoft_RpcContextData();
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public byte[] SerializeToBytes(RpcContextData data) 
+        {
+            return Serialization_Newtonsoft.Instance.SerializeToBytes(data);
+        }
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public RpcContextData DeserializeFromBytes(ArraySegment<byte> data)
+        {
+            return Serialization_Newtonsoft.Instance.DeserializeFromArraySegmentByte<RpcContextData>(data);
+        }
+
+
+
+ 
+    }
+}

+ 2 - 1
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/StringBuilder_RpcContextData.cs

@@ -21,7 +21,8 @@ namespace Sers.Core.Module.Rpc.Serialization
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public RpcContextData DeserializeFromBytes(ArraySegment<byte> data)
         {
-            throw new NotImplementedException();
+            //throw new NotImplementedException();
+            return Text_RpcContextData.Instance.DeserializeFromBytes(data);
         }
 
 

+ 71 - 7
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.Module.Serialization.Qps/Program.cs

@@ -98,8 +98,8 @@ namespace App
                             //序列化         130 万qps
                             //反序列化       103 万qps  
                             //序列化+序列化   50 万qps
-                            bytes = Text_RpcContextData.Instance.SerializeToBytes(demo_data);
-                            data2 = Text_RpcContextData.Instance.DeserializeFromBytes(bytes);
+                            //bytes = Text_RpcContextData.Instance.SerializeToBytes(demo_data);
+                            //data2 = Text_RpcContextData.Instance.DeserializeFromBytes(bytes);
 
 
                             //(x.3)StringBuilder                       
@@ -116,9 +116,9 @@ namespace App
                             //vr 1线程 
                             //序列化         480 万qps
                             //反序列化           万qps  
-                            //序列化+序列化  196 万qps
-                            //bytes = BytePointor_RpcContextData.Instance.SerializeToBytes(demo_data);                  
-                            //data2 = BytePointor_RpcContextData.Instance.DeserializeFromBytes(bytes.BytesToArraySegmentByte());
+                            //序列化+序列化  180 万qps
+                            bytes = BytePointor_RpcContextData.Instance.SerializeToBytes(demo_data);
+                            //data2 = BytePointor_RpcContextData.Instance.DeserializeFromBytes(bytes);
 
                         }
 
@@ -134,7 +134,73 @@ namespace App
         }
 
 
+
         public static void StartThread2()
+        {
+            QpsData qpsInfo = new QpsData(statisticsQps);
+            Task.Run(() =>
+            {
+                
+ 
+                var Instance = Vit.Core.Module.Serialization.Serialization_Text.Instance;
+
+                byte[] bytes;
+                JObject jo;
+                string str;
+                RpcContextData data, data2;
+
+
+                data = new RpcContextData();
+
+                data.route = "/a";
+
+                data.http.url = "http://sers.internal/a";
+                data.http.method = "GET";
+                data.http.statusCode = 400;
+                data.http.protocol = "HTTP/2.0";
+                data.http.Headers()["Content-Type"] = "application/javascript";
+
+                data.caller.rid = "8320becee0d945e9ab93de6fdac7627a";
+                data.caller.callStack = new List<string>() { "asdgsgsagddsg" };
+                data.caller.source = "Internal";
+
+                jo = new JObject() { ["userInfo11"] = 11.545, ["userInfo12"] = 123456789012456 };
+                jo["userInfo13"] = new JObject() { ["userInfo131"] = "131", ["userInfo132"] = "132" };
+                jo["userInfo14"] = null;
+                jo["userInfo15"] = new JArray { 1, true, 12.5, null, DateTime.Now };
+                data.user = jo;
+
+
+                IRpcSerialize RpcSerialize = BytePointor_RpcContextData.Instance;
+
+                while (true)
+                {
+                    try
+                    {                      
+
+                        for (var t = 0; t < 10000; t++)
+                        {           
+
+                            bytes = RpcSerialize.SerializeToBytes(data);
+                            //str = bytes.BytesToString();
+                            data2 = RpcSerialize.DeserializeFromBytes(bytes);
+                        }
+
+                        qpsInfo.RequestCount++;
+                    }
+                    catch (Exception ex)
+                    {
+                    }
+                }
+
+            });
+
+        }
+
+
+
+
+        public static void StartThread3()
         {
             QpsData qpsInfo = new QpsData(statisticsQps);
             Task.Run(() =>
@@ -225,8 +291,6 @@ namespace App
             });
 
         }
-         
-
 
 
     }