From c678ba6de2112cdc07bc475bb34ce2413f966b36 Mon Sep 17 00:00:00 2001 From: gmega Date: Mon, 23 Jun 2025 15:40:21 -0300 Subject: [PATCH] feat: add default options to Codex, enable metrics on experiment scope --- src/codex.bash | 31 +++++++++++++++++++++--------- src/experiment.bash | 17 ++++++++++++++-- src/utils.bash | 5 +++++ test/test_codex.bats | 12 ++++++++++++ test/test_helper/common_setup.bash | 5 +++-- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/codex.bash b/src/codex.bash index 2ae1a7d..3e980b9 100644 --- a/src/codex.bash +++ b/src/codex.bash @@ -40,6 +40,8 @@ _cdx_timing_prefix="" # Log file where timings are aggregated _cdx_timing_log="/dev/null" +_cdx_defaultopts=() + # Base ports and timeouts _cdx_base_api_port=8080 _cdx_base_disc_port=8190 @@ -64,28 +66,39 @@ _cdx_metrics_port() { echo $((_cdx_base_metrics_port + node_index)) } +cdx_add_defaultopts() { + _cdx_defaultopts+=("$@") +} + +cdx_clear_defaultopts() { + _cdx_defaultopts=() +} + cdx_cmdline() { - local node_index spr cdx_cmd="${_cdx_binary} --nat:none" + local node_index spr cdx_cmd="${_cdx_binary} --nat:none" opts=("$@") - node_index="$1" - shift + opts+=("${_cdx_defaultopts[@]}") - while [[ "$#" -gt 0 ]]; do - case "$1" in + node_index="${opts[0]}" + shift_arr opts + + while [[ "${#opts[@]}" -gt 0 ]]; do + opt="${opts[0]}" + case "$opt" in --bootstrap-node) - shift - spr="$1" + shift_arr opts + spr="${opts[0]}" cdx_cmd="${cdx_cmd} --bootstrap-node=$spr" ;; --metrics) cdx_cmd="${cdx_cmd} --metrics --metrics-port=$(_cdx_metrics_port "$node_index") --metrics-address=0.0.0.0" ;; *) - echoerr "Error: unknown option $1" + echoerr "Error: unknown option $opt" return 1 ;; esac - shift + shift_arr opts done if [[ "$node_index" -gt 0 && -z "$spr" ]]; then diff --git a/src/experiment.bash b/src/experiment.bash index 888281a..47dfa7b 100644 --- a/src/experiment.bash +++ b/src/experiment.bash @@ -1,12 +1,21 @@ #!/usr/bin/env bash +set -o pipefail LIB_SRC=${LIB_SRC:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)} # shellcheck source=./src/utils.bash source "${LIB_SRC}/utils.bash" +# shellcheck source=./src/codex.bash +source "${LIB_SRC}/codex.bash" + +_experiment_type="" +_experiment_id="" exp_start() { - _experiment_type="$1" + local experiment_id experiment_type="$1" + + experiment_id="${experiment_type}-$(date +%s)-${RANDOM}" || return 1 + # FIXME: this is pretty clumsy/confusing. We're "initing" the # harness just so it sets the base output folder, and then # "initing" it again. @@ -14,5 +23,9 @@ exp_start() { clh_init fi - clh_init "${_clh_output}/${_experiment_type}-$(date +%s)-${RANDOM}" || return 1 + _experiment_id="${experiment_id}" + _experiment_type="${experiment_type}" + + clh_init "${_clh_output}/${_experiment_id}" || return 1 + cdx_add_defaultopts "--metrics" } diff --git a/src/utils.bash b/src/utils.bash index 7ca14d2..3c85390 100644 --- a/src/utils.bash +++ b/src/utils.bash @@ -25,6 +25,11 @@ echoerr() { echo "$@" >&2 } +shift_arr () { + local -n arr_ref="$1" + arr_ref=("${arr_ref[@]:1}") +} + sha1() { sha1sum "$1" | cut -d ' ' -f 1 || return 1 } \ No newline at end of file diff --git a/test/test_codex.bats b/test/test_codex.bats index 8042248..1a25933 100755 --- a/test/test_codex.bats +++ b/test/test_codex.bats @@ -38,6 +38,18 @@ setup() { " --api-port=8080 --disc-port=8190 --log-level=INFO" } +@test "should allow setting of global default options" { + ! [[ "$(cdx_cmdline 0)" =~ "--metrics --metrics-port=8290 --metrics-address=0.0.0.0" ]] + + cdx_add_defaultopts "--metrics" + + [[ "$(cdx_cmdline 0)" =~ "--metrics --metrics-port=8290 --metrics-address=0.0.0.0" ]] + + cdx_clear_defaultopts + + ! [[ "$(cdx_cmdline 0)" =~ "--metrics --metrics-port=8290 --metrics-address=0.0.0.0" ]] +} + @test "should fail readiness check if node is not running" { refute cdx_ensure_ready 0 1 } diff --git a/test/test_helper/common_setup.bash b/test/test_helper/common_setup.bash index 07826b0..bdf994b 100644 --- a/test/test_helper/common_setup.bash +++ b/test/test_helper/common_setup.bash @@ -5,7 +5,8 @@ common_setup() { export LIB_SRC="${BATS_TEST_DIRNAME}/../src" - # shellcheck source=./src/clh - source "${LIB_SRC}/clh" + # shellcheck source=./src/utils.bash + source "${LIB_SRC}/utils.bash" + clh_init "${LIB_SRC}/../test_outputs" }