BenchmarkComponent.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class BenchmarkComponentEvent : ObjectEvent<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 async 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. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
  26. this.TestAsync(networkComponent, ipEndPoint, i);
  27. }
  28. }
  29. catch (Exception e)
  30. {
  31. Log.Error(e.ToString());
  32. }
  33. }
  34. public async void TestAsync(NetOuterComponent networkComponent, IPEndPoint ipEndPoint, int j)
  35. {
  36. try
  37. {
  38. using (Session session = networkComponent.Create(ipEndPoint))
  39. {
  40. int i = 0;
  41. while (i < 100000000)
  42. {
  43. ++i;
  44. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
  45. await this.Send(session, j);
  46. }
  47. }
  48. }
  49. catch (RpcException e)
  50. {
  51. Log.Error(e.ToString());
  52. }
  53. catch (Exception e)
  54. {
  55. Log.Error(e.ToString());
  56. }
  57. }
  58. public async Task Send(Session session, int j)
  59. {
  60. try
  61. {
  62. await session.Call<R2C_Ping>(new C2R_Ping());
  63. ++this.k;
  64. if (this.k % 10000 != 0)
  65. {
  66. return;
  67. }
  68. long time2 = TimeHelper.ClientNow();
  69. long time = time2 - this.time1;
  70. this.time1 = time2;
  71. Log.Info($"Benchmark k: {this.k} 每10W次耗时: {time} ms");
  72. }
  73. catch (Exception e)
  74. {
  75. Log.Error(e.ToString());
  76. }
  77. }
  78. public override void Dispose()
  79. {
  80. if (this.Id == 0)
  81. {
  82. return;
  83. }
  84. base.Dispose();
  85. }
  86. }
  87. }