Fractal Softworks Forum

Starsector => Mods => Modding => Topic started by: Debido on May 02, 2014, 09:45:21 AM

Title: Your script is slow - and how to find out where - now with Video
Post by: Debido on May 02, 2014, 09:45:21 AM
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]


Spoiler
(http://s5.postimg.org/l3b6jamif/Profiler1.png)
[close]


Spoiler
(http://s5.postimg.org/gip04d2t3/Profiler2.jpg)
[close]


Spoiler
(http://s5.postimg.org/45c5xgd4n/Profiler3.jpg)
[close]


Spoiler
(http://s5.postimg.org/i0agfx7jr/Profiler4.jpg)
[close]


Spoiler
(http://s5.postimg.org/7rhza3jhz/Profiler5.jpg)
[close]


Spoiler
(http://s5.postimg.org/i2yxgi0l3/Profiler6.jpg)
[close]



Spoiler
(http://s5.postimg.org/wng0bbvjr/Profiler7.jpg)
[close]


Spoiler
(http://s5.postimg.org/8xqkmmx6f/Profiler8.jpg)
[close]


Spoiler
(http://s5.postimg.org/hu1cqknsn/Profiler9.jpg)
[close]


Spoiler
(http://s5.postimg.org/afc0y71x3/Profiler10.png)
[close]


(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.
Title: Re: Your script is slow - and how to find out where.
Post by: xenoargh on May 02, 2014, 10:18:57 AM
Great article, very useful for people needing to do this stuff :)
Title: Re: Your script is slow - and how to find out where.
Post by: ValkyriaL on May 02, 2014, 10:29:21 AM
Id love to have my stuff checked, Valks are very very old and might not be running as well as they used to, if only i had the time. =/
Title: Re: Your script is slow - and how to find out where.
Post by: Tecrys on May 02, 2014, 11:43:57 AM
I am prou to be the first guinnea pig and it improved performance a lot!

Great job, Debido and Dark.Revenant!
Title: Re: Your script is slow - and how to find out where.
Post by: Uomoz on May 02, 2014, 11:49:59 AM
Uh oh...

(http://i.imgur.com/camcvxC.png)
Title: Re: Your script is slow - and how to find out where.
Post by: Debido on May 02, 2014, 12:13:14 PM
Uh oh...

(http://i.imgur.com/camcvxC.png)

Well that looks fairly clean given perspective, if that was taken over a duration of 2-3 minutes, then that is quite normal. 200ms is nothing over the course of 180,000ms in 3 minutes.
Title: Re: Your script is slow - and how to find out where.
Post by: Tecrys on May 02, 2014, 12:39:54 PM
I think it's about the reflector shield, that is 20 times slower. The rest seems fine.
Title: Re: Your script is slow - and how to find out where.
Post by: Debido on May 02, 2014, 02:13:13 PM
Ok, I've done a video on the top,it's going to take about 2.5 hours to upload...>_<
Title: Re: Your script is slow - and how to find out where - now with Video
Post by: GenBOOM on May 03, 2014, 10:25:11 PM
nice post  :)

prolly should have posted this on youtube comments...
your mic is very quiet, have you turned up to max and its still not loud enough  :-\

usually I have to turn videos down, so your mic is much lower volume than avg youtube vids just fyi
Title: Re: Your script is slow - and how to find out where - now with Video
Post by: Debido on May 04, 2014, 02:07:18 AM
Thank GenBOOM. Sadly my Microphone setup is a bit rubbish, there is probably a way I can boost the signal in post but it would likely produce more noise than signal. Sorry about that, I'll see if there is anything I can do with the mic if I do another tutorial.
Title: Re: Your script is slow - and how to find out where - now with Video
Post by: GenBOOM on May 05, 2014, 05:12:59 AM
If you need mic boost, livestream, or just reg video recording I'd recommend http://obsproject.com/ (http://obsproject.com/)  ;D
Title: Re: Your script is slow - and how to find out where - now with Video
Post by: Sundog on May 24, 2014, 12:08:49 PM
I've been meaning to thank you for this guide (as well as the debugging one). I just assumed it would be a huge hassle to get debugging/profiling working with a Main()less project. It has been very helpful to be able to debug properly, so thanks!  :D
Title: Re: Your script is slow - and how to find out where - now with Video
Post by: Debido on May 24, 2014, 12:41:16 PM
I've been meaning to thank you for this guide (as well as the debugging one). I just assumed it would be a huge hassle to get debugging/profiling working with a Main()less project. It has been very helpful to be able to debug properly, so thanks!  :D

You're welcome.

Another top tip for debugging is to unzip the StarsectorAPI zip file that has the source in it, place that in a folder somewhere and make a new NetBeans project where you point the source to that folder. It allows you to interrogate the source files of the API with a simple Ctrl + Click on the API or function name and it often has more documentation than the JavaDoc.

The second reason this is useful is if you're getting an NPE or other such error coming from one of the com.fs.starfarerapi.api.impl scripts and you need to debug things such as errors you are producing in the vanilla FleetEncounterContextDialog function.