cyberark/secretless-broker

View on GitHub
bin/test_benchmarks

Summary

Maintainability
Test Coverage
#!/bin/bash

set -eo pipefail

project_dir=$PWD
auto_bench_file="docs/_includes/auto_benchmarks.html"

rm -f "$project_dir/test/bench.output"
touch "$project_dir/test/bench.output"

rm -f "$project_dir/$auto_bench_file"
touch "$project_dir/$auto_bench_file"

# test benchmarks in each directory
pushd test
  exit_status="0"
  for dir in */; do
    pushd "$dir" 2>/dev/null
      # Bail if the folder doesn't have a bench_test file
      # Assumes folder has a start file if it has a bench_test file
      if [[ ! -f bench_test.go ]]; then
        popd 2>/dev/null
        continue
      fi

      # set up the containers to run in their own namespace
      COMPOSE_PROJECT_NAME="$(basename "$PWD")_$(openssl rand -hex 3)"
      export COMPOSE_PROJECT_NAME

      # Start the needed prerequisites. Assumes that start pre-cleans the env
      ./start

      # Run the tests
      set +e
        ./test -b | tee -a "$project_dir/test/bench.output"
        last_status="$?"

        # Only save first failure exit code
        if [[ "$exit_status" -eq "0" && "$last_status" -ne "0" ]]; then
          echo "ERROR: Detected a failure in the runner for $dir!"
          exit_status="$last_status"
        fi
      set -e

      # Clean up
      if [[ -f ./stop ]]; then
        ./stop
      fi
    popd
  done

  # Format the benchmark output
  rm -f "$project_dir/test/bench.xml"
  docker run --rm \
    -v "$project_dir/test/:/secretless/test/output/" \
    secretless-dev \
    bash -exc "
      go install github.com/jstemmer/go-junit-report@latest
      cat ./test/output/bench.output | go-junit-report > ./test/output/bench.xml
    "
popd || true

# gather auto benchmark data for website

# container size
container_size=$(docker images -q \
  --filter=reference='cyberark/secretless-broker' \
   --format="{{ .Size }}")

echo '
<h3>Docker Image Size</h3>
<p>The Docker image size is auto-updated on this page with every build of the
project. You may also visit our
<a href="https://hub.docker.com/r/cyberark/secretless-broker/">DockerHub</a>.</p>' \
  | tee -a "$project_dir/$auto_bench_file"

echo "<p>Docker image size: ${container_size}</p>" \
  | tee -a "$project_dir/$auto_bench_file"

printf 'Container size: %s\n' "$container_size" \
  | tee -a "$project_dir/test/bench.output"

# lines of code count
echo '
<h2>Code Metrics</h2>
<h3>Lines of Code</h3>
<p>Code line counts were generated with
<a href="https:github.com/hhatto/gocloc">gocloc</a> and are auto-updated
with every build of this project.
<pre>' \
  | tee -a "$project_dir/$auto_bench_file"

docker run --rm \
  -v "$project_dir/docs/_includes/:/secretless/docs/_includes/" \
  secretless-dev \
  bash -exc "
    go install github.com/hhatto/gocloc/cmd/gocloc@latest
    gocloc . | tee -a ./${auto_bench_file}
  " \
  | tee -a "$project_dir/test/bench.output"

echo "</pre></p>" \
  | tee -a "$project_dir/$auto_bench_file"

exit $exit_status