diff --git a/CHANGELOG.md b/CHANGELOG.md index 38662be1..dc949e73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Enable parallel tests on Windows - Add `assert_not_called` - Improve `find_total_tests` performance +- Improve `clock` performance ## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23 diff --git a/src/clock.sh b/src/clock.sh index 5e851e95..51d4b913 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -1,9 +1,17 @@ #!/usr/bin/env bash function clock::now() { + if [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]]; then + local seconds microseconds + seconds=${EPOCHREALTIME%%.*} + microseconds=${EPOCHREALTIME##*.} + printf '%d\n' $((10#$seconds * 1000000000 + 10#$microseconds * 1000)) + return 0 + fi + if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then - return 0 + return 0 fi fi @@ -52,7 +60,7 @@ function clock::shell_time() { function clock::total_runtime_in_milliseconds() { end_time=$(clock::now) if [[ -n $end_time ]]; then - math::calculate "($end_time-$_START_TIME)/1000000" + printf '%d\n' $(((end_time - _START_TIME)/1000000)) else echo "" fi @@ -61,7 +69,7 @@ function clock::total_runtime_in_milliseconds() { function clock::total_runtime_in_nanoseconds() { end_time=$(clock::now) if [[ -n $end_time ]]; then - math::calculate "($end_time-$_START_TIME)" + printf '%d\n' $((end_time - _START_TIME)) else echo "" fi diff --git a/src/env.sh b/src/env.sh index 50cc5165..c140d854 100644 --- a/src/env.sh +++ b/src/env.sh @@ -79,15 +79,19 @@ function env::active_internet_connection() { function env::find_terminal_width() { local cols="" - if [[ -z "$cols" ]] && command -v stty > /dev/null; then + if command -v tput >/dev/null; then cols=$(tput cols 2>/dev/null) fi - if [[ -n "$TERM" ]] && command -v tput > /dev/null; then + + if [[ -n "$TERM" ]] && command -v stty >/dev/null; then cols=$(stty size 2>/dev/null | cut -d' ' -f2) fi - # Directly echo the value with fallback - echo "${cols:-100}" + if [[ -z "$cols" || "$cols" == "0" ]]; then + cols=100 + fi + + echo "$cols" } function env::print_verbose() { diff --git a/src/runner.sh b/src/runner.sh index a39e06ed..a26f266b 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -178,8 +178,8 @@ function runner::run_test() { exec 3>&- local end_time=$(clock::now) - local duration_ns=$(math::calculate "($end_time - $start_time) ") - local duration=$(math::calculate "$duration_ns / 1000000") + local duration_ns=$((end_time - start_time)) + local duration=$((duration_ns / 1000000)) if env::is_verbose_enabled; then if env::is_simple_output_enabled; then diff --git a/tests/unit/clock_test.sh b/tests/unit/clock_test.sh index 5ab0d5ba..81edb0ff 100644 --- a/tests/unit/clock_test.sh +++ b/tests/unit/clock_test.sh @@ -4,10 +4,12 @@ __ORIGINAL_OS="" function set_up_before_script() { __ORIGINAL_OS=$_OS + unset EPOCHREALTIME } function tear_down_after_script() { export _OS=$__ORIGINAL_OS + unset EPOCHREALTIME } function mock_non_existing_fn() { @@ -20,6 +22,12 @@ function test_now_with_perl() { assert_same "1720705883457" "$(clock::now)" } +function test_now_with_epochrealtime() { + EPOCHREALTIME="12345.678901" + + assert_same "12345678901000" "$(clock::now)" +} + function test_now_on_linux_unknown() { mock_unknown_linux_os mock perl mock_non_existing_fn diff --git a/tests/unit/directory_test.sh b/tests/unit/directory_test.sh index eeb26046..4716ed84 100644 --- a/tests/unit/directory_test.sh +++ b/tests/unit/directory_test.sh @@ -111,7 +111,7 @@ function test_unsuccessful_assert_is_directory_readable_when_a_file_is_given() { } function test_unsuccessful_assert_is_directory_readable_without_execution_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi @@ -126,7 +126,7 @@ function test_unsuccessful_assert_is_directory_readable_without_execution_permis } function test_unsuccessful_assert_is_directory_readable_without_read_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi @@ -141,7 +141,7 @@ function test_unsuccessful_assert_is_directory_readable_without_read_permission( } function test_successful_assert_is_directory_not_readable_without_read_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi @@ -152,7 +152,7 @@ function test_successful_assert_is_directory_not_readable_without_read_permissio } function test_successful_assert_is_directory_not_readable_without_execution_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi @@ -178,7 +178,7 @@ function test_successful_assert_is_directory_writable() { } function test_unsuccessful_assert_is_directory_writable() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi @@ -202,7 +202,7 @@ function test_unsuccessful_assert_is_directory_writable_when_a_file_is_given() { } function test_successful_assert_is_directory_not_writable() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$EUID" -eq 0 || "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then return fi