diff --git a/lib/common.sh b/lib/common.sh index a390283..1858d87 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -180,6 +180,43 @@ stop_timer () { fi } +#================================================= +# Resource metrics helpers +#================================================= +# Communication between the background thread and the main one +# is done via a shared memory. + +get_ram_usage() { + free -m | grep Mem | awk '{print $3}' +} + +metrics_background_thread() { + declare -A resources=( [ram]=0 ) + while true; do + ram_usage=$(get_ram_usage) + echo "$ram_usage" + if ((ram_usage > resources[ram])); then + resources[ram]=$ram_usage + fi + declare -p resources > "$TEST_CONTEXT/metrics_vars" + sleep 1 + done +} + +metrics_start() { + ram_usage_base=$(get_ram_usage) + metrics_background_thread & + metrics_background_thread_pid=$! +} + +metrics_stop() { + kill "$metrics_background_thread_pid" + source "$TEST_CONTEXT/metrics_vars" + + max_ram_usage_diff=$((resources[ram] - ram_usage_base)) + log_info "RAM usage for this test: ${max_ram_usage_diff}MB (total: ${resources[ram]}MB)" +} + #================================================= # Package check self-upgrade #================================================= diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 2d0e42e..fa448d8 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -111,6 +111,9 @@ TEST_LAUNCHER () { # And keep this value separately local global_start_timer=$starttime + # Start metrics measurement + metrics_start + current_test_id=$(basename $testfile | cut -d. -f1) current_test_infos="$TEST_CONTEXT/tests/$current_test_id.json" current_test_results="$TEST_CONTEXT/results/$current_test_id.json" @@ -168,6 +171,9 @@ TEST_LAUNCHER () { # End the timer for the test stop_timer one_test + # Stop metrics and show results + metrics_stop + LXC_STOP $LXC_NAME # Update the lock file with the date of the last finished test.