BenchmarkComponentSystem.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. using ETModel;
  5. namespace ETHotfix
  6. {
  7. [ObjectSystem]
  8. public class BenchmarkComponentSystem : AwakeSystem<BenchmarkComponent, IPEndPoint>
  9. {
  10. public override void Awake(BenchmarkComponent self, IPEndPoint a)
  11. {
  12. self.Awake(a);
  13. }
  14. }
  15. public static class BenchmarkComponentEx
  16. {
  17. public static void Awake(this BenchmarkComponent self, IPEndPoint ipEndPoint)
  18. {
  19. try
  20. {
  21. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  22. for (int i = 0; i < 1000; i++)
  23. {
  24. self.TestAsync(networkComponent, ipEndPoint, i);
  25. }
  26. }
  27. catch (Exception e)
  28. {
  29. Log.Error(e);
  30. }
  31. }
  32. public static async void TestAsync(this BenchmarkComponent self, NetOuterComponent networkComponent, IPEndPoint ipEndPoint, int j)
  33. {
  34. try
  35. {
  36. using (Session session = networkComponent.Create(ipEndPoint))
  37. {
  38. int i = 0;
  39. while (i < 100000000)
  40. {
  41. ++i;
  42. await self.Send(session, j);
  43. }
  44. }
  45. }
  46. catch (RpcException e)
  47. {
  48. Log.Error(e);
  49. }
  50. catch (Exception e)
  51. {
  52. Log.Error(e);
  53. }
  54. }
  55. public static async Task Send(this BenchmarkComponent self, Session session, int j)
  56. {
  57. try
  58. {
  59. await session.Call(new C2R_Ping());
  60. ++self.k;
  61. if (self.k % 100000 != 0)
  62. {
  63. return;
  64. }
  65. long time2 = TimeHelper.ClientNow();
  66. long time = time2 - self.time1;
  67. self.time1 = time2;
  68. Log.Info($"Benchmark k: {self.k} 每10W次耗时: {time} ms");
  69. }
  70. catch (Exception e)
  71. {
  72. Log.Error(e);
  73. }
  74. }
  75. }
  76. }