Skip to content

Improve clock performance #411

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions src/clock.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/clock_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/directory_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
Loading