-
Notifications
You must be signed in to change notification settings - Fork 36
feat: support log_metric #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| ############# | ||
| Log Metrics | ||
| ############# | ||
|
|
||
| The ``log_metric`` fixture is a powerful tool for recording performance metrics or other numerical data during your tests. It generates a file that follows the Prometheus text-based format, which is highly compatible with modern monitoring systems and the OpenMetrics standard. | ||
|
|
||
| ********** | ||
| Use Case | ||
| ********** | ||
|
|
||
| You can use this fixture to track key metrics from your embedded device, such as boot time, memory usage, or network throughput. By logging these values, you can monitor performance trends over time and catch regressions automatically. | ||
|
|
||
| ************** | ||
| CLI Argument | ||
| ************** | ||
|
|
||
| To enable metric logging, you need to provide the ``--metric-path`` command-line argument. This specifies the file where the metrics will be saved. | ||
|
|
||
| .. code:: bash | ||
|
|
||
| pytest --metric-path=output/metrics.txt | ||
|
|
||
| *************** | ||
| Fixture Usage | ||
| *************** | ||
|
|
||
| To use the fixture, simply include ``log_metric`` as an argument in your test function. It provides a callable that you can use to log your metrics. | ||
|
|
||
| .. code:: python | ||
|
|
||
| def test_my_app(log_metric): | ||
| # ... test code ... | ||
| boot_time = 123.45 # measured boot time | ||
| log_metric("boot_time", boot_time, target="esp32", sdk="v5.1") | ||
|
|
||
| *************** | ||
| Output Format | ||
| *************** | ||
|
|
||
| The metrics are written to the file specified by ``--metric-path`` in the Prometheus text-based format. Each line represents a single metric. | ||
|
|
||
| Example output in ``output/metrics.txt``: | ||
|
|
||
| .. code:: text | ||
|
|
||
| boot_time{target="esp32",sdk="v5.1"} 123.45 | ||
|
|
||
| If ``--metric-path`` is not provided, the ``log_metric`` function will do nothing and issue a ``UserWarning``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
elif os.path.dirname(metric_path)will be False for paths in the current directory (e.g., 'metrics.txt'), which causes the directory creation to be skipped. This logic should handle the case where the file path is relative and has a non-empty directory component. Consider checking ifos.path.dirname(metric_path)is not empty and that directory does not already exist, or use a simpler approach like unconditionally creating the directory if it's not empty.