Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Profile Analyzer: How do I start/stop recording of profiling data from script?

Discussion in 'Profiler Previews' started by Peter77, May 14, 2019.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,640
    For quite a while I provide performance overviews for every Unity beta release (see here).

    I would like to extend this test by saving Unity profiling data too, that I can then analyze in the new Profile Analyzer.

    What I'm trying to do is to start and stop recording of profiling data from within the build, then save this data as a file. The test currently collects data during specific time periods only, basically separate data for every level.

    I would like to run this without the need to have an Unity editor open.

    I want to implement it that way, because the test collects performance data of currently 60 different levels in total. I don't want to do this manually, thus I want to continue to keep it automated. I would also like to keep the option to be able to send the performance-test-build to testers, thus the requirement for no-editor.

    My question is:
    • How can I start/stop profiling via scripting from within a build?
    • How can I save the recorded data as a file with a specific file path?
     
    idbrii and tigerleapgorge like this.
  2. alexeyzakharov

    alexeyzakharov

    Joined:
    Jul 2, 2014
    Posts:
    508
    idbrii, MartinTilo and Peter77 like this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,640
    Hi Alex!

    wow, that sounds really simple to implement. I wonder why I didn't find it :) Thanks for the quick reply!
     
    MartinTilo and alexeyzakharov like this.
  4. alexeyzakharov

    alexeyzakharov

    Joined:
    Jul 2, 2014
    Posts:
    508
    Great, let me know if that works for you or you have any issues :)
     
  5. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    Profiler.enabled was also useful when profiling interactively in the editor:

    Code (CSharp):
    1. [UnityEditor.MenuItem("Tools/Roll Many")]
    2. static void Menu_RollMany()
    3. {
    4.     // Unfortunately, this doesn't start recording in Profiler.
    5.     Profiler.enabled = true;
    6.  
    7.     Profiler.BeginSample("Roll 10,000 - dicebag");
    8.     for (int i = 0; i < 10000; ++i)
    9.     {
    10.         dicebag();
    11.     }
    12.     Profiler.EndSample();
    13.  
    14.     Profiler.BeginSample("Roll 10,000 - Simplified");
    15.     for (int i = 0; i < 10000; ++i)
    16.     {
    17.         Simplified();
    18.     }
    19.     Profiler.EndSample();
    20.  
    21.     Profiler.enabled = false;
    22. }
    I open Profiler, Clear, start recording, click my Tools/Roll Many menu item (recording pauses but doesn't stop), stop recording, and my data is the last frame.
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,490
    Not sure I get your point but just FYI: in the editor, you might have to also set ProfilerDriver.enabled and ProfilerDriver.profileEditor (if what you are measuring isn't part of the playmode player).
     
    Deadcow_ and UnLogick like this.
  7. AdrianMesa

    AdrianMesa

    Joined:
    Oct 8, 2021
    Posts:
    10
    Hi!
    I was observing an issue in a tool we did to do performance reports. We weren't getting frames after Profiler.enabled was set to true when entering in Play mode.
    In order to record information in Play mode in Editor you have to enable both the Profiler and the ProfilerDriver.

    The behaviour of this feature is tricky, because if you have the profiler tab present, Profiler.enabled = true is enough to start recording some data.
     
    MartinTilo likes this.