Skip to content
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

Load timer #405

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 31 additions & 17 deletions bin/armbian-config
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@
# initializes the terminal from TERM if stdin is a terminal
[[ -t 0 ]] && tput init

# Language-based variable assignment for script directory path
# This serves as a Rosetta Stone for developers,
# allowing them to use the variable name they are most comfortable with.

# allows CTRL c to exit
trap "exit" INT TERM
[[ $EUID != 0 ]] && exec sudo "$0" "$@"
#
# Get the script directory
script_dir="$(dirname "$0")"

declare -A module_options

# Conditional settings based on arguments
# for interactive vs non-interactive modes
if [[ "$1" == "--api" || "$1" == "--doc" ]]; then
UXMODE="false"
elif [[ -z "$1" || "$1" == "--cmd" || "$1" == "--help" ]]; then
UXMODE="true"
fi

# Additional check for interactive vs non-interactive mode
if [[ ! -t 0 ]]; then
UXMODE="false"
fi
Comment on lines +15 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW can be replaced with:

unset UXMODE
[[ -t 0 && "$1" =~ ^(|--cmd|--help)$ ]] && UXMODE="true"


# Load the initialize modules
source "$script_dir/../lib/armbian-config/config.initialize.sh"

set_checkpoint start
set_checkpoint mark "Initializing script" true

[[ -d "$script_dir/../tools" ]] && tools_dir="$script_dir/../tools"
[[ ! -d "$script_dir/../lib" && -n "$tools_dir" ]] && die -e "Please run\nbash "$tools_dir/config-assemble.sh" to build the lib directory\n"

Expand All @@ -29,32 +46,29 @@ json_file="$lib_dir/config.jobs.json"
# Load The Bash procedure Objects
json_data=$(<"$json_file")

#
# Prepare the module options array
declare -A module_options

#
# Load configng core functions and module options array

source "$lib_dir/config.functions.sh"
set_runtime_variables
check_distro_status
echo "Loaded Runtime variables..." #| show_infobox ;
#set_newt_colors 2
echo "Loaded Dialog..." #| show_infobox ;
set_checkpoint mark "Loaded Runtime variables..." true
# checks for supported os
set_checkpoint mark "$(check_distro_status)" true

set_checkpoint mark "Loaded Dialog..." true
source "$lib_dir/config.docs.sh"
echo "Loaded Docs..." #| show_infobox ;
set_checkpoint mark "Loaded Docs..." true
source "$lib_dir/config.system.sh"
echo "Loaded System helpers..." #| show_infobox ;
set_checkpoint mark "Loaded System helpers..." true
source "$lib_dir/config.network.sh"
echo "Loaded Network helpers..." #| show_infobox ;
set_checkpoint mark "Loaded Network helpers..." true
source "$lib_dir/config.software.sh"
echo "Loaded Software helpers..." #| show_infobox ;
set_checkpoint mark "Loaded Software helpers..." true
#
# Loads the variables from beta armbian-config for runtime handling

source "$lib_dir/config.runtime.sh"
echo "Loaded Runtime conditions..." #| show_infobox ;
echo "Loaded Runtime conditions..." true

case "$1" in
"--help")
Expand Down
67 changes: 67 additions & 0 deletions tools/modules/initialize/message_checkpoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

module_options+=(
["set_checkpoint,author"]="@armbian"
["set_checkpoint,maintainer"]="@igorpecovnik"
["set_checkpoint,feature"]="set_checkpoint"
["set_checkpoint,example"]="help start mark stop show"
["set_checkpoint,desc"]="Helper module for timing code execution"
["set_checkpoint,status"]=""
["set_checkpoint,doc_link"]=""
["set_checkpoint,group"]="Development"
["set_checkpoint,port"]=""
["set_checkpoint,arch"]="x86-64 arm64"
)
#
# Function to manage timer with multiple checkpoints
function set_checkpoint() {
case "$1" in
help)
echo "Usage: set_checkpoint <start|stop|mark|show> [description] [show]"
echo "Commands:"
echo " start Start the timer."
echo " stop Stop the timer."
echo " mark [description] [time] Mark a checkpoint with an optional description and an optional flag to show the output."
echo " show Show the total elapsed time and checkpoints."
;;
Comment on lines +18 to +25
Copy link
Collaborator

@dimitry-ishenko dimitry-ishenko Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indent? Same for other cases.

start)
set_checkpoint_START=$(date +%s)
set_checkpoint_CHECKPOINTS=()
set_checkpoint_DESCRIPTIONS=()
set_checkpoint_PREV=$set_checkpoint_START
;;
stop)
set_checkpoint_STOP=$(date +%s)
;;
mark)
local checkpoint_time=$(date +%s)
local checkpoint_duration=$((checkpoint_time - set_checkpoint_PREV))
set_checkpoint_PREV=$checkpoint_time
set_checkpoint_CHECKPOINTS+=($checkpoint_time)
set_checkpoint_DESCRIPTIONS+=("$2")
local count=${#set_checkpoint_DESCRIPTIONS[@]}
if [[ "$3" == "true" && $UXMODE == "true" ]]; then
printf "%-30s %10d seconds\n" "$2:" "${checkpoint_duration}"
fi
Comment on lines +36 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but if you do this, you can avoid unnecessary calculations in non-UX mode:

	mark)
		if [[ "$UXMODE" == "true" ]]; then
			local checkpoint_time=$(date +%s)
			local checkpoint_duration=$((checkpoint_time - set_checkpoint_PREV))
			set_checkpoint_PREV=$checkpoint_time
			set_checkpoint_CHECKPOINTS+=($checkpoint_time)
			set_checkpoint_DESCRIPTIONS+=("$2")
			local count=${#set_checkpoint_DESCRIPTIONS[@]}
			[[ "$3" == "true" ]] && printf "%-30s %10d seconds\n" "$2:" "${checkpoint_duration}"
		fi
	;;

;;
show)
if [[ -n "$set_checkpoint_START" && -n "$set_checkpoint_STOP" ]]; then
set_checkpoint_DURATION=$((set_checkpoint_STOP - set_checkpoint_START))
printf "%-30s: %d seconds\n" "Total elapsed time" "${set_checkpoint_DURATION}"

local previous_time=$set_checkpoint_START
for i in "${!set_checkpoint_CHECKPOINTS[@]}"; do
local checkpoint_time=${set_checkpoint_CHECKPOINTS[$i]}
local checkpoint_duration=$((checkpoint_time - previous_time))
local description=${set_checkpoint_DESCRIPTIONS[$i]}
printf "%-30s: %d seconds\n" "${description:-Checkpoint $((i+1))}" "${checkpoint_duration}"
previous_time=$checkpoint_time
Comment on lines +53 to +57
Copy link
Collaborator

@dimitry-ishenko dimitry-ishenko Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indent inside for-loop?

done
else
echo "Timer has not been started and stopped properly."
fi
;;
*)
echo "Usage: set_checkpoint <start|stop|mark|show> [description]"
;;
esac
}