Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a decorator to record metrics


A popular thing to do in professional studios is to record how long something takes and how often it happens. This sort of profiling can be useful for tracking things like program startup time, how long it takes to export meshes, or who is using that deprecated tool that should have been killed a long time ago. We will build a decorator to time how long a function takes to execute.

Generally this sort of recording has the following pattern:

  1. Get a unique key for a callable item.

  2. Record the time it takes to invoke the callable item.

  3. Report the time taken to some other system, identifying the callable through the unique key.

This is perfect for a decorator. We use a decorator instead of a context manager because the fact that a function is profiled is an aspect of the function itself, and not something left up to the caller.

The use of the finished decorator will look something like the following:

@profiling.record_duration
def export_scene():
    # do stuff

We...