Python Metrics: Capture Named Arguments

By Jérôme Vieilledent, on Dec 17, 2020

Metrics are one of the most powerful features of Blackfire. They make it possible to enrich your tests within your profiles, and to modify the way a Call-Graph is being rendered, thanks to Argument Capturing.

When a function is being called several times with different arguments, the default behavior is to aggregate these calls into the same node. This behavior can be customized by defining metrics within a .blackfire.yaml file.

Consider the following snippets:

"""'multipli' module"""

def table(nb, max=10):
	"""Displays the multiplication table of the given number
	from 1 * nb to max * nb"""

	i = 0
	while i < max:
		print(i+1, "*", nb, "=", (i+1) * nb)
		i +=1
import multipli

print("Multiplication table - '6'")



print("Multiplication table - '7'")



Now let’s profile

blackfire-python run python

The result is the following Call-Graph:

We can see the 2 calls to the multipli.table function, aggregated into one and only node.

Argument Capturing to the Rescue

It is possible to discriminate the calls by defining a metric and by capturing the arguments. However, in this example, I am using named arguments… The good news is that with the latest versions of the Blackfire Python Probe (>=1.4.3) and of the Agent (>=1.36.0), it is now possible to capture named arguments.

Consider the following metric:

# .blackfire.yaml
        label: 'Multiplication Table'
                - callee:
                      selector: "=multipli.table"
                          nb: "*"

Notice the argument section: you can now specify arguments by their names.

The resulting Call-Graph is the following:

One note though: Named-argument capturing only works if the matching functions is being called with named-arguments. If you switch to positional arguments, the function calls will then be aggregated back into one node.

Give Blackfire a try!

This powerful feature is available for all Premium and Enterprise plans.

Play with the demo or subscribe now!

Happy Profiling!

Jérôme Vieilledent

As a Developer Advocate, Jérôme is all about spreading the love! His technical/development background enable him to talk as a peer to peers with our developer users. You’ll read his tips and advices on performance management with Blackfire. And he’ll support you as a day-to-day user.