BenchmarkComponent.cs 1.5 KB

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