BenchmarkComponent.cs 1.6 KB

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