Brief introduction
Stackprof is a sampling-based tuning tool, what are the benefits of sampling? The advantage is that you can use it online and grab part of the data based on the built-in algorithm, affecting only a fraction of the performance. It generates a series of dump files, and then you analyze the files online to locate the problem, and Google has a sample based paper that basically proves that sampling is feasible. And Stackprof is also deeply influenced by Google's perftools, using the sampling method to do the tuning.
Basic Use Method
StackProf.run(mode::cpuout:‘./stackprof.dump‘do # 你的代码end
Here we give a sample code to test the target:
require "Stackprof"Class Compute def M1"string"* - Enddef m2"string"*10000 Enddef start -_000.times DoM1 m2End EndEndStackprof.run (Mode:: CPU, out:'./stackprof.dump ') DoCompute.New. StartEnd
Save As test.rb
, while executing Ruby TEST.RB will generate the Stackprof.dump file in the current directory, we open this file with Stackprof:
stackprof stackprof.dump --text
================================== Mode:cpu ( +) Samples:1793(0.61% miss Rate) GC:587(32.74%)================================== Total (PCT) SAMPLES (PCT) FRAME1106(61.7%) 1106(61.7%)Compute#m298(5.5%) 98(5.5%)Compute#m11206(67.3%) 2(0.1%)BlockinchCompute#start1206(67.3%) 0(0.0%)<main>1206(67.3%) 0(0.0%)Compute#start1206(67.3%) 0(0.0%)<main>1206(67.3%) 0(0.0%)Blockinch<main>
Here it is obvious that the M2 method is slow, occupy most of the execution time, compared to other tuning tools, it just lists the user's own method of the time ratio, in the test in Ruby-prof, it will show the String#*
proportion of this method, but for us, It has little meaning, and stackprof does not care about the methods in the standard library. At the same time Stackprof can also filter the method, for example, we found M2 This method has a problem, then you can filter it out to see the details:
Stackprof Stackprof.dump--text-- method 'Compute#m2'Compute#m2 (/users/lizhe/workspace/ Ruby-performance-tuning/test.rb:9)samples: 1106 Self(61.7%) /1106Total (61.7%) Callers:1106(100.0%)Block inchCompute#start Code: |9|End 1106(61.7%) /1106(61.7%) |Ten| | One| def start
We can see M2 This method defines which row of the file, and who is calling it, and also shows its context in the source code. If more than one method calls the M2, it also shows these methods, and the proportion they call M2, which is the callers part above, because only one start method calls M2, so it is 100%.
How to use in rack
The stackprof itself implements a rack middleware, so it can be conveniently mounted to a rack application:
use StackProf::Middleware, enabled: true, mode: :cpu, save_every: 5
To use in rails, first add stackprof to Gemfile and then add Middleware:
config.middleware.use StackProf::Middleware, enabled: true, mode: :cpu, save_every: 5
Then request your app, request a few more times, every 5 seconds it will save the output to the TMP directory to see one of the results:
================================== Mode:cpu ( +) Samples:155(0.00% miss Rate) GC: One(7.10%)================================== Total (PCT) SAMPLES (PCT) FRAME -(11.6%) -(11.6%)Hike::index#entries A(7.7%) A(7.7%)Hike::index#stat9(5.8%) 9(5.8%)#<module:0x007fb72a0c7b08>.load_with_autoloading -(11.6%) 9(5.8%)Sprockets::cache::filestore#[]6(3.9%) 6(3.9%)Block (2Levels)inchBindingofcaller::bindingextensions#callers5(3.2%) 5(3.2%)Time.parse5(3.2%) 5(3.2%)Sprockets::mime#mime_types5(3.2%) 5(3.2%)Pathname#chop_basename4(2.6%) 4(2.6%)BlockinchActionview::P athresolver#find_template_paths4(2.6%) 4(2.6%)BlockinchBettererrors::exceptionextension#set_backtrace the(9.7%) 3(1.9%)BlockinchActivesupport::D ependencies#load_file2(1.3%) 2(1.3%)Temple::mixins::compileddispatcher::D ispatchnode#initialize5(3.2%) 2(1.3%)Actiondispatch::cookies::encryptedcookiejar#initialize2(1.3%) 2(1.3%)Activesupport::keygenerator#generate_key2(1.3%) 2(1.3%)BlockinchActionview::P athresolver#query4(2.6%) 2(1.3%)Slim::P arser#initialize113(72.9%) 2(1.3%)Actionview::renderer#render_template2(1.3%) 2(1.3%)Hike::trail#stat2(1.3%) 2(1.3%)BlockinchActivesupport::D ependencies#search_for_file A(14.2%) 2(1.3%)BlockinchTemple::filters::multiflattener#on_multi -(12.9%) 2(1.3%)Temple::filters::controlflow#dispatcher the(9.7%) 2(1.3%)Actionview::renderer#render_partial1(0.6%) 1(0.6%)BlockinchSlim::P arser#initialize1(0.6%) 1(0.6%)Pathname#prepend_prefix1(0.6%) 1(0.6%)String#blank?1(0.6%) 1(0.6%)Activesupport::safebuffer#initializeTen(6.5%) 1(0.6%)Sprockets::asset#dependency_fresh?1(0.6%) 1(0.6%)Sprockets::asset#init_with1(0.6%) 1(0.6%)Hike::index#sort_matches1(0.6%) 1(0.6%)BlockinchActivesupport::D Ependencies::loadable#require
You can use this method to debug your online environment.
Reference Links:
- Https://github.com/tmm1/stackprof
This article is a ONEAPM engineer original article. ONEAPM is the emerging leader in China's basic software industry, helping enterprise users and developers easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the ONEAPM Official technology blog.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Stackprof of Ruby Profiler