DEV Community

Dmitry Daw
Dmitry Daw

Posted on • Edited on

Base test profiling inside docker with test-prof

Install test-prof and stackprof gems.

group :test do
  gem "test-prof", "~> 1.0"
  gem "stackprof", '>= 0.2.9', require: false
end
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then you could visualize it.

Visualize with speedscope

Another way is to use speedscope, as it provides better interface. For this, generate json output

stackprof --flamegraph tmp/test_prof/stack-prof-report-wall-raw-total.dump > tmp/test_prof/stack-prof-report-wall-raw-total.json
Enter fullscreen mode Exit fullscreen mode

And upload the file to speedscope, e.g. at https://www.speedscope.app/

Visualize with stackprof's viewer

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Links

Top comments (0)