|
| 1 | +# Test & Benchmark Profiler |
| 2 | + |
| 3 | +Automatic hierarchical call tracing and performance profiling for Go tests and benchmarks in the Token SDK. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +cd tools/profiler |
| 9 | +./profile.sh BenchmarkValidatorTransfer |
| 10 | +``` |
| 11 | + |
| 12 | +## What It Does |
| 13 | + |
| 14 | +1. Copies your repo to `/tmp/profiler-<PID>` (your files are never touched) |
| 15 | +2. Finds your test/benchmark in `token/` directory |
| 16 | +3. Instruments all Go packages in `token/` |
| 17 | +4. Runs with profiling |
| 18 | +5. Saves results to current directory |
| 19 | +6. Cleans up automatically |
| 20 | + |
| 21 | +**Safety:** All work happens in `/tmp`. Your repository is never modified. |
| 22 | + |
| 23 | +## Example Output |
| 24 | + |
| 25 | +``` |
| 26 | +## Call Hierarchy |
| 27 | +
|
| 28 | +└── >>> (*Validator).VerifyTransfer [2.5s, 100.00%] |
| 29 | + ├── >>> (*Validator).verifyInputs [1.8s, 72.00%] |
| 30 | + │ └── >>> (*Verifier).Verify x5 [1.5s, 60.00%] |
| 31 | + └── (*Validator).verifyOutputs [0.7s, 28.00%] |
| 32 | +
|
| 33 | +## Top 20 Functions by Time |
| 34 | +
|
| 35 | +*Note: These functions are marked with `>>>` in the call tree above* |
| 36 | +
|
| 37 | +| Function | Total Time | % of Root | |
| 38 | +|----------|------------|-----------| |
| 39 | +| (*Validator).VerifyTransfer | 2.5s | 100.00% | |
| 40 | +| (*Validator).verifyInputs | 1.8s | 72.00% | |
| 41 | +| (*Verifier).Verify | 1.5s | 60.00% | |
| 42 | +``` |
| 43 | + |
| 44 | +**Reading the output:** |
| 45 | +- `>>>` marks the top 20 functions by time |
| 46 | +- Times are **cumulative** (include all child functions) |
| 47 | +- `x5` means called 5 times (total time shown) |
| 48 | +- Percentages relative to root function |
| 49 | + |
| 50 | +## Options |
| 51 | + |
| 52 | +```bash |
| 53 | +./profile.sh <test_or_benchmark_name> [options] |
| 54 | + |
| 55 | +Options: |
| 56 | + -d, --display <mode> Display: both, time, percent (default: both) |
| 57 | + -f, --root-function <name> Start from this function (show subtree) |
| 58 | + -m, --min-percent <n> Hide functions below this % (default: 0) |
| 59 | + -h, --help Show help |
| 60 | +``` |
| 61 | + |
| 62 | +**Examples:** |
| 63 | +```bash |
| 64 | +# Show only percentages |
| 65 | +./profile.sh BenchmarkValidatorTransfer -d percent |
| 66 | + |
| 67 | +# Profile a subtree |
| 68 | +./profile.sh BenchmarkValidatorTransfer -f VerifyTransfer |
| 69 | + |
| 70 | +# Hide functions using less than 1% |
| 71 | +./profile.sh BenchmarkValidatorTransfer -m 1.0 |
| 72 | + |
| 73 | +# Profile a test |
| 74 | +./profile.sh TestValidatorTransfer |
| 75 | +``` |
| 76 | + |
| 77 | +## Scope |
| 78 | + |
| 79 | +**Searches for tests/benchmarks in:** |
| 80 | +- `token/` directory and all subdirectories |
| 81 | + |
| 82 | +**Instruments:** |
| 83 | +- All Go packages in `token/` directory |
| 84 | + |
| 85 | +**Output:** |
| 86 | +- Saved to current directory as `<TestName>_profile.md` |
| 87 | + |
| 88 | +## Files |
| 89 | + |
| 90 | +- `profile.sh` - Automated profiling script |
| 91 | +- `auto-instrument.go` - Adds tracer hooks to Go files |
| 92 | +- `inject-tracer.go` - Injects tracer into test/benchmark |
| 93 | +- `tracer/` - Runtime tracing library |
| 94 | + |
| 95 | +## Troubleshooting |
| 96 | + |
| 97 | +**Can't find test/benchmark:** |
| 98 | +- Check spelling |
| 99 | +- Ensure it's in `token/` directory |
| 100 | + |
| 101 | +**Empty output file:** |
| 102 | +- Check if test/benchmark runs successfully |
| 103 | +- Look for compilation errors in script output |
| 104 | + |
| 105 | +**Times don't add up:** |
| 106 | +- Times are cumulative (include children) |
| 107 | +- Use percentages to see relative contributions |
0 commit comments