FProfiler - GMod Profiling tool

FProfiler - Find those god damn performance hogs

Perfect for the following things:

  • Finding performance bottlenecks (i.e. which code is spent the most time on)
  • Finding out which functions are called most
  • Identifying the source of lag spikes
  • Profiling specific functions

DOWNLOAD

Using FProfiler

The FProfiler console command opens the profiler. Everything can be done from there.
The Readme contains a tour of the buttons n shit.

Using FProfiler through code
You can just start profiling whenever the fuck you like. You don’t need to click the button. The functions are simple as fuck:
[lua]-- Starts profiling.
– When focus is given, the profiler will only profile the focussed upon function, and the functions it calls
FProfiler.start([focus])

– Stops profiling
FProfiler.stop()

– Continue profiling
FProfiler.continueProfiling()[/lua]

Oh but how would I get the data!?” Simple, just open the menu, like, whenever. You could also use the internal functions if you want your own format of the data.

ABOUT PROFILING AND PERFORMANCE IN GENERAL
When faced with performance problems, people tend to dive in the code to perform micro-optimisations. Think of localising libraries to the scope of a file, storing LocalPlayer() in a variable for re-use and that kind of stuff. This is a naive approach and is unlikely to get you very far. The reason for that is very simple: micro-optimisations have very little effect on the eventual performance of the game. They’re called micro-optimisations for a reason.

What you should be after is macro-optimisations, i.e. the big guys. Attacking those will give you the biggest benefit. Doubling your FPS is not uncommon when you attack the big guys.

What do I mean by macro-optimisation/the big guys you ask? Think of reducing an O(n^2) algorithm to an O(n lg n) one, if you know what that means. Think of things like using more efficient data structures, more efficient algorithms, caching the results of complicated calculations, alternative ways to calculate things that don’t give the exact right result, but give a “good enough” result and are way faster than the original algorithm. THAT kind of shit.

That’s where the profiler comes in. Always mistrust what you think is a big performance hog is, measure it. Even the assumptions of people who have optimising code as their profession are often proven wrong by the profiler. Don’t be smug and think you can do any better.

When working on performance, the profiler is to be your guide. The profiler is to tell you what to optimise. Do not bother with anything other than the most expensive functions for you will be wasting your time.

VIDEO

Thanks a lot. Your FProfiler works fantastic and have helped with my gamemode and addons.

This is fucking fantastic.

Also loving the tin can used as a microphone. :slight_smile:

I would not call micro-optimisations a naïve approach: efficiency across the board is the idea. If you have the option to store the LocalPlayer in a variable and localise libraries to a file (although LuaJIT handles most of this now), then do it. Small efficiency measures add up, but it’s not like you have to sacrifice one for the other.

[editline]1st May 2016[/editline]

Great work as always, of course.

i have only skimmed over the code, but is there no stopping players from fetching code from the server? i can only find the reverse

Found out what caused the lag at the start of every TTT round because of this.

Just gave it a lil test and it’s working very well, thanks Falco!

Would there be any noticeable performance decrease from running this on a live server, or should this tool only be used in devlopement?

Due to the way profiling works, this would indeed have a very significant impact on performance, especially with multiple other addons.

Profiling of this magnitude should only be used when in active development or during optimization cycles of finding bottlenecks and expensive functions.

I get that the GMod development community is (largely) casual and doesn’t follow modern software development standards and practises, but wow I am kinda surprised it took this long for someone to release a proper profiling suite.

Sick. Might play around with this a bit with some shit I sometimes come back to. It doesn’t run anywhere near as well as I know I could make it run!

Oub released a hook profiler ages ago, it’s just outdated iirc

Only superadmins can do anything related to serverside profiling. That includes getting the source of a function. Non-superadmins can be given permission in a CAMI supporting Admin mod. Clientside profiling has no limitations. Clients can read the sources of clientside functions with FProfiler. I don’t consider this a problem, since the clients already have this data. Stealing code is done much more efficiently by searching the downloaded lua files.

[editline]2nd May 2016[/editline]

Micro optimisations have their place, but they’re a waste of time if they’re your starting point to improve performance.
If you have a low FPS on your client, just open your custom HUD file and then start localising LocalPlayer and all the library functions, you’re being naive and you won’t get serious performance gains.

On the other hand, when the profiler points to a small function that is called VERY often, you can attack that in (at least) two ways:

  • have it called less often: the algorithm that uses it may benefit from macro optimisation (again, macro optimisation -> bigger gains)
  • micro optimisations, because a micro optimisation can have a significant effect if the function is very small, called very often and is the bottleneck.

In my optimisation university course (INFOMOV, Utrecht 2015) I learnt about this list of steps on how to attack performance issues:
(0.) Determine optimization requirements

  1. Profile: determine hotspots
  2. Analyze hotspots: determine scalability
  3. Apply high level optimizations to hotspots
  4. Profile again.
  5. Parallelize
  6. Use GPGPU
  7. Profile again.
  8. Apply low level optimizations to hotspots
  9. Repeat steps 7 and 8 until time runs out
  10. Report.

Parallelization and GPGPU don’t apply to gmod Lua, so that can only be skipped. The point is the importance of profiling and attacking high level optimisations first. They’re very important and they’ll give you the biggest performance gains.

Huh alright, looked to me like anyone could request code from the server…

Helped me fix a freezing bug that’s been going on for over a year at least!

@Falco Yeah, I completely agree in that respect to priority. I was implying the use of micro-optimisations as an end goal for any long-term project or habits you should be accustomed to in general (in Lua, using ipairs over pairs for numerical tables with a countable number of elements, caching highly used functions (Page 17)).

This is just sexy as fuck. Awesome job! This is definitely useful.

now i have a statistical reason to improve my shitty code

You’ve been busy again I see!

An excellent, useful tool.

yeah i finally solved the multiple 10+ second long freezes on my server with this v:v:v

It’s great to hear that the tool actively helps solving lag problems. We can make gmod great again!

where do i put this? i tried the command FProfiler and nothing happened :frowning:

EDIT* DONE IT, BUT HOLY CRAP THIS IS AMAZING. I DID THE FIX SOMEONE LINKED EARLIER (https://github.com/garrynewman/garrysmod/pull/1168), basically I have a billion addons and on the first round of opening the server it would lag for around 60-80 seconds and then drop everyone, but now its faster than the speed of light.