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 ...

Get Practical Maya Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.