Skip to content

Commit

Permalink
Add test of performance for bash completion
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
  • Loading branch information
marckhouzam committed Apr 30, 2022
1 parent b4b37e2 commit 91e09c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
18 changes: 18 additions & 0 deletions testprog/testprog.go
Expand Up @@ -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) {
Expand All @@ -218,6 +235,7 @@ func main() {
subDirCmd,
errorCmd,
dashArgCmd,
manyCompsCmd,
)

prefixCmd.AddCommand(
Expand Down
15 changes: 15 additions & 0 deletions tests/bash/comp-test-lib.bash
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/bash/comp-tests.bash
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/test-all.sh
Expand Up @@ -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 . .
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 91e09c3

Please sign in to comment.