4
0

profile.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #! /usr/bin/env bash
  2. # NAME
  3. # profile.sh - generate gitleaks profile data
  4. #
  5. # USAGE
  6. # profile.sh <gitleaks-path> <benchmark-repo-path>
  7. #
  8. # DESCRIPTION
  9. # Generates profile data for tuning gitleaks performance under ./profile/<timestamp>
  10. #
  11. # Options:
  12. # <gitleaks-path> - gitleaks binary to profile
  13. # <benchmark-repo-path> - git repo to run profile against
  14. #
  15. # SEE ALSO
  16. # Dave Cheney GopherCon 2019 talk on go profiling:
  17. #
  18. # https://www.youtube.com/watch?v=nok0aYiGiYA
  19. #
  20. set -euo pipefail
  21. gitleaks_path="$1"
  22. test_repo_path="$2"
  23. base_scan_cmd="${gitleaks_path} --exit-code=0 --max-decode-depth 8"
  24. base_profile_dir="profile/$(date +%s)"
  25. log() {
  26. echo >&2 "$@"
  27. }
  28. log '========================================================================'
  29. log 'generating profile data'
  30. log '------------------------------------------------------------------------'
  31. # Warm up the fs and also get benchmark data
  32. for scan_mode in dir git
  33. do
  34. profile_dir="${base_profile_dir}/${scan_mode}"
  35. scan_cmd="${base_scan_cmd} ${scan_mode} ${test_repo_path}"
  36. mkdir -p "${profile_dir}"
  37. echo "- mode: ${scan_mode}"
  38. # include hyperfine benchmrak results if hyperfine is installed
  39. if command -v hyperfine > /dev/null
  40. then
  41. export_path="${profile_dir}/benchmark.json"
  42. echo " benchmark:"
  43. echo " tool: hyperfine"
  44. hyperfine -w 3 --export-json "${export_path}" "${scan_cmd}" &> /dev/null
  45. echo " path: ${export_path}"
  46. # Show the results if we can :D
  47. if command -v yq > /dev/null
  48. then
  49. echo " results:"
  50. yq -P -oy \
  51. '.results[] | pick(["mean","stddev","median","user","system","min","max"])' \
  52. ${export_path} | sed 's/^/ /g'
  53. else
  54. echo " view: ${PAGER:-less} ${export_path}"
  55. fi
  56. fi
  57. # generate profile data
  58. echo " profile:"
  59. for profile_mode in cpu mem trace
  60. do
  61. # Generate diagnostics data
  62. ${scan_cmd} --diagnostics=$profile_mode --diagnostics-dir="${profile_dir}" &> /dev/null
  63. profile_file="$(find "${profile_dir}" -type f -name "${profile_mode}*")"
  64. echo " - mode: ${profile_mode}"
  65. echo " path: ${profile_file}"
  66. if [[ "${profile_mode}" = "trace" ]]
  67. then
  68. echo " view: go tool trace ${profile_file}"
  69. else
  70. echo " view: go tool pprof -http=localhost: ${gitleaks_path} ${profile_file}"
  71. fi
  72. done
  73. done
  74. log '------------------------------------------------------------------------'
  75. log "results in: ${base_profile_dir}"
  76. log '========================================================================'