BenchmakComponent.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.ClientNowTicks();
  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. using (Session session = networkComponent.Create(address))
  30. {
  31. int i = 0;
  32. while (i < 10000000)
  33. {
  34. ++i;
  35. try
  36. {
  37. await session.Call<C2R_Ping, R2C_Ping>(new C2R_Ping());
  38. ++this.k;
  39. if (this.k % 100000 == 0)
  40. {
  41. long time2 = TimeHelper.ClientNowTicks();
  42. long time = time2 - this.time1;
  43. this.time1 = time2;
  44. Log.Info($"{j} Benchmark k: {k} 每10W次耗时: {time}");
  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. }
  57. }
  58. public override void Dispose()
  59. {
  60. if (this.Id == 0)
  61. {
  62. return;
  63. }
  64. base.Dispose();
  65. }
  66. }
  67. }