EDIT: Starsector Profiling youtube video tutorial with some discussion on interpreting the results:
http://youtu.be/utT2rJHLb_4 (http://youtu.be/utT2rJHLb_4)
Hi Guys,
I recently went about finding why there was a big performance issue being created in someones code. For us script bashers, we often write code that needs cleanup or optimisation. The next question is, what is wrong with the code?
Seeing as the fix has already been done in the previous code, here is a screenshot that will basically say it all for a script in Shadowyards...
(http://s5.postimg.org/bg7o9ktiv/ms_swacs.jpg)
Now I'm not picking on Shadowyards, I just picked a random mod from my MODs folder and ran a mission. This profile was run over 168 seconds, and here we can see that because at some point during the coding of this script someone decided to leave in this debug line, 1% of the about 1% of the CPU time is being utilised for this error - in just one script.
and we can find this line fairly easily:
(http://s5.postimg.org/dm1z42wzb/ms_swacs2.jpg)
Otherwise the rest of the Shadowyard mod scripts look fine as you can see.
So how do you use this magical and arcane 'profiler' that will tell you about the performance of your code?
If you don't already have Netbeans or a good IDE, go and grab one, and setup your Mod as a project. LazyWizard has already done a post on how to get you setup in Netb173.0[/url]
- Next start up NetBeans and click on Profile --> Advanced Commands --> Manage Calibration Data
Spoiler
(http://s5.postimg.org/l3b6jamif/Profiler1.png)
- Click on your JRE platform, and then click Calibrate (only need to do this once)
Spoiler
(http://s5.postimg.org/gip04d2t3/Profiler2.jpg)
- Wait a few moments and it will tell you your calibration was successful, and may warn you about have dynamic CPU frequency switching. Just click OK. Dynamic CPU frequency is for another discussion.
Spoiler
(http://s5.postimg.org/45c5xgd4n/Profiler3.jpg)
- Now start up Starsector, and jump into a mission or whatever you want to profile. Next step, Click on Profile --> Attach Profiler
- Then click on the CPU and use the Quick Method with the Quick Filter (see below). You can also just Exclude Java core classes. I'd recommend you do profile both at some stage to get a feel for what else is going on and get an idea of how much CPU your mod scripts are using in relation to all the others.
Spoiler
(http://s5.postimg.org/i0agfx7jr/Profiler4.jpg)
- For the first exercise I'm going to monitor all the scripts under data.* as all the rest is usually noise if you go with Exclude Java core classes which will mean tracking of most all the Starsector code.
Spoiler
(http://s5.postimg.org/7rhza3jhz/Profiler5.jpg)
- Either way, once you've set the quick filter, click OK, then Click Attach. You will be prompted which java process, select Starsectors thread, then click OK.
Spoiler
(http://s5.postimg.org/i2yxgi0l3/Profiler6.jpg)
- You are now profiling Starsector with your mod, I personally just use a stop watch, there is one down in the bottom right of NetBeans you can use.
- I'd suggest pressing the 'Live Results' button on the left so you can see a real time summary of your scripts CPU usge.
Spoiler
(http://s5.postimg.org/wng0bbvjr/Profiler7.jpg)
- You can quit Starsector or stop the profiling, and it will prompt you whether you want to take a snapshot. Do so.
- You will then be displayed a snapshot of the profile like below.
Spoiler
(http://s5.postimg.org/8xqkmmx6f/Profiler8.jpg)
- Now when looking at this data, refer it back to your stopwatch. It is that many milliseconds of CPU in relation to the whole time spent profiling. The above profile was taken over nearly 2 minutes.
- You may then want see what your script is doing in the context of the entirety of Starsector, as you may remember before there was the option of Excluding Java core classes with CPU monitoring. If you do that however you can get a lot of noise:
Spoiler
(http://s5.postimg.org/hu1cqknsn/Profiler9.jpg)
- It is then harder to find the offending line of code or called method that is causing performance issues.
Spoiler
(http://s5.postimg.org/afc0y71x3/Profiler10.png)
- Anyway, after you've found the offending method or line of code, right click on it in the snapshot and Click on Go To Source it will then take you to the source file if you have it as a project in NetBeans already.
(http://s5.postimg.org/ff9h657jr/Profiler11.jpg)
Oracle also has an introductory article on this, though it mostly relates to using a profiler with an application that you've built yourself: https://netbeans.org/kb/docs/java/profiler-intro.html (https://netbeans.org/kb/docs/java/profiler-intro.html)
There is also an article on finding memory leaks if you're interested: https://netbeans.org/kb/articles/nb-profiler-uncoveringleaks_pt1.html
For those of you who are running NetBeans with the 64-bit JRE you will need to use NetBeans 64-bit, and those the same applies for 32-bit, 32-bit Starsector = 32-bit NetBeans.
If anyone finds a major step I've missed, or something isn't working please send me an update so I can update this and keep it accurate.
For my next tutorial I'm going to do an article on 'How to debug my Starsector mod script' and show how easy it is to see your variables at breakpoints, and how to update your code whilst Starsector is still running so you don't keep needing to restart Starsector every time you want to make a change.