Jump to content


Calculating The Current Server FPS In Sourcepawn

Recommended Posts

Hi everyone,


I figured I'd post here about this to see if anybody had ideas. I'm trying to make a plugin that precisely calculate the amount of frames per second the server is running at. At the moment, I've made a public GitHub repository of this that adds a forward for when the server is under a certain threshold regarding FPS. However, for some reason, the calculations aren't precise. For example, when the in-game net_graph reports the server running at ~10 FPS, the plugin only calculates a ~20% decrease compared to the server's tick-rate (the maximum FPS the server should be running at). The fact that it even goes down when the server is performing poorly is a good sign, but I'm not sure why it isn't as precise as net graph.


In the plugin itself, I use the OnGameFrame() forward to execute code before each frame (which in this case, calculates the time using GetEngineTime() and increments the frame rate until one second is up). The code on GitHub is located here. I will post it below.


public void OnGameFrame()
    // Always increase frame count.

    // Get engine time and compare against last time.
    float current_time = GetEngineTime();
    float time = current_time - last_time;

    // Check to see if we exceed a second.
    if (time >= 1)
        last_time = current_time;

        float fps = float(g_framecount);
        g_lastfps = fps;

        // Reset frame count to 0.
        g_framecount = 0;

        // Reset g_indexlasttick if it exceeds g_cvMaxStore value.
        if (g_index > (g_cvMaxStore.IntValue - 1))
            g_uptospeed = true;
            g_index = 0;

        g_fpsarr[g_index] = fps;
        if (g_uptospeed)
            // Calculate average FPS.
            float avg;
            int j = g_index;

            for (int i = 0; i < g_cvMaxStore.IntValue; i++)
                if (j < 0)
                    j = (g_cvMaxStore.IntValue - 1);

                avg += g_fpsarr[j];


            avg /= g_cvMaxStore.FloatValue;

            if (avg <= 0)

            if (avg < g_cvThreshold.FloatValue)
                // Call On Detection forward.

        // Increase index.


Does anyone have any suggestions for this?


Thank you for your time.

Share this post

Link to post
Share on other sites

Do you know if it can do anything weird if your loop finishes after the next frame begins?  How big is g_cvMaxStore and do you know how long it takes for that loop to run compared to each frame?


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...