Using the visual studio profiler to profile Ingame Scripts
Here i will show you how to Attach and profile Ingame scripts with the Visual studio compiler.
- Visual Studio 2017 Installed
- Space Engineers Installed
- A script to profile
Attaching the Visual Studio Debugger.
First we will Attach the Visual studio debugger to the Space Engineers (Of course make sure its running). To do this we will first go over to the Debug tab in Visual studio and click the “Attach to process” option.
After clicking this option you will be greeted by a List of processes, scroll down the list and find “SpaceEngineers.exe”.
After selecting it click “Attach”. You should now see a screen on the side pop up with Diagnostic tools, the debugger is now Attached and ready to go!
Profiling a script
Great! so you have your debugger attached, now for actually profiling a script with it. To do this you will ofcourse first need to make sure you have a script running in the world. After setting up the scenario you want to profiler you can click the “Record CPU Profile” button, this will start the profiling.
After enabling the profiling you will be taken over to the CPU Usage tab, now you do whatever you need to do in SE and when you think you have profiled for long enough you can click “Break All” to stop profiling (Note that this will freeze the game) and see the results.
Reading the results
After profiling you will sometimes be asked to select a range of time to analyze, To do this you click and drag within the CPU usage graph up top and select a range. After selecting a range You will see a list of function names appear, since we are profiling the Programmable block we will be looking for “Program::Main” (note, not My. Now if your script is very slow it will show up right at the top. you can also ofcourse use the filters and the searchbar to find the programmable block run.
Note: in case you cannot find your programmable block main/ are only finding the wrong one in that list you can also find “MyProgrammableBlock.RunSandboxedProgramAction” and click the one that looks like this:
After this you will see a list of mains there, you may need some trial and error to find the correct one, (These Main methods are from All the programmable blocks in the game but im sure you will figure it out.
From there on you will be able to see which parts of your script take how much performance (in %) hovering over them you can also see how many milliseconds it took in your selected time period.
Also dont forget that this is a full on profiler and that you are profiling the entire game, so if you want you can also use this knowledge to see which areas of the game are slow and which blocks/systems might be slowing down your worlds.