TsukiNi22/ai-utils / skills/audit/benchmark
benchmark
VerifiedBenchmark and profile the current program (C / C++ first, also Rust and Python) with the tools available (perf stat / record / report, hyperfine, valgrind callgrind / massif, heaptrack, flame graphs), asking before installing a missing tool - optimized build with symbols in a copy, wall time, hardware counters, hot functions (self and inclusive), hot lines, memory - then the analysis of what is slow and why, each finding classified as a quick win or a complex change with its estimated gain, delivered as a PDF / Markdown report (report skill), and optionally the optimizations with a before / after comparison. Use whenever the user asks for a benchmark, a profiling, a performance analysis, what is slow, which function takes the time, or how to optimize a program.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 6.8 KB |
| requires.txt | markdown | 7 B |
| scripts/hotspots.pyexecutable | script | 8.7 KB |
| scripts/profile.shexecutable | script | 6.7 KB |
| scripts/tools.shexecutable | script | 2.4 KB |
scripts/profile.sh
View on GitHub#!/bin/bash
# Profile a program: optimized build with symbols (in a copy), wall time, hardware counters, hot functions,
# flame graph, and optionally exact instruction counts (callgrind) and memory (heaptrack / massif).
#
# Usage: profile.sh --cmd "<command>" [--project <dir>] [--cwd <dir>] [--build-type <type>] [--runs <n>]
# [--out <dir>] [--callgrind] [--memory] [--no-build]
#
# --cmd command to measure (run with bash from --cwd), ex: --cmd "xstyle -S -l cpp"
# --project CMake project rebuilt in a /tmp copy (-g -fno-omit-frame-pointer, uncommitted changes
# included); its build outputs come first in the PATH while --cmd runs (default: .)
# --no-build measure the command as it is (already built program, Python script...)
# --cwd directory of the run, where the inputs are (default: the project)
# --build-type CMake build type (default: Optimized, the -O3 mode of the user's projects; Release otherwise)
# --runs wall time runs (default: 10)
# --out output directory (default: /tmp/benchmark-<date>)
# --callgrind exact instruction counts per function (valgrind, 20-50x slower: small inputs)
# --memory allocations / peak heap (heaptrack, else valgrind massif)
#
# Outputs in --out: build.log, time.json|time.txt, stat.csv, self.txt, children.txt, flame.svg, callgrind.txt,
# memory.txt, info.txt. Then: python3 hotspots.py <out> --md <out>/hotspots.md
# Exit code: 0 measured, 2 build / run failure.
set -uo pipefail # no -e: the profiled program and the optional tools may fail (xstyle: ignore-file SH-STRICT)
CMD=""; PROJECT="."; CWD=""; BUILD_TYPE=""; RUNS=10; OUT=""; CALLGRIND=false; MEMORY=false; BUILD=true
while [[ $# -gt 0 ]]; do
case "$1" in
--cmd) CMD="$2"; shift ;;
--project) PROJECT="$2"; shift ;;
--cwd) CWD="$2"; shift ;;
--build-type) BUILD_TYPE="$2"; shift ;;
--runs) RUNS="$2"; shift ;;
--out) OUT="$2"; shift ;;
--callgrind) CALLGRIND=true ;;
--memory) MEMORY=true ;;
--no-build) BUILD=false ;;
-h|--help) sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) echo "Error: unknown argument '$1'" >&2; exit 2 ;;
esac
shift
done
[[ -z "$CMD" ]] && { echo "Error: --cmd is required" >&2; exit 2; }
PROJECT="$(cd "$PROJECT" && pwd)"
CWD="$(cd "${CWD:-$PROJECT}" && pwd)"
OUT="${OUT:-/tmp/benchmark-$(date +%Y%m%d-%H%M%S)}"
command mkdir -p "$OUT"
OUT="$(cd "$OUT" && pwd)"
# =========================
# Build (copy of the working tree)
# =========================
if $BUILD && [[ -f "$PROJECT/CMakeLists.txt" ]]; then
COPY="$(mktemp -d /tmp/benchmark-build-XXXXXX)"
echo "build: $COPY"
if command -v rsync > /dev/null 2>&1; then
rsync -a --exclude .git --exclude 'build*' --exclude 'cmake-build-*' "$PROJECT/" "$COPY/"
else
cp -r "$PROJECT/." "$COPY/" && rm -rf "$COPY/.git"
fi
if [[ -z "$BUILD_TYPE" ]]; then
grep -q "CONFIG:Optimized" "$PROJECT/CMakeLists.txt" && BUILD_TYPE="Optimized" || BUILD_TYPE="Release"
fi
FLAGS="-g -fno-omit-frame-pointer"
if ! cmake -S "$COPY" -B "$COPY/build" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DCMAKE_CXX_FLAGS="$FLAGS" -DCMAKE_C_FLAGS="$FLAGS" > "$OUT/build.log" 2>&1 \
|| ! cmake --build "$COPY/build" --parallel "$(nproc)" >> "$OUT/build.log" 2>&1; then
echo "Error: build failed, see $OUT/build.log" >&2
exit 2
fi
export PATH="$COPY:$COPY/build:$PATH"
fi
cd "$CWD" || exit 2
RUN="exec $CMD"
{
echo "command: $CMD"
echo "cwd: $CWD"
echo "project: $PROJECT"
echo "build type: ${BUILD_TYPE:-not rebuilt}"
echo "date: $(date '+%Y-%m-%d %H:%M')"
echo "cpu: $(sed -n 's/^model name\s*: //p' /proc/cpuinfo | head -1) ($(nproc) threads)"
echo "kernel: $(uname -r)"
} > "$OUT/info.txt"
# =========================
# Wall time
# =========================
echo "wall time: $RUNS runs"
if command -v hyperfine > /dev/null 2>&1; then
hyperfine -i --warmup 1 --runs "$RUNS" --export-json "$OUT/time.json" "$CMD" > "$OUT/hyperfine.txt" 2>&1
else
: > "$OUT/time.txt"
for _ in $(seq "$RUNS"); do
/usr/bin/time -f "%e %M" -a -o "$OUT/time.txt" bash -c "$RUN" > /dev/null 2>&1
done
fi
# =========================
# Hardware counters (user space) & sampling
# =========================
if command -v perf > /dev/null 2>&1; then
echo "perf stat"
# LC_NUMERIC=C: decimal point in the CSV (a comma breaks it); cpu-clock: one sample stream on hybrid CPUs
LC_NUMERIC=C perf stat -r 3 -x, -o "$OUT/stat.csv" \
-e task-clock,cycles:u,instructions:u,branches:u,branch-misses:u,cache-references:u,cache-misses:u \
-- bash -c "$RUN" > /dev/null 2>&1
echo "perf record"
if ! perf record -q -e cpu-clock -F 999 --call-graph dwarf,16384 -o "$OUT/perf.data" -- bash -c "$RUN" > /dev/null 2>&1 \
&& [[ ! -s "$OUT/perf.data" ]]; then
perf record -q -e cpu-clock -F 999 --call-graph fp -o "$OUT/perf.data" -- bash -c "$RUN" > /dev/null 2>&1
fi
LC_NUMERIC=C perf report -i "$OUT/perf.data" --stdio --no-children --sort symbol,dso -g none --percent-limit 0.3 --field-separator=$'\t' 2> /dev/null > "$OUT/self.txt"
LC_NUMERIC=C perf report -i "$OUT/perf.data" --stdio --children --sort symbol -g none --percent-limit 1 --field-separator=$'\t' 2> /dev/null > "$OUT/children.txt"
if command -v stackcollapse-perf.pl > /dev/null 2>&1 && command -v flamegraph.pl > /dev/null 2>&1; then
perf script -i "$OUT/perf.data" 2> /dev/null | stackcollapse-perf.pl | flamegraph.pl --title "$CMD" > "$OUT/flame.svg"
fi
fi
# =========================
# Exact counts & memory (optional, slow)
# =========================
if $CALLGRIND && command -v valgrind > /dev/null 2>&1; then
echo "callgrind"
valgrind --tool=callgrind --trace-children=yes --callgrind-out-file="$OUT/callgrind.%p" bash -c "$RUN" > /dev/null 2>&1
biggest="$(ls -S "$OUT"/callgrind.* 2> /dev/null | head -1)"
[[ -n "$biggest" ]] && callgrind_annotate --inclusive=no "$biggest" 2> /dev/null | head -60 > "$OUT/callgrind.txt"
fi
if $MEMORY; then
echo "memory"
/usr/bin/time -v bash -c "$RUN" 2> "$OUT/rss.txt" > /dev/null
if command -v heaptrack > /dev/null 2>&1; then
heaptrack -o "$OUT/heaptrack" bash -c "$RUN" > /dev/null 2>&1
heaptrack_print "$(ls "$OUT"/heaptrack.* | head -1)" 2> /dev/null | head -120 > "$OUT/memory.txt"
elif command -v valgrind > /dev/null 2>&1; then
valgrind --tool=massif --trace-children=yes --massif-out-file="$OUT/massif.%p" bash -c "$RUN" > /dev/null 2>&1
biggest="$(ls -S "$OUT"/massif.* 2> /dev/null | head -1)"
[[ -n "$biggest" ]] && ms_print "$biggest" 2> /dev/null | head -80 > "$OUT/memory.txt"
fi
fi
echo "out: $OUT"
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |