#!/usr/bin/env bash
#
# Usage:
#   script/heap-profile
#
# Parse an example source file and record memory usage
#
# Dependencies:
#   * `pprof` executable:   https://github.com/google/pprof
#   * `gperftools` package: https://github.com/gperftools/gperftools

set -e

GRAMMARS_DIR=$PWD/test/fixtures/grammars

# Build the library
make

# Build the heap-profiling harness
clang++                               \
  -I lib/include                      \
  -I $GRAMMARS_DIR                    \
  -D GRAMMARS_DIR=\"${GRAMMARS_DIR}/\" \
  -l tcmalloc                         \
  ./libtree-sitter.a                  \
  test/profile/heap.cc                \
  -o target/heap-profile

# Run the harness with heap profiling enabled.
export HEAPPROFILE=$PWD/profile
target/heap-profile $@

# Extract statistics using pprof.
pprof -top -cum profile.0001.heap
