Make stackprof a ruby script [ci skip]

This commit is contained in:
Parker Moore 2017-03-31 00:49:42 -04:00
parent 3921e523c5
commit 409c8f9f31
No known key found for this signature in database
GPG Key ID: 193CDEBA72063C58
1 changed files with 41 additions and 18 deletions

View File

@ -1,26 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env ruby
set -e require "bundler/setup"
require "json"
require "stackprof"
require File.expand_path("../../lib/jekyll", __FILE__)
case "$1" in MODE = ARGV.first || "cpu"
cpu|object) STACKPROF_MODE="$1"; shift ;; PROF_OUTPUT_FILE = File.expand_path("../../tmp/stackprof-#{MODE}-#{Time.now.strftime("%Y%m%d%H%M")}.dump", __FILE__)
*) STACKPROF_MODE="cpu" ;;
esac
export BENCHMARK=true puts "Stackprof Mode: #{MODE}"
command -v stackprof > /dev/null || script/bootstrap
TEST_SCRIPT="Jekyll::Commands::Build.process({'source' => 'docs'})" unless File.exist?(PROF_OUTPUT_FILE)
PROF_OUTPUT_FILE=tmp/stackprof-${STACKPROF_MODE}-$(date +%Y%m%d%H%M).dump StackProf.run(
mode: MODE.to_sym,
interval: 100,
raw: true,
out: PROF_OUTPUT_FILE
) do
puts "GC Stats:", JSON.pretty_generate(GC.stat)
GC.disable
GC_BEFORE="puts 'GC Stats:'; puts JSON.pretty_generate(GC.stat); GC.disable" Jekyll::Commands::Build.process({'source' => 'docs'})
GC_AFTER="puts 'GC Stats:'; GC.start(full_mark: true, immediate_sweep: false); puts JSON.pretty_generate(GC.stat);"
echo Stackprof Mode: $STACKPROF_MODE puts 'GC Stats:'
test -f "$PROF_OUTPUT_FILE" || { GC.start(full_mark: true, immediate_sweep: false)
bundle exec ruby -r./lib/jekyll -rstackprof -rjson \ puts JSON.pretty_generate(GC.stat)
-e "StackProf.run(mode: :${STACKPROF_MODE}, interval: 100, out: '${PROF_OUTPUT_FILE}') { ${GC_BEFORE}; ${TEST_SCRIPT}; ${GC_AFTER}; }" end
end
options = {
sort: true,
limit: 30,
format: :text,
} }
set -x # You can also do other things. Examples:
bundle exec stackprof $PROF_OUTPUT_FILE $@ --sort-total # https://github.com/tmm1/stackprof/blob/master/bin/stackprof
report = StackProf::Report.new(Marshal.load(IO.binread(PROF_OUTPUT_FILE)))
report.print_text(
options[:sort],
options[:limit],
options[:select_files],
options[:reject_files],
options[:select_names],
options[:reject_names]
)
puts "Results stored in #{PROF_OUTPUT_FILE}"