Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Your script is slow - and how to find out where - now with Video  (Read 7695 times)

Debido

  • Admiral
  • *****
  • Posts: 1183
    • View Profile

EDIT: Starsector Profiling youtube video tutorial with some discussion on interpreting the results:
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...



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:


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
[close]

  • Click on your JRE platform, and then click Calibrate (only need to do this once)

Spoiler
[close]

  • 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
[close]

  • 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
[close]

  • 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
[close]

  • 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
[close]


  • 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
[close]

  • 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
[close]

  • 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
[close]

  • It is then harder to find the offending line of code or called method that is causing performance issues.

Spoiler
[close]

  • 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.



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
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.
« Last Edit: May 03, 2014, 01:53:30 AM by Debido »
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #1 on: May 02, 2014, 10:18:57 AM »

Great article, very useful for people needing to do this stuff :)
Logged
Please check out my SS projects :)
Xeno's Mod Pack

ValkyriaL

  • Admiral
  • *****
  • Posts: 2145
  • The Guru of Capital Ships.
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #2 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. =/
Logged

Tecrys

  • Admiral
  • *****
  • Posts: 595
  • repair that space elevator!
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #3 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!
Logged
Symbiotic Void Creatures 0.5.0-alpha for 0.97a is out
https://fractalsoftworks.com/forum/index.php?topic=28010.0

Uomoz

  • Admiral
  • *****
  • Posts: 2663
  • 'womo'dz
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #4 on: May 02, 2014, 11:49:59 AM »

Uh oh...

Logged

Debido

  • Admiral
  • *****
  • Posts: 1183
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #5 on: May 02, 2014, 12:13:14 PM »

Uh oh...



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.
Logged

Tecrys

  • Admiral
  • *****
  • Posts: 595
  • repair that space elevator!
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #6 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.
Logged
Symbiotic Void Creatures 0.5.0-alpha for 0.97a is out
https://fractalsoftworks.com/forum/index.php?topic=28010.0

Debido

  • Admiral
  • *****
  • Posts: 1183
    • View Profile
Re: Your script is slow - and how to find out where.
« Reply #7 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...>_<
Logged

GenBOOM

  • Ensign
  • *
  • Posts: 24
    • View Profile
Re: Your script is slow - and how to find out where - now with Video
« Reply #8 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
Logged

Debido

  • Admiral
  • *****
  • Posts: 1183
    • View Profile
Re: Your script is slow - and how to find out where - now with Video
« Reply #9 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.
Logged

GenBOOM

  • Ensign
  • *
  • Posts: 24
    • View Profile
Re: Your script is slow - and how to find out where - now with Video
« Reply #10 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/  ;D
Logged

Sundog

  • Admiral
  • *****
  • Posts: 1727
    • View Profile
Re: Your script is slow - and how to find out where - now with Video
« Reply #11 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

Debido

  • Admiral
  • *****
  • Posts: 1183
    • View Profile
Re: Your script is slow - and how to find out where - now with Video
« Reply #12 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.
Logged