From 91e09c314887d1014d6c42217c376d9f6dbdcc5d Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 30 Apr 2022 12:29:14 -0400 Subject: [PATCH] Add test of performance for bash completion Signed-off-by: Marc Khouzam --- testprog/testprog.go | 18 ++++++++++++++++++ tests/bash/comp-test-lib.bash | 15 +++++++++++++++ tests/bash/comp-tests.bash | 3 +++ tests/test-all.sh | 4 ++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/testprog/testprog.go b/testprog/testprog.go index 7f45add..c33c078 100644 --- a/testprog/testprog.go +++ b/testprog/testprog.go @@ -194,6 +194,23 @@ var dashArgCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) {}, } +// ====================================================== +// Command generates many completions. +// It can be used to test performance. +// ====================================================== +var manyCompsCmd = &cobra.Command{ + Use: "manycomps", + Short: "Outputs a thousand completions", + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var comps []string + for i := 0; i < 1000; i++ { + comps = append(comps, fmt.Sprintf("%[1]d-comp\tThis is comp %[1]d", i)) + } + return comps, cobra.ShellCompDirectiveDefault + }, + Run: func(cmd *cobra.Command, args []string) {}, +} + func setFlags() { rootCmd.Flags().String("customComp", "", "test custom comp for flags") rootCmd.RegisterFlagCompletionFunc("customComp", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -218,6 +235,7 @@ func main() { subDirCmd, errorCmd, dashArgCmd, + manyCompsCmd, ) prefixCmd.AddCommand( diff --git a/tests/bash/comp-test-lib.bash b/tests/bash/comp-test-lib.bash index f3dcb47..5ffdb80 100755 --- a/tests/bash/comp-test-lib.bash +++ b/tests/bash/comp-test-lib.bash @@ -135,6 +135,21 @@ _completionTests_sort() { fi } +# $1 - The completion to measure +# $2 - The maximum time to consider it an error +_completionTests_timing() { + TIMEFORMAT=%R + timing=$({ time { _completionTests_complete "$1" > /dev/null; } } 2>&1) + if (( $(echo "$timing > ${2}" | bc -l) )); then + _completionTests_TEST_FAILED=1 + echo -e "${RED}Processing 1000 completions took ${timing} seconds which is more than the ${2-0.1} seconds limit$NC" + return 1 + else + echo -e "${GREEN}Processing 1000 completions took ${timing} seconds which is less than the ${2-0.1} seconds limit$NC" + return 0 + fi +} + # Find the completion function associated with the binary. # $1 is the first argument of the line to complete which allows # us to find the existing completion function name. diff --git a/tests/bash/comp-tests.bash b/tests/bash/comp-tests.bash index 620764e..f1a5c37 100755 --- a/tests/bash/comp-tests.bash +++ b/tests/bash/comp-tests.bash @@ -271,6 +271,9 @@ bearpaw" nofile unset COMP_TYPE fi +# Measure speed of execution +_completionTests_timing "testprog manycomps " 0.5 + # This must be the last call. It allows to exit with an exit code # that reflects the final status of all the tests. _completionTests_exit \ No newline at end of file diff --git a/tests/test-all.sh b/tests/test-all.sh index ac2bb59..761c960 100755 --- a/tests/test-all.sh +++ b/tests/test-all.sh @@ -140,7 +140,7 @@ if [ $SHELL_TYPE = bash ]; then $CONTAINER_ENGINE build -t ${IMAGE} ${BASE_DIR} -f - <<- EOF FROM redhat/ubi8 - RUN yum install -y bash-completion which + RUN yum install -y bash-completion which bc WORKDIR /work COPY . . @@ -190,7 +190,7 @@ fi if [ "$(uname)" == "Darwin" ]; then echo echo "===================================" - echo "Attempting completion tests locally" + echo "Attempting $SHELL_TYPE completion tests locally" echo "===================================" make clean && make build