surging內部使用的是高性能RPC遠程服務調用,若是用json.net序列化確定性能上達不到最優,因此後面擴展了protobuf,messagepack序列化組件,以支持RPC二進制傳輸.git
在這裏須要感謝白紙無字Zonciu,新增了messagepack序列化,讓surging 性能上跨了一大步。此篇文章咱們來談談messagepack、protobuffer、json.net ,而且性能作下對比github
開源地址:https://github.com/dotnetcore/surgingjson
2.1 surging 使用的是如下序列化組件:網絡
json.net:surging 使用的是Newtonsoft.Json, 它是基於json格式的序列化和反序列化的組件.官方網站: http://json.codeplex.com/async
protobuf:surging 使用的是protobuf-net, 它是基於二進制格式的序列化和反序列化的組件.官方網站: https://github.com/mgravell/protobuf-netide
messagepack:surging 使用的是MessagePack-CSharp, 它是基於二進制格式的序列化和反序列化的組件.官方網站: https://github.com/neuecc/MessagePack-CSharp性能
2.2 各個組件的優勢測試
json.net 有如下優勢:大數據
侵入性:能夠不添加attribute,就能進行序列化操做網站
靈活性:能夠靈活性配置,好比容許被序列化的成員自定義名字,屏蔽的非序列化屬性成員
可讀性: 數據格式比較簡單, 易於讀寫
依賴性:能夠序列化成JObject,無需依賴對象進行序列化和泛型化。
protobuf 有如下優勢:
性能高 序列化後體積相比Json和XML很小,適合RPC二進制傳輸
跨語言:支持跨平臺多語言
兼容性:消息格式升級和兼容性還不錯
速度快 :序列化反序列化速度很快,快於Json的處理速速
messagepack有如下優勢:
性能高 序列化後體積相比Json和XML很小,適合RPC二進制傳輸
跨語言:支持跨平臺多語言
兼容性:消息格式升級和兼容性還不錯
速度快 :序列化反序列化速度很快,快於Json的處理速度
針對於protobuf和messagepack都是基於二進制格式的序列化和反序列化,優勢都同樣,可是基於messagepack的MessagePack-CSharp組件侵入性更小,能夠不須要加attribute,並且性能上更優.下一節來看看組件在surging 中的表現
服務端:
(注:若是不加UseProtoBufferCodec和UseMessagePackCodec就是json.net序列化)
var host = new ServiceHostBuilder() .RegisterServices(option=> { option.Initialize(); //初始化服務 option.RegisterServices();//依賴注入領域服務 option.RegisterRepositories();//依賴注入倉儲 option.RegisterModules();//依賴注入第三方模塊 option.RegisterServiceBus();//依賴注入ServiceBus }) .RegisterServices(builder => { builder.AddMicroService(option => { option.AddServiceRuntime();// // option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181")); //使用Zookeeper管理 option.UseConsulManager(new ConfigInfo("127.0.0.1:8500"));//使用Consul管理 option.UseDotNettyTransport();//使用Netty傳輸 option.UseRabbitMQTransport();//使用rabbitmq 傳輸 option.AddRabbitMQAdapt();//基於rabbitmq的消費的服務適配 // option.UseProtoBufferCodec();//基於protobuf序列化傳輸 option.UseMessagePackCodec();//基於MessagePack序列化傳輸 builder.Register(p => new CPlatformContainer(ServiceLocator.Current));//初始化注入容器 }); }) .SubscribeAt() //消息訂閱 .UseServer("127.0.0.1", 98) //.UseServer("127.0.0.1", 98,「true」) //自動生成Token //.UseServer("127.0.0.1", 98,「123456789」) //固定密碼Token .UseStartup<Startup>() .Build(); using (host.Run()) { Console.WriteLine($"服務端啓動成功,{DateTime.Now}。"); }
客戶端:
var host = new ServiceHostBuilder() .RegisterServices(option => { option.Initialize(); option.RegisterServices(); option.RegisterRepositories(); option.RegisterModules(); }) .RegisterServices(builder => { builder.AddMicroService(option => { option.AddClient(); option.AddClientIntercepted(typeof(CacheProviderInterceptor)); //option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181")); option.UseConsulManager(new ConfigInfo("127.0.0.1:8500")); option.UseDotNettyTransport(); option.UseRabbitMQTransport(); option.UseProtoBufferCodec(); //option.UseMessagePackCodec(); builder.Register(p => new CPlatformContainer(ServiceLocator.Current)); }); }) .UseClient() .UseStartup<Startup>() .Build(); using (host.Run()) { Startup.Test(ServiceLocator.GetService<IServiceProxyFactory>()); Startup.TestRabbitMq(); }
測試 0 object(注:測試無參數)
/// <summary> /// 測試 /// </summary> /// <param name="serviceProxyFactory"></param> public static void Test(IServiceProxyFactory serviceProxyFactory) { Task.Run(async () => { var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User"); await userProxy.GetUserId("user"); do { Console.WriteLine("正在循環 1w次調用 GetUser....."); //1w次調用 var watch = Stopwatch.StartNew(); for (var i = 0; i < 10000; i++) { var a =userProxy.GetDictionary().Result; } watch.Stop(); Console.WriteLine($"1w次調用結束,執行時間:{watch.ElapsedMilliseconds}ms"); Console.ReadLine(); } while (true); }).Wait(); }
測試 1 object(注:測試參數傳對象)
/// <summary> /// 測試 /// </summary> /// <param name="serviceProxyFactory"></param> public static void Test(IServiceProxyFactory serviceProxyFactory) { Task.Run(async () => { var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User"); await userProxy.GetUserId("user"); do { Console.WriteLine("正在循環 1w次調用 GetUser....."); //1w次調用 var watch = Stopwatch.StartNew(); for (var i = 0; i < 10000; i++) { var a =userProxy.GetUser(new UserModel { UserId = 1 }).Result; } watch.Stop(); Console.WriteLine($"1w次調用結束,執行時間:{watch.ElapsedMilliseconds}ms"); Console.ReadLine(); } while (true); }).Wait(); }
測試 10 object(注:測試參數傳List 集合對象)
/// <summary> /// 測試 /// </summary> /// <param name="serviceProxyFactory"></param> public static void Test(IServiceProxyFactory serviceProxyFactory) { Task.Run(async () => { var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User"); await userProxy.GetUserId("user"); var list = new List<UserModel>(); for(int i=0;i<10;i++) { list.Add(new UserModel { UserId = 1, Age = 18, Name = "fanly" }); } do { Console.WriteLine("正在循環 1w次調用 GetUser....."); //1w次調用 var watch = Stopwatch.StartNew(); for (var i = 0; i < 10000; i++) { var a =userProxy.Get(list).Result; } watch.Stop(); Console.WriteLine($"1w次調用結束,執行時間:{watch.ElapsedMilliseconds}ms"); Console.ReadLine(); } while (true); }).Wait(); }
測試100 object(注:測試參數傳List 集合對象)
/// <summary> /// 測試 /// </summary> /// <param name="serviceProxyFactory"></param> public static void Test(IServiceProxyFactory serviceProxyFactory) { Task.Run(async () => { var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User"); await userProxy.GetUserId("user"); var list = new List<UserModel>(); for(int i=0;i<100;i++) { list.Add(new UserModel { UserId = 1, Age = 18, Name = "fanly" }); } do { Console.WriteLine("正在循環 1w次調用 GetUser....."); //1w次調用 var watch = Stopwatch.StartNew(); for (var i = 0; i < 10000; i++) { var a =userProxy.Get(list).Result; } watch.Stop(); Console.WriteLine($"1w次調用結束,執行時間:{watch.ElapsedMilliseconds}ms"); Console.ReadLine(); } while (true); }).Wait(); }
經過以上測試代碼,咱們獲得了以下的測試結果
經過上圖,能夠發現messagepack無論是小數據量仍是大數據量都保持比較穩定的性能,而json.net 在100object平均已經達到了1.1ms,和messagepack、protobuffer比差太多,而 protobuffer在這次測試中表現的極其不穩定只有在1 object 和100 object 性能比較不錯,可是與messagepack比仍是相差比較大。因此我建議仍是使用messagepack,性能上更優,侵入性也很是低
咱們來看看性能最優的messagepack 詳細測試數據
o object:
1 object:
10 object:
100 object
測試環境
CPU:Intel Core i7-4710MQ
內存:16G
硬盤:1T SSD+512G HDD
網絡:局域網
surging 已經完成JWT驗證和AppSecret驗證,下篇文章會詳細介紹surging 身份認證,如感興趣請多關注或者加入QQ羣:542283494