溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

.net core怎樣在網(wǎng)絡(luò)高并發(fā)下提高JSON的處理效率

發(fā)布時間:2021-02-05 11:21:36 來源:億速云 閱讀:304 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了.net core怎樣在網(wǎng)絡(luò)高并發(fā)下提高JSON的處理效率,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

現(xiàn)有的webapi一般都基于JSON的格式來處理數(shù)據(jù),由于JSON是一個文本類的序列化協(xié)議所以在性能上自然就相對低效一些。在.net中常用Newtonsoft.Json是最常用的組件,由于提供簡便基于完整的json的String方法使用起來非常方便;但也正是這原因?qū)е翹ewtonsoft.Json在性能上一直被說慢,雖然Newtonsoft.Json提供Stream的方式來處理JSON不過想復(fù)用writer和reader還是需要一些應(yīng)用技巧。如果需要在網(wǎng)絡(luò)通訊中應(yīng)用JSON,那在這里介紹一下SpanJson這個組件,并通過一些測試來講述如何使用它。

SpanJson介紹

SpanJson是一個性能相對不錯的JSON組件,組件直接提供了byte[]和stream兩種操作方式,而這兩種方式非常適合在構(gòu)建自有網(wǎng)絡(luò)通訊上使用。通過這些基礎(chǔ)的字節(jié)和流結(jié)構(gòu)來處理可以相對降低一個大string的開銷。不過這個組件的熱度并不高,完善成度暫還不如Newtonsoft.Json,不過asp.net core 在FrameworkBenchmarks測試上已經(jīng)引入??梢試L試一下使用,組件開源地址: https://github.com/Tornhoof/SpanJson (本地下載)

性能測試

組件提供的方法相對比較少,從設(shè)計上來說更多是針對通訊方面的支持。基于Stream的序列化可以直接掛載在NetStream上,這樣可以節(jié)省數(shù)據(jù)復(fù)制帶來的開銷。不過反序列化不能直接在有混合數(shù)據(jù)的Stream上進(jìn)行,這或多或少有些可惜。從issues的解答來看作者也不太愿意在混合數(shù)據(jù)流上進(jìn)行調(diào)整。接下來針對bytes和Stream使用進(jìn)行一個性能測試,而Stream則采用一個可復(fù)用池的設(shè)計

MemoryStream 池的設(shè)計

public class MemoryStreamPool
 {

  private static System.Collections.Concurrent.ConcurrentStack<JsonMemoryStream> mPool = new System.Collections.Concurrent.ConcurrentStack<JsonMemoryStream>();

  public static Stream Pop()
  {
   if (!mPool.TryPop(out JsonMemoryStream result))
   {
    result = new JsonMemoryStream(1024 * 32);
   }
   return result;
  }


  public class JsonMemoryStream : MemoryStream
  {
   public JsonMemoryStream(int size) : base(size) { }

   protected override void Dispose(bool disposing)
   {
    MemoryStreamPool.Push(this);
   }

  }

  private static void Push(JsonMemoryStream stream)
  {
   stream.Position = 0;
   stream.SetLength(0);
   mPool.Push(stream);
  }
 }

測試內(nèi)容

測試的方式主要針對一個簡單的對象和一個對象列表,然后在不同線程下bytes和Stream pool這兩種方式的性能差別;壓測的線程數(shù)據(jù)分別是1,2,4,8,16,24,32,每次測試執(zhí)行的總數(shù)是100萬次,然后統(tǒng)計出執(zhí)行需要的時間和并發(fā)量。 測試代碼:

public class Bytes_JSON : BeetleX.Benchmark.BenchmarkBase
 {
  protected override void OnTest()
  {
   while (Increment())
   {
    var data = SpanJson.JsonSerializer.NonGeneric.Utf8.Serialize(DataHelper.Defalut.Employees[0]);
    var employees = SpanJson.JsonSerializer.Generic.Utf8.Deserialize<Employee>(data);
   }
  }
 }

 public class StreamPool_JSON : BeetleX.Benchmark.BenchmarkBase
 {
  protected override void OnTest()
  {
   RunTest();
  }

  private async void RunTest()
  {
   while (Increment())
   {
    using (Stream stream = MemoryStreamPool.Pop())
    {
     await SpanJson.JsonSerializer.NonGeneric.Utf8.SerializeAsync(DataHelper.Defalut.Employees[0], stream);
     stream.Position = 0;
     var employees = await SpanJson.JsonSerializer.Generic.Utf8.DeserializeAsync<Employee>(stream);
    }
   }
  }
 }


 public class Bytes_JSON_List : BeetleX.Benchmark.BenchmarkBase
 {
  protected override void OnTest()
  {
   while (Increment())
   {
    var data = SpanJson.JsonSerializer.NonGeneric.Utf8.Serialize(DataHelper.Defalut.Employees);
    var employees = SpanJson.JsonSerializer.Generic.Utf8.Deserialize<List<Employee>>(data);
   }
  }
 }

 public class StreamPool_JSON_List : BeetleX.Benchmark.BenchmarkBase
 {
  protected override void OnTest()
  {
   RunTest();
  }

  private async void RunTest()
  {
   while (Increment())
   {
    using (Stream stream = MemoryStreamPool.Pop())
    {
     await SpanJson.JsonSerializer.NonGeneric.Utf8.SerializeAsync(DataHelper.Defalut.Employees, stream);
     stream.Position = 0;
     var employees = await SpanJson.JsonSerializer.Generic.Utf8.DeserializeAsync<List<Employee>>(stream);
    }
   }
  }
 }

測試結(jié)果

C:\Users\Administrator\Desktop\json_test>dotnet JsonSample.dll
BeetleX.Benchmark [0.5.4.0] Copyright ? ikende.com 2019
EMail:henryfan@msn.com
Github:https://github.com/ikende
-------------------------------------------------------------------------------
|Name                          | Round| Threads|     Count| Use time(s)|   Sec|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|       1|   1000000|        5.57|179580|
-------------------------------------------------------------------------------
|StreamPool_JSON               |     1|       1|   1000000|        5.44|183898|
-------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|       1|   1000000|       43.01| 23248|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|       1|   1000000|       42.75| 23391|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|       2|   1000000|        2.81|355990|
-------------------------------------------------------------------------------
|StreamPool_JSON               |     1|       2|   1000000|        2.95|338969|
-------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|       2|   1000000|       23.16| 43180|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|       2|   1000000|        22.4| 44650|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|       4|   1000000|        1.51|661246|
-------------------------------------------------------------------------------
|StreamPool_JSON               |     1|       4|   1000000|        1.57|636130|
-------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|       4|   1000000|       13.35| 74915|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|       4|   1000000|       11.97| 83508|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|       8|   1000000|         .83|1199453|
--------------------------------------------------------------------------------
|StreamPool_JSON               |     1|       8|   1000000|         .88|1142495|
--------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|       8|   1000000|        9.24|108228|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|       8|   1000000|        6.75|148132|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|      16|   1000000|         .56|1795910|
--------------------------------------------------------------------------------
|StreamPool_JSON               |     1|      16|   1000000|         .74|1344851|
--------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|      16|   1000000|        7.67|130424|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|      16|   1000000|        4.61|216860|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|      24|   1000000|         .54|1849769|
--------------------------------------------------------------------------------
|StreamPool_JSON               |     1|      24|   1000000|         .73|1361382|
--------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|      24|   1000000|        7.61|131373|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|      24|   1000000|         4.7|212779|
-------------------------------------------------------------------------------
|Bytes_JSON                    |     1|      32|   1000000|         .55|1825484|
--------------------------------------------------------------------------------
|StreamPool_JSON               |     1|      32|   1000000|         .75|1339050|
--------------------------------------------------------------------------------
|Bytes_JSON_List               |     1|      32|   1000000|        8.01|124885|
-------------------------------------------------------------------------------
|StreamPool_JSON_List          |     1|      32|   1000000|        5.21|192038|
-------------------------------------------------------------------------------
Test completed!

總結(jié)

從測試結(jié)果來看,如果序列化的對象比小,那可以直接基于bytes的方式。雖然會產(chǎn)生新的bytes對象,不過由于對象比較小,引起的分配和回收并沒有對象池操作上的損耗高。不過如果對象相對復(fù)雜些的情況下,那對象池的作用就能發(fā)揮出來,并發(fā)越大其作用越明顯!,當(dāng)并發(fā)線程數(shù)達(dá)到8的時候,效率已經(jīng)明顯拋開!由于業(yè)務(wù)上的數(shù)據(jù)信息都相對比較復(fù)雜些,所以在處理上還是建議通過對象池的方式來完成json序列化處理。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.net core怎樣在網(wǎng)絡(luò)高并發(fā)下提高JSON的處理效率”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI