Skip to content

Commit

Permalink
[Perf] Display results with 4 significant figures (#25462)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored Aug 1, 2022
1 parent 8a03bff commit 07c4aaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
"ipconfiguration",
"ipconfigurations",
"iqmp",
"isclass",
"iscoroutine",
"iscoroutinefunction",
"iscsi",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import inspect
import logging
import math
import os
import pkgutil
import sys
Expand Down Expand Up @@ -207,8 +208,11 @@ async def _run_tests(self, title: str, duration: int) -> None:
seconds_per_operation = 1 / operations_per_second
weighted_average_seconds = total_operations / operations_per_second
self.logger.info(
"Completed {:,} operations in a weighted-average of {:,.2f}s ({:,.2f} ops/s, {:,.3f} s/op)".format(
total_operations, weighted_average_seconds, operations_per_second, seconds_per_operation
"Completed {:,} operations in a weighted-average of {}s ({} ops/s, {} s/op)".format(
total_operations,
self._format_number(weighted_average_seconds, 4),
self._format_number(operations_per_second, 4),
self._format_number(seconds_per_operation, 4)
)
)
else:
Expand All @@ -226,3 +230,31 @@ def _print_status(self, title):

self._operation_status_tracker = total_operations
self.logger.info("{}\t\t{}\t\t{:.2f}".format(current_operations, total_operations, average_operations))

def _format_number(self, value, min_significant_digits):
"""
Formats a number with a minimum number of significant digits.
Digits to the left of the decimal point are always significant.
This function has been ported from .NET for cross-language consistency.
Examples:
- _format_number(0, 4) -> "0.000"
- _format_number(12345, 4) -> "12,345"
- _format_number(1.2345, 4) -> "1.235"
- _format_number(0.00012345, 4) -> "0.0001235"
"""

# Special case since log(0) is undefined
if value == 0:
return ("{:." + str(min_significant_digits - 1) + "f}").format(value)

log = math.log10(abs(value))
significant_digits = max(math.ceil(log), min_significant_digits)

divisor = 10 ** (math.ceil(log) - significant_digits)
rounded = divisor * round(value / divisor)

decimals = max(0, significant_digits - math.floor(log) - 1)

return ("{:,." + str(decimals) + "f}").format(rounded)

0 comments on commit 07c4aaa

Please sign in to comment.