Built-in profiler implementation is very simple, in ruby2.2 only 150 lines of code, you can see its implementation profile.rb. The built-in profiler is very convenient to use, just add-rprofile parameters. For example:
Perform:
The output results are:
By printing out the results can be very obvious to see the time-consuming method. The built-in profiler is very simple, can only print out such a result, no other output format options, the other examples described below are rich in format output.
ruby-prof
Repo:https://github.com/ruby-prof/ruby-prof
Ruby-prof has a C extension, so it can run faster, and it supports rich output formats to help us find performance bottlenecks. The ruby-prof supports the DOT format supported by the output Graphviz, both of which are installed as follows:
Gem install ruby-prof
Ubuntu | sudo apt-get install graphviz
Mac | Brew Install Graphviz
The execution command is simple, as follows:
Ruby-prof--mode=wall--printer=dot--file=output.dot test.rb 25
For detailed use of this command, refer to the Help information ruby-prof--help.
The execution result of the above command outputs a graphviz dot file, Graphviz provides a format conversion command that converts the file to a PDF file for easy viewing, as follows:
Dot-t Pdf-o output.pdf Output.dot
This allows you to turn on the method invocation in the Output.pdf Viewer.
perftools.rb
Repo:https://github.com/tmm1/perftools.rb
PERFTOOLS.RB is a ruby version of Google-perftools, but it only supports the ruby2.1 version, which requires the following stackprof for version 2.1 and above, both of which are written by one person. In view of this, we skip Perftools.rb, the author realizes Stackprof, is to replace PERFTOOLS.RB. If there is a need, please refer to its GitHub homepage.
stackprof
Repo:https://github.com/tmm1/stackprof
Stackprof only supports ruby2.1+, but now that Ruby is released quickly, with every release bringing something new, 2.1 should soon be a very basic version, and we'll do some testing on this version.
Installation:
This time we'll use the Stackprof method directly in our code:
Require ' Stackprof '
def M1
5_000_000.times{1+2+3+4+5}
end
def m2 1_000_000.times{1+2+3+4+5
}
End
Stackprof.run (Mode:: CPU, out: ' Tmp/stackprof.dump ') does
M1
m2
end
We execute this Ruby program, Ruby Test.rb, will generate a file stackprof.dump in the TMP directory of the current directory, and then analyze the following file, the Stackprof command itself can parse the file and execute the following command:
Stackprof Tmp/stackprof.dump--text
will produce the following result, the result should be very clear, obviously in the code M1 method to occupy most of the running time.
==================================
mode:cpu (1000)
samples:75 (0.00% miss Rate)
gc:0 (0.00%)
= = = = ============================
Total (PCT) SAMPLES (PCT) FRAME
(82.7%) 62 (82.7%) Block in Object#m1 (17.3%) (
17.3%) blocks in object#m2
(100.0%) 0 (0%) C19/><main>
(100.0%) 0 (0.0%) block in <main>
(100.0%) 0 (0.0 %) <main> (
82.7%) 0 (0.0%) object#m1 (
17.3%) 0 (0.0%) Object#m2
Other richer way of output and analysis, please refer to Stackprof's GitHub homepage, explained very comprehensive.
If you want to display relevant information in the front of the web, look at the gem of Stackprof-webnav, which provides a more comprehensive display, operation, etc., suitable for use in some Web applications, GitHub address: Stackprof-webnav
Rack-mini-profiler
Repo:https://github.com/miniprofiler/rack-mini-profiler
Rack-mini-profiler's performance tuning specifically applies to rack Web applications, using rails in the following ways:
First add the gem to the Gemfile:
Gem ' Rack-mini-profiler '
Perform:
Then restart your server, access any URL, and in the upper-left corner of the page you will see the number of milliseconds of response time. As shown in the following illustration:
Click 1 SQL in the query time (MS) column to view the SQL statements executed and time consuming: