Python Metrics: Capture Named Arguments
Argument Capturing also work in Python using named-arguments.
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
Consider the following snippets:
# multipli.py """'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
# test.py import multipli print("Multiplication table - '6'") print("--------------------------") multipli.table(nb=6) print("") print("Multiplication table - '7'") print("--------------------------") multipli.table(nb=7) print("")
Now let’s profile
blackfire-python run python test.py
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 metrics: multiplication_table: label: 'Multiplication Table' matching_calls: python: - callee: selector: "=multipli.table" argument: nb: "*"
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.
Start your Premium Trial Now!