BenchmarkComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class BenchmarkComponentEvent : ObjectEvent<BenchmarkComponent>, IAwake<string>
  7. {
  8. public void Awake(string address)
  9. {
  10. this.Get().Awake(address);
  11. }
  12. }
  13. public class BenchmarkComponent: Component
  14. {
  15. private int k;
  16. private long time1 = TimeHelper.ClientNow();
  17. public async void Awake(string address)
  18. {
  19. try
  20. {
  21. NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
  22. for (int i = 0; i < 100; i++)
  23. {
  24. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
  25. this.TestAsync(networkComponent, address, i);
  26. }
  27. }
  28. catch (Exception e)
  29. {
  30. Log.Error(e.ToString());
  31. }
  32. }
  33. public async void TestAsync(NetOuterComponent networkComponent, string address, int j)
  34. {
  35. try
  36. {
  37. using (Session session = networkComponent.Create(address))
  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. }