Search Unity

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,090
    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

    Unity Technologies

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

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,090
    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

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    424
    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:
    1,590
    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.
unityunity