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 May 3, 2022
1 parent b4b37e2 commit d74c92b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
18 changes: 18 additions & 0 deletions testprog/testprog.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 6 additions & 0 deletions tests/bash/comp-tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ _completionTests_verifyCompletion "testprog prefix nospace b" "bear bearpaw" nos
_completionTests_verifyCompletion "testprog prefix nofile b" "bear bearpaw" nofile
unset COMP_TYPE

# Measure speed of execution without descriptions (for both v1 and v2)
_completionTests_timing "testprog manycomps " 1.0

# Test descriptions of bash v2
if [ "$BASHCOMP_VERSION" = bash2 ]; then

Expand Down Expand Up @@ -269,6 +272,9 @@ bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nofile b" "bear
bearpaw" nofile
unset COMP_TYPE

# Measure speed of execution with descriptions
_completionTests_timing "testprog manycomps " 2.0
fi

# This must be the last call. It allows to exit with an exit code
Expand Down
4 changes: 2 additions & 2 deletions tests/test-all.sh
Original file line number Diff line number Diff line change
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 d74c92b

Please sign in to comment.