From 533aaa462b301187aeff4fb7ad096e68c73c6545 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 23 Oct 2020 13:20:57 -0700 Subject: [PATCH] Add heap-profiling script --- script/heap-profile | 34 ++++++++++++++++++++++++++++++++++ test/profile/heap.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 script/heap-profile create mode 100644 test/profile/heap.cc diff --git a/script/heap-profile b/script/heap-profile new file mode 100755 index 00000000..012d86c7 --- /dev/null +++ b/script/heap-profile @@ -0,0 +1,34 @@ +#!/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 diff --git a/test/profile/heap.cc b/test/profile/heap.cc new file mode 100644 index 00000000..6c0027e8 --- /dev/null +++ b/test/profile/heap.cc @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +extern "C" { +#include "javascript/src/parser.c" +#include "javascript/src/scanner.c" +} + +#define LANGUAGE tree_sitter_javascript +#define SOURCE_PATH "javascript/examples/jquery.js" + +int main() { + TSParser *parser = ts_parser_new(); + if (!ts_parser_set_language(parser, LANGUAGE())) { + fprintf(stderr, "Invalid language\n"); + exit(1); + } + + const char *source_path = GRAMMARS_DIR SOURCE_PATH; + + printf("Parsing %s\n", source_path); + + std::ifstream source_file(source_path); + if (!source_file.good()) { + fprintf(stderr, "Invalid source path %s\n", source_path); + exit(1); + } + + std::string source_code( + (std::istreambuf_iterator(source_file)), + std::istreambuf_iterator() + ); + + TSTree *tree = ts_parser_parse_string( + parser, + NULL, + source_code.c_str(), + source_code.size() + ); +}