Profiling using MEL or Python or the API

Profiling using MEL/Python commands

You can use the MEL/Python profiler and profilerTool commands to start/stop recording in the Profiler, query event information, search events, as well as to instrument procedures for profiling. Instrumentation is the modification of a program to collect information for analysis.

The following profilerTool flags must be used in edit mode:

The following profilerTool flags must be used in query mode:

Using MEL

To record

Use the profiler command -sampling (-s) flag to enable and disable the recording of events; for example, as follows:

profiler -s true;
polyCube;
profiler -s false;

To add a category

To add a profiling category, use the -addCategory flag; for example, as follows:

int $melCategoryIndex = `profiler -addCategory "MEL Scripts"`;
Note:

Currently, category names with localized characters (that is, upper-ASCII and double byte characters) are not supported.

To instrument a procedure

Define your MEL procedure, then instrument this procedure using the -instrumentMel flag. The following example demonstrates how to enable the instrumentation of a procedure called Factorial.

profiler -instrumentMel true -procedureName "Factorial" -categoryIndex $melCategoryIndex -colorIndex 8 -procedureDescription "FactorialDesc" ;

You can disable the instrumentation of one or more procedures. To disable the instrumentation of a specific procedure, set -instrumentMel to false and provide the name of the procedure for which you want to disable instrumentation:

profiler -instrumentMel false -procedureName "Factorial" -categoryIndex $melCategoryIndex;

To disable all instrumentation at once, use the -clearAllMelInstrumentation flag:

profiler -clearAllMelInstrumentation;

See the profiler MEL command documentation for more information on the flags mentioned above, as well as other useful flags, such as: -eventCount that queries the event count in the buffer, or -reset to reset the Profiler's data.

See the profilerTool MEL command documentation for information on how to interact with the Profiler's main view. For example, you can switch between views, or isolate a particular segment.

Using Python

To record

To enable and disable the recording of events, set sampling to True/False respectively as follows:

cmds.profiler(sampling = True)
cmds.polyCube()
cmds.profiler(sampling = False)

To add a category

To add a profiling category, call OpenMaya.MProfiler.addCategory() as follows:

categoryIndex = OpenMaya.MProfiler.addCategory("Python Scripts")

To instrument a procedure

To instrument a procedure, call OpenMaya.MProfilingScope() in your procedure definition; for example, as follows:

def Factorial(number):
    # Instrument this procedure
    profiler = OpenMaya.MProfilingScope(categoryIndex, OpenMaya.MProfiler.kColorE_L1, "Factorial", "FactorialDesc")
    
    result = 1
    for i in range(1, number+1):
        result = result * i
    return result

See the profiler Python command documentation for more information on the flags mentioned above, as well as other useful flags, such as: eventCount that queries the event count in the buffer, or reset to reset the Profiler's data. The -instrumentMel flag can be used by Python to instrument a MEL procedure.

Note: Performance issues can occur when executing large amounts of Python commands. If this occurs, define MAYA_RETAIN_PYTHON_GIL in your environment. (MAYA_RELEASE_PYTHON_GIL is deprecated. It is true by default and can be overwritten by MAYA_RETAIN_PYTHON_GIL.)

Also, Python performance issues can occur when evaluating other threads with custom Python nodes.

See the profilerTool Python command documentation for information on how to interact with the Profiler's main view. For example, you can switch between views, or isolate a particular segment.

Instrument a function in your plug-in

You can call the MProfiler::addCategory() method in your plug-in to add a category to the Profiler. Call MProfilingScope::MProfilingScope() to instrument a function that you want to profile. See the MProfiler and MProfilingScope C++ API reference documentation, as well as What's New in Extension for Autodesk Maya 2015 in the Maya Developer Help for more information.

Instrument a procedure and register an associated DG node

When you instrument a procedure using Python or C++, you can also register an associated DG node. This way, when you select an instrumented event in your Profiler graph, its associated DG node is also selected and displayed in the Attribute Editor, Outliner or Node Editor.

To do so, when you call MProfilingScope::MProfilingScope() in your Python script or C++ plug-in, provide the associated DG node as an MObject as one of the input parameters. The MObject input parameter must point to a DG node and cannot point to an attribute or a component.

For more information, see the OpenMaya::MProfilingScope() C++ API Reference documentation, as well as What's New in Autodesk Maya 2016 in the Maya Developer Help for more information.

Related topics