Base test profiling inside docker with test-prof

RMAG news

Install test-prof and stackprof gems.

group :test do
gem “test-prof”, “~> 1.0”
gem “stackprof”, ‘>= 0.2.9’, require: false
end

Run test with flag TEST_STACK_PROF=1. Better to run only part of tests or use SAMPLE=10 flag, otherwise you could get too big of a stack dump.

TEST_STACK_PROF=1 bundle exec rspec spec/my_test.rb

Then convert flamegraph dump to html

stackprof –flamegraph tmp/test_prof/stack-prof-report-wall-raw-total.dump > tmp/test_prof/stack-prof-report-wall-raw-total.html

Then you could see the flamegraph with gem’s viewer. From inside docker you could copy flamegraph.js and viewer.html to same shared folder, e.g. tmp folder in project.

cp /usr/local/bundle/gems/stackprof-0.2.26/lib/stackprof/flamegraph/viewer.html ./tmp/test_prof/
cp /usr/local/bundle/gems/stackprof-0.2.26/lib/stackprof/flamegraph/flamegraph.js ./tmp/test_prof/

And then open in the browser url like

file:///home/my/path_to_project_folder/tmp/test_prof/viewer.html?data=/home/my/path_to_project_folder/tmp/test_prof/stack-prof-report-wall-raw-total.html

Links

https://github.com/test-prof/test-prof Test-prof gem

https://evilmartians.com/chronicles/testprof-a-good-doctor-for-slow-ruby-tests article about usage