BenchmakComponent.cs 1.5 KB

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