Using the PStats tool for finding performance bottlenecks

From a plain technological point of view, most video games fall into the category of so-called "soft real time multi-agent simulations". This means that in games, we are simulating, by stepping frame by frame, a collection of multiple interacting game entities or agents within soft real time boundaries. Or even simpler: We are moving models and actors around in our game world while trying to maintain a frame rate high enough to create the illusion of smooth movement.

In practice this imposes limits to our games' usage of computing resources, because dropping frame rates and stuttering gameplay need to be avoided at all cost. Additionally, we want our games to be able to run on a wealth of hardware configurations, even ones that do not feature top of the line hardware.

The reasons for a game to perform poorly are manifold: Inefficient algorithms, too many models and actors per scene, too much geometry to draw per model or actor, too many changes of render states, too many transforms—the list of possible causes goes on and on. The fact is, however, that the program as a whole is almost never slow. Instead, in most cases, performance problems are caused by single bottlenecks within the program.

We could just try to find performance problems by randomly crossing off items from our imaginary list of possible causes but in this case, the odds for actually finding anything are not very high. The right way to go about performance problems in our code is to observe, measure, and finally locate the points in our code that make the game exceed the maximum time it is allowed to take for rendering one frame.

Luckily, Panda3D is able to collect detailed profiling data. Together with the PStats tool, which will be introduced in the following tasks, we are able to display and observe this data to quickly find out where precious CPU cycles are wasted in our programs.

Getting ready

We are going to profile the sample program created in the recipe Managing recurring tasks, found in Chapter 7. Please prepare this sample before proceeding to be able to follow the tasks as closely as possible.

How to do it...

These are some of the common tasks where you will be using PStats in your projects:

  1. Open Application.py and add the following line to the constructor of the Application class:
    PStatClient.connect()
    
  2. Navigate to C:Panda3D-1.7.0in and launch pstats.exe. Your firewall might show a popup window asking if you want to allow PStats to listen on a network port. If so, allow the port to be opened.
  3. Launch the application. The following window will pop up:
    How to do it...
  4. Click Graphs | Frame components | App components | Show code components | UpdateSmileys. An additional child window will be created:
    How to do it...
  5. Click Graphs | Nodes to view the following graph:
    How to do it...
  6. Clicking Graphs | System memory will bring up the following display:
    How to do it...
  7. Click Graphs | Vertices. This will show the following child window:
    How to do it...
  8. By clicking Graphs | State changes, the application will present the following graph:
    How to do it...
  9. Finally, click Graphs | Piano Roll to view the following profiling data representation:
How to do it...

How it works...

PStats is a network server program that once started, listens for incoming connections from a Panda3D runtime. At application startup time, in the constructor of the Application class, we instruct the engine to connect to the PStats instance running on the local machine. We could also run PStats on an additional machine and pass a string containing its host name or IP address to PStatClient.connect()—a great feature if we intend to run the game in full screen mode.

As soon as a connection is established, PStats shows its window and displays the Frame time graph to give a very general performance overview. The tool allows us to get exact timing data on individual tasks, the number of nodes in the scene, memory usage statistics, the number of vertices that are drawn, how many state changes there have been in a frame, and a detailed list of which component takes which amount of time to execute, among others.

There is no fixed way of using this tool. The goal should be to observe and interpret the readings. Watch for sudden anomalies and keep an eye on how the readings change over time. Big spikes in the graphs or constantly growing values should make you suspicious.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset