Search Unity

Higher network latency than expected with MLAPI

Discussion in 'Multiplayer' started by Joskman, Jul 4, 2021.

  1. Joskman

    Joskman

    Joined:
    Jan 6, 2019
    Posts:
    5
    I have developed a small sandbox to analyse network latency issues with MLAPI, resulting in 43ms (!!) between any Server and Client communication, both running locally (see attached picture), on the same machine.

    Is there a common cause for this big delay?

    ------------------------- Details of the sandbox:

    Sandbox based in Unity’s tutorial: https://docs-multiplayer.unity3d.com/docs/tutorials/helloworldtwo

    Scenario:
    • As showed in attached screen capture, there are 2 instances of the same application, each able to act as Server and Client at will, running at 60fps (top left corner).
    • Please note that both instances run on the same machine (so latency should be minimal).
    • Experiment starts when we click a button in Server app that sends a network variable (type =NetworkVariableVector3) to Client.
    • Client has an event that receives changed network variable. Time intervals are measured.
    Results:
    • Time between Server variable change and Client reception is 387-344 = 43ms (!!), time enough on my machine to draw 3 full frames at 60fps. The lag is clearly visible to naked eye.
    Question:
    Is there an explanation to this long delay? Knowing it all happens on the same machine, no real network traffic involved. I hoped there would be a lot of network capacity... even inside one frame! But, 3 frames for just one Vector3??

    I also thought network traffic was independent on frames, relying only on network quality, not on graphics performance. Am I wrong and perhaps network traffic is somehow binding to frame rate? If so... does it make sense?

    What are the parameters I should correct to overcome this issue? (the NetworkManager parameters right now are the Unity defaults; also NetworkObject ones).​

    P.S.:
    Following are the full results of the communication between Server and Client application, where you can also see a RPC roundtrip experiment time, higher than expected too:


    12:46:02.715 Host: Server ON -> Button manually clicked
    12:46:04.800 Host: Client ON -> Button manually clicked
    12:46:05.104 Client: Network Start + First object move

    *** Begin of RPC Experiment: ***
    12:46:05.105 Client: Calling Server through RPC to ask for new coordinates
    12:46:05.148 Server: RPC receives Client call and changes network variable (coordinates)
    12:46:05.208 Client: EVENT - Network variable (coordinates) changed -> TOTAL RPC roundtrip time = 208-105 = 103ms (!!)
    *** End of RPC Experiment ***

    *** Begin of send coordinates experiment: ***
    12:46:13.344 Server: Sending coords. to client 1 -> Button manually clicked
    12:46:13.344 Server: Sent coords. to client 1 -> A network variable (coordinates) is assigned in Server
    12:46:13.387 Client: EVENT - Network variable (coordinates) changed -> Network variable change is received by event -> Time travel = 43ms (!!)
    *** End of send coordinates experiment ***
     

    Attached Files:

    Last edited: Jul 6, 2021
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Network traffic is very well depended on the graphics or update loop of the engine. MLAPI sends data at fixed tick rates at the end of each Update and polls them at the beginning of Update. This is necessary so that your game code can work properly. For instance you want RPCs to arrive on the main thread before update to be able to write gameplay code with them. So this adjusting to frames often adds ~20ms of delays under normal circumstances. In addition to that the UNet LLAPI transport does some additional batching which adds delay.
     
    Joe-Censored likes this.
  3. MishaPph

    MishaPph

    Joined:
    Nov 27, 2015
    Posts:
    2
    I have the same problem, ping around 42 ms.
    My fps around 140 frames.
    Network Tick interval: 16ms.
    Sending and receiving data ~20 Kbyte/sec
    I tried on UNet and LiteNetLib, the results almost the same.
    For measuring, I used Unreliable RPC.
    Also, I download the sample project "BossRoomProject". Then with my fps around 50, I got a ping ~60ms.

    So, I tried "Mirror" on my project. The same code just changes network library and RPC methods, I got ping less than 10 ms.
     
    Joskman likes this.
  4. Cranom

    Cranom

    Joined:
    Jan 31, 2018
    Posts:
    26
    Bumping the subject, as it's an interesting one for decision making regarding project type, sorry @luke-unity don't know if there is a general tag for the mlapi team.

    A small study case on the matter from the MLAPI team would be nice, or should i say mandatory, since latency will determine speed of play and will prevent creators from succeeding at fast-paced games or at the very least will force devs to hide latency via trickery such as increasing animation time and generally slowing the feel of the game among other things...

    There was a comparison of various API some times ago from unity but via hear-say, it was clearly lacking in seriousness and updated after negative feedbacks received from the dishonesty it demonstrated. A clear description of any process adding latency is needed and the "how to" circumvent them if at all possible , and data to back everything up!
     
  5. sssemil

    sssemil

    Joined:
    Sep 20, 2020
    Posts:
    2
    Same issue in my case.
     
  6. firaui

    firaui

    Joined:
    Nov 14, 2018
    Posts:
    12
    Hello Luke,

    How do you suggest to calculate just the network latency without frame calculation?