#!/usr/bin/env bash set -e test_target=$1 if [[ -z "${test_target}" ]]; then echo "error: must provide a single go_test target as an argument, e.g. //path/to/my/test:my_test" exit fi bazel query "kind(test, ${test_target})" name=$(echo "${test_target}" | cut -d':' -f2) echo "${name}" bazel run "${test_target}" -- -test.run="Benchmark.*" -test.bench=. -test.cpuprofile="${name}.cpuprofile.out" -test.blockprofile="${name}.blockprofile.out" -test.benchtime=1x # Choose which profile to open # Available profiles are listed in https://go.dev/doc/diagnostics # Also see: go help testflag # profile=cpuprofile profile=blockprofile bin=$(bazel info bazel-bin) package=$(bazel query "${test_target}" --output package) prof_file="${bin}/${package}/${name}_/${name}.runfiles/edge_infra/${package}/${name}.${profile}.out" echo "opening pprof file ${prof_file}" bazel run @io_bazel_rules_go//go -- tool pprof -http :8080 "${prof_file}"