Skip to main content

Profiling

caution

This guide uses Unity 2021.2 beta. On 2021.1 and below, the profiler outputs only simple statistics.

Profiling is the process of measuring the performance of your game while it is running. It allows you to check your optimization points and control that your enhancements make a real difference.

In multiplayer game programming, you have three primary resources you need to keep under control:

  • Bandwidth consumption
  • CPU
  • Latency

These three resources are the three vertices of a triangle with a constant area. For example, reducing your bandwidth consumption will probably increase your CPU cost and latency by using compression algorithms or heavier serialization strategies.

For this article, we will only focus on bandwidth profiling. You can find the official profiler documentation for CPU here.

Regarding latency, it is a whole topic in itself, but remember that we are talking about the time taken for sending a packet here, not the network latency. Thus, a simple StopWatch on your serialization and compression algorithms should be sufficient to measure the CPU latency you are introducing here.

Prerequisites#

Starting from Unity 2021.1, the Unity profiler can extend its features with Netcode for GameObjects [NetCode]. Netcode enables you to profile your game in the same tool you are already using for CPU, GPU, and memory.

To enable profiling support for Netcode, install the com.unity.multiplayer.tools package from the Unity Package Manager. Then, you should see two additional modules in your profiler (Window > Analysis > Profiler):

  • Messages - Network messages sent to the transport
  • Objects - Activity related to, and grouped by, network objects
important

If you have the profiler window open when you install the tools package in 2020.3 or 2021.1, you may need to close and reopen the profiler window for the moduels to appear.

caution

Netcode for GameObjects does not update the legacy profiler modules Network Messages and Network Operations.

note

The network profiler samples its data by frame rate. Therefore, you may find the graphs a little spiky.

Messages Module#

The following table includes the Messages the Netcode Profiler counts:

MessageDescription
Total Bytes SentTotal number of bytes sent during this frame
Total Bytes ReceivedTotal number of bytes received during this frame
Named Message Bytes SentTotal number of named message bytes sent to any connection in this frame
Named Message Bytes ReceivedTotal number of named message bytes received in this frame
Unnamed Message Bytes SentTotal number of unnamed message bytes sent to any connection in this frame
Unnamed Message Bytes ReceivedTotal number of unnamed message bytes received in this frame
Scene Event Bytes SentTotal number of scene event bytes sent during this frame
Scene Event Bytes ReceivedTotal number of scene event bytes received during this frame
note

Messages sent to the local client (for example, a server in host mode sending RPCs to itself) appears greyed-out in the profiler to indicate that they may not actually be sent over the network.

Objects Module#

The following table includes the Objects the Netcode Profiler counts:

ObjectDescription
Rpc Bytes SentTotal number of remote procedure calls (RPCs) sent in this frame
Rpc Bytes ReceivedTotal number of remote procedure calls (RPCs) received in this frame
Network Variable Delta SentTotal number of network variables sent as a first-time update for an object
Network Variable Delta ReceivedTotal number of network variables sent as a delta
Object Spawned SentTotal number of spawned objects sent in this frame
Object Spawned ReceivedTotal number of spawned objects received in this frame
Object Destroyed SentTotal number of destroyed objects sent in this frame
Object Destroyed ReceivedTotal number of destroyed objects received in this frame
Ownership Change SentTotal number of ownership changes sent
Ownership Change ReceivedTotal number of ownership changes received

Known Issues for Versions Below 2021.2#

  • The profiler modules can be removed from the profiler by either using the Restore Defaults functionality or manually removing them. You can re-add them by re-opening the editor or recompiling a script.

  • If you remove the tools package, the profiler modules remain in the profiler but are not functional. You can remove them by using the Restore Defaults functionality or manually removing them.

Best Practices#

The profiler tool is capable of providing a lot of information and can be intimidating to determine the best strategy for optimization. Use the following best practices to make your optimization process easier.

Module elements and filtering results#

You can use the following filter rules to narrow your search results:

RuleDescription
t:Filters by type
dir:inFilters anything with bytes received
dir:outFilters anything with bytes sent
b>NFilters all events with bytes greater than N (N being an integer)
b<NFilters all events with bytes less than N (N being an integer)

You can combine these rules to continue narrowing your results.

Double-clicking on an element shows it in the Unity hierarchy tab.

Hide unnecessary content#

You can hide profiler data to focus only on information you are interested in.

  1. In the Profiler window, select the dropdown arrow for Profiler Modules and check only the modules you would like to focus on. For example, the Messages and Objects modules.

Profiling a development build of your game#

For the most accurate results, you want to profile your game as a development build on the target hardware whenever possible. To profile a development build of your game on Windows, OS X, Linux, or WebGL, see Profile your application on a target platform for more information.

For iOS and Android, see Profiling on mobile devices.

Profiling inside the Unity editor#

When it is more useful to have a broader sense of performance quickly than high accuracy, you want to profile inside the Unity editor. For example, you may try enabling and disabling multiple GameObjects at runtime to determine the culprit for a performance problem.

To profile inside the Unity editor, see Profiling in the Unity Editor for more information.

Iterate your optimization#

Every time you make new feature or refactor an old one, profile your game. This allows you to compare each change with your performance to adjust for peak efficiency.

There are no definite rules with performance optimization. Profile your game regularly, investigate the nature of your optimization problems, experiment with different solutions, and measure the results of your changes.