Using the profiler

A community post by kn0pee, last updated on February 1, 2023


Introduction
In a recent update to FiveM, we have a very easy and in game tool which we can use to identify the exact lines of code and threads causing issues within your server!

This can be used both server side, to help find hitch warnings, or client side to improve FPS. This guide will not explain how to fix these issues, simply how to identify where they are so you can tweak and research to fix them

Prerequisites
To ensure profiler works, make sure your client is up to date, and you are using the latest server artifacts. You also need google chrome installed to properly view the profile. If you are viewing the profile without saving first make sure you keep your game / the server open and running!

Usage of the Profiler

Capturing a Profile

To begin the profiling, open your console (F8 on client, or the server console), and type the following command
profiler record (frames to record)
A good starting point is 500 frames, as this captures things over a substantial period of time. All going well you should see a message which says start

image
To check the status of the profiler, such as whether the capture is active, and how many frame it has captured, use profiler status. It will display something like this
image
Once the profile is complete, it will display that it is no longer recording
image

Viewing the Profiles

You can immediately view the capture using profiler view, which will open google chrome and begin loading the profile.

NOTE: Viewing profiles server side will not automatically load chrome, you will need to copy the provided link to chrome.

Saving a Profile

You can save a profile by using the command
profiler saveJSON filename.json
This will save a file to the folder where your run.bat file lives.

image

Once you have saved the file, copy it to your computer into a folder you can navigate to easily and complete the following steps

  1. Open a new tab on Google Chrome
  2. Open the dev tools by pressing CTRL + SHIFT + I or Settings > More Tools > Developer Tools
  3. Select the Performance tab

image

  1. Right click anywhere on the panel, and select load profile

image

  1. Navigate to the saved file, open it and continue the rest of the guide

Reading the Profiles

image
When you open the profile, you will be presented with a similar screen (I recommend adjusting the size to view it easier).

Starting from the top bar, we have the time stamp of the recording, followed by an FPS graph (green) and finally a CPU time graph (yellow, this one is important). This can be used to identify peaks, which correlate to frame drops / micro stuttering or server hitches.

Server Hitch Example

We can identify server hitches by sudden spikes in the CPU time, which exceed normal frames.

Viewing Specific Frame Information
Scrolling on the time bar will cause the whole display to zoom in on the specific period of time your cursor is hovering on. At this point, you will want to zoom in on specific frames, such as the one below.
image
The information above shows the recording timestamp, the total frame time (green section) and the script threads which executed that frame. Hovering over the frame time also displays average FPS at that time.

Viewing the resource tick breakdown
Next, zoom on the yellow resource tick event so we can identify what is exactly causing the FPS drop / loss.


In this example, I have a script named debug which has a problematic thread causing significant performance loss. Hovering over any of the events will outline how many ms it is taking to process this thread, and display the file and even line numbers of the thread causing problems.
In this example, it is located in a file called client.lua, on lines 29 to 34.

Hope this helps people, if you need a hand or better explanation of something with the profiler, comment or send me a DM and I will do my best to help