diff --git a/aurornis/__init__.py b/aurornis/__init__.py index ca60b04..c447a58 100644 --- a/aurornis/__init__.py +++ b/aurornis/__init__.py @@ -5,7 +5,7 @@ from sys import platform from os import environ -from datetime import datetime +from time import perf_counter_ns from .model import CommandResult @@ -34,12 +34,10 @@ def run( >>> c.successful False - You can also check the execution time of your command. - The object provides two values to facilitate your tests, one in milliseconds: - >>> assert c.exec_time_ms < 1000 - - and one in microseconds: - >>> assert c.exec_time_us < 1000000 + You can also check the execution time of your command thanks to three properties provided by the object: + >>> assert c.exec_time_ms < 1000 # in millisecond + >>> assert c.exec_time_us < 1000000 # in microseconds + >>> assert c.exec_time_ns < 1000000000 # in nanoseconds You can get the text returned to the standard output and error: >>> c.stdout @@ -128,7 +126,7 @@ def _get_execution_environment( else: input = None - start_time = datetime.now() + start_time = perf_counter_ns() process = subprocess.Popen( command, @@ -138,6 +136,7 @@ def _get_execution_environment( stdin=subprocess.PIPE, ) stdout, stderr = process.communicate(input) + exec_time = perf_counter_ns() - start_time stdout = stdout.decode() stderr = stderr.decode() @@ -154,8 +153,6 @@ def _get_execution_environment( stdout = stdout.replace("\r\n", "\n") stderr = stderr.replace("\r\n", "\n") - exec_time = (datetime.now() - start_time).microseconds - if remove_colors: stdout, stderr = _remove_colors(stdout), _remove_colors(stderr) diff --git a/aurornis/model.py b/aurornis/model.py index 140c288..9709943 100644 --- a/aurornis/model.py +++ b/aurornis/model.py @@ -12,13 +12,13 @@ def __init__( return_code: int, stdout: str, stderr: str, - exec_time_microseconds: int, + exec_time_nanoseconds: int, ): self._command = command self._return_code = return_code self._stdout = stdout self._stderr = stderr - self._exec_time_microseconds = exec_time_microseconds + self._exec_time_nanoseconds = exec_time_nanoseconds @property def command(self) -> [str]: @@ -49,23 +49,35 @@ def stderr(self) -> str: """The text that has been returned by the command in the error output""" return self._stderr + @property + def exec_time_ns(self) -> int: + """The time of execution of the command in nanoseconds + + This can be useful if you have huge constraints regarding the execution time. + Note that all systems don't support such precision. + + If you don't need a so precise value, you can use the ``exec_time_us`` or ``exec_time_ms`` properties instead. + """ + return self._exec_time_nanoseconds + @property def exec_time_us(self) -> int: """The time of execution of the command in microseconds - This can be useful if you have big constraints and want to guaranty the time execution. - If you don't need a so precise value, you can use the `exec_time_ms` property instead. + This can be useful if you have big constraints regarding the execution time. + If you need a more precise value, you can use the ``exec_time_ns`` property instead. + If you don't need a so precise value, you can use the ``exec_time_ms`` property instead. """ - return self._exec_time_microseconds + return int(self.exec_time_ns / 1000) @property def exec_time_ms(self) -> int: """The time of execution of the command in milliseconds - This can be useful if you have big constraints and want to guaranty the time execution. - If you need a more precise value, you can use the `exec_time_us` property instead. + This can be useful if you have some constraints regarding the execution time. + If you need a more precise value, you can use the ``exec_time_us`` or ``exec_time_ms`` properties instead. """ - return int(self._exec_time_microseconds / 1000) + return int(self.exec_time_us / 1000) @deprecated( deprecated_in="1.5.0",