Skip to content

Commit f3d3648

Browse files
authored
Load timer (#405)
* add check point timer module * updated main to se checkpoint timer * cp armbian-config to tools/armbian-config-dev * fix style * Delete tools/armbian-config-dev remove redundant file * fix conflict * part fix conflict * update * rfine set_checkpoint show to use for debug * rm consept * fix typo * Added alias/helper `checkpoint` with a debug option. * added checkpoint to --api --cmd * fix style * explore testcase error * revert test case * explore 2 * Update MAN001.conf revert module_cockpit test case
1 parent 6d38a19 commit f3d3648

File tree

2 files changed

+141
-22
lines changed

2 files changed

+141
-22
lines changed

bin/armbian-config

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
#!/bin/bash
22

33
# initializes the terminal from TERM if stdin is a terminal
4-
[[ -t 0 ]] && tput init
5-
6-
# Language-based variable assignment for script directory path
7-
# This serves as a Rosetta Stone for developers,
8-
# allowing them to use the variable name they are most comfortable with.
9-
4+
unset UXMODE
5+
[[ -t 0 && "$1" =~ ^(|--cmd|--help)$ ]] && UXMODE="true" && tput init
106
# allows CTRL c to exit
117
trap "exit" INT TERM
8+
129
[[ $EUID != 0 ]] && exec sudo "$0" "$@"
1310
#
1411
# Get the script directory
1512
script_dir="$(dirname "$0")"
1613

14+
declare -A module_options
15+
16+
# Load the initialize modules
17+
source "$script_dir/../lib/armbian-config/config.initialize.sh"
18+
19+
# Start loading messages
20+
set_checkpoint start
21+
set_checkpoint mark "Initializing script"
22+
1723
[[ -d "$script_dir/../tools" ]] && tools_dir="$script_dir/../tools"
1824
[[ ! -d "$script_dir/../lib" && -n "$tools_dir" ]] && die -e "Please run\nbash "$tools_dir/config-assemble.sh" to build the lib directory\n"
1925

@@ -29,32 +35,33 @@ json_file="$lib_dir/config.jobs.json"
2935
# Load The Bash procedure Objects
3036
json_data=$(<"$json_file")
3137

32-
#
33-
# Prepare the module options array
34-
declare -A module_options
3538

36-
#
3739
# Load configng core functions and module options array
3840

3941
source "$lib_dir/config.functions.sh"
4042
set_runtime_variables
41-
check_distro_status
42-
echo "Loaded Runtime variables..." #| show_infobox ;
43-
#set_newt_colors 2
44-
echo "Loaded Dialog..." #| show_infobox ;
43+
set_checkpoint mark "Loaded Runtime variables..."
44+
# checks for supported os
45+
set_checkpoint mark "$(check_distro_status)"
46+
47+
set_checkpoint mark "Loaded Dialog..."
4548
source "$lib_dir/config.docs.sh"
46-
echo "Loaded Docs..." #| show_infobox ;
49+
set_checkpoint mark "Loaded Docs..."
4750
source "$lib_dir/config.system.sh"
48-
echo "Loaded System helpers..." #| show_infobox ;
51+
set_checkpoint mark "Loaded System helpers..."
4952
source "$lib_dir/config.network.sh"
50-
echo "Loaded Network helpers..." #| show_infobox ;
53+
set_checkpoint mark "Loaded Network helpers..."
5154
source "$lib_dir/config.software.sh"
52-
echo "Loaded Software helpers..." #| show_infobox ;
55+
set_checkpoint mark "Loaded Software helpers..."
5356
#
5457
# Loads the variables from beta armbian-config for runtime handling
5558

5659
source "$lib_dir/config.runtime.sh"
57-
echo "Loaded Runtime conditions..." #| show_infobox ;
60+
set_checkpoint mark "Loaded Runtime conditions..."
61+
62+
# usage: sudo DEBUG=1 ./config-time/bin/armbian-config $@
63+
# place one in a section with a quoted "about message"
64+
[[ -n "$DEBUG" ]] && set_checkpoint mark "DEBUG Messages on" ;
5865

5966
case "$1" in
6067
"--help")
@@ -63,7 +70,7 @@ case "$1" in
6370
echo ""
6471
else
6572
echo "Usage: armbian-config --[option]
66-
Options:
73+
Options:
6774
--help [category] Use [category] to filter specific menu options.
6875
--cmd [option] Run a command from the menu (simple)
6976
--api [option] Run a helper command (advanced)
@@ -82,6 +89,7 @@ case "$1" in
8289
;;
8390

8491
"--cmd")
92+
checkpoint debug "Starting Command line UX option"
8593
INPUTMODE="cmd"
8694
case "$2" in
8795
""|"help")
@@ -90,11 +98,13 @@ case "$1" in
9098
*)
9199
cmd=$(sanitize "$2") || die
92100
execute_command "$cmd"
101+
checkpoint debug "Starting Command line options UX"
93102
;;
94103
esac
95104
;;
96105

97106
"--api")
107+
checkpoint debug "Starting --api options"
98108
case "$2" in
99109
""|"help")
100110
see_use
@@ -103,6 +113,7 @@ case "$1" in
103113
fn=$(sanitize "$2") || die
104114
shift 2
105115
"$fn" "$@"
116+
checkpoint debug "Exiting --api"
106117
;;
107118
esac
108119
;;
@@ -145,9 +156,13 @@ case "$1" in
145156
*)
146157
# Generate the top menu with the modified Object data
147158
set_colors 4
159+
checkpoint debug "Starting Text User Interface (TUI)"
148160
generate_top_menu "$json_data"
149-
150-
# Show about this tool on exit
161+
checkpoint debug "Exiting TUI"
162+
# Show about this tool on exit
151163
about_armbian_configng
164+
152165
;;
153166
esac
167+
168+
[[ -n "$DEBUG" && -n $tools_dir ]] && set_checkpoint show
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module_options+=(
2+
["set_checkpoint,author"]="@armbian"
3+
["set_checkpoint,maintainer"]="@igorpecovnik"
4+
["set_checkpoint,feature"]="set_checkpoint"
5+
["set_checkpoint,example"]="help start mark stop show"
6+
["set_checkpoint,desc"]="Helper module for timing code execution"
7+
["set_checkpoint,status"]=""
8+
["set_checkpoint,doc_link"]=""
9+
["set_checkpoint,group"]="Development"
10+
["set_checkpoint,port"]=""
11+
["set_checkpoint,arch"]="x86-64 arm64"
12+
)
13+
14+
module_options+=(
15+
["debug_checkpoint,author"]="@armbian"
16+
["debug_checkpoint,maintainer"]="@igorpecovnik"
17+
["debug_checkpoint,feature"]="debug_checkpoint"
18+
["debug_checkpoint,example"]="help start mark stop show"
19+
["debug_checkpoint,desc"]="Helper module for debug info"
20+
["debug_checkpoint,status"]=""
21+
["debug_checkpoint,doc_link"]=""
22+
["debug_checkpoint,group"]="Development"
23+
["debug_checkpoint,port"]=""
24+
["debug_checkpoint,arch"]="x86-64 arm64"
25+
)
26+
#
27+
# Function to manage timer with multiple checkpoints
28+
function set_checkpoint() {
29+
case "$1" in
30+
help)
31+
echo "Usage: set_checkpoint <start|stop|mark|show> [description] [show]"
32+
echo "Commands:"
33+
echo " start Start the timer."
34+
echo " stop Stop the timer."
35+
echo " mark [description] [time] Mark a checkpoint with an optional description and an optional flag to show the output."
36+
echo " show Show the total elapsed time and checkpoints."
37+
;;
38+
start)
39+
set_checkpoint_START=$(date +%s)
40+
set_checkpoint_CHECKPOINTS=()
41+
set_checkpoint_DESCRIPTIONS=()
42+
set_checkpoint_PREV=$set_checkpoint_START
43+
;;
44+
stop)
45+
set_checkpoint_STOP=$(date +%s)
46+
;;
47+
mark)
48+
if [[ "$UXMODE" == "true" || -n "$DEBUG" ]]; then
49+
local checkpoint_time=$(date +%s)
50+
local checkpoint_duration=$((checkpoint_time - set_checkpoint_PREV))
51+
set_checkpoint_PREV=$checkpoint_time
52+
set_checkpoint_CHECKPOINTS+=($checkpoint_time)
53+
set_checkpoint_DESCRIPTIONS+=("$2")
54+
local count=${#set_checkpoint_DESCRIPTIONS[@]}
55+
# No debug UX mode debug level 1 defaulted here
56+
[[ -z "$DEBUG" ]] && printf "%-30s %10d seconds\n" "$2 " "${checkpoint_duration}"
57+
58+
# debug level 2 shows all message
59+
[[ "$DEBUG" -eq 2 ]] && printf "%-30s %10d seconds\n" "$2 " "${checkpoint_duration}"
60+
fi
61+
;;
62+
show)
63+
[[ -z "$set_checkpoint_STOP" ]] && set_checkpoint stop
64+
if [[ -n "$set_checkpoint_START" && -n "$set_checkpoint_STOP" ]]; then
65+
set_checkpoint_DURATION=$((set_checkpoint_STOP - set_checkpoint_START))
66+
printf "%-30s: %d seconds\n" "Total elapsed time" "${set_checkpoint_DURATION}"
67+
68+
local previous_time=$set_checkpoint_START
69+
for i in "${!set_checkpoint_CHECKPOINTS[@]}"; do
70+
local checkpoint_time=${set_checkpoint_CHECKPOINTS[$i]}
71+
local checkpoint_duration=$((checkpoint_time - previous_time))
72+
local description=${set_checkpoint_DESCRIPTIONS[$i]}
73+
printf "%-30s: %d seconds\n" "${description:-Checkpoint $((i+1))}" "${checkpoint_duration}"
74+
previous_time=$checkpoint_time
75+
done
76+
else
77+
echo "Timer has not been started and stopped properly."
78+
fi
79+
;;
80+
*)
81+
echo "Usage: set_checkpoint <start|stop|mark|show> [description]"
82+
;;
83+
esac
84+
}
85+
86+
# Example usage
87+
# set_checkpoint aluse with debugging
88+
checkpoint() {
89+
case "$1" in
90+
start|stop|mark|show)
91+
set_checkpoint "$1" "$2"
92+
;;
93+
debug)
94+
[[ -n "$DEBUG" && -n $tools_dir ]] && set_checkpoint mark "$2"
95+
;;
96+
help)
97+
set_checkpoint help
98+
echo " debug DEBUG checkpoint message"
99+
;;
100+
*)
101+
echo "Invalid command. Use: help, start, stop, mark, debug, show"
102+
;;
103+
esac
104+
}

0 commit comments

Comments
 (0)