#!/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 libtree-sitter.a

# Build the heap-profiling harness
clang++                                  \
  -Wno-reorder-init-list                 \
  -Wno-c99-designator                    \
  -I lib/include                         \
  -I "$GRAMMARS_DIR"                     \
  -D GRAMMARS_DIR="\"${GRAMMARS_DIR}/\"" \
  test/profile/heap.cc                   \
  -l tcmalloc                            \
  libtree-sitter.a                       \
  -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
