The usage of the Profiling tool in Ruby, rubyprofiling

Source: Internet
Author: User
Tags time in milliseconds

The usage of the Profiling tool in Ruby, rubyprofiling

The built-in profiler implementation is very simple. In ruby2.2, there are only 150 lines of code. You can see its implementation profile. rb. The built-in profiler is very convenient to use. You only need to add the-rprofile parameter. For example:

Run:

ruby -rprofile test.rb

Output result:

The printed results clearly show the time-consuming method. The built-in profiler is very simple. Only such results can be printed. There are no other output format options. The other types described below have rich output formats.
Ruby-prof

Repo: https://github.com/ruby-prof/ruby-prof

Ruby-prof has C extensions, so it can run faster. It also supports a wide range of output formats to help us find performance bottlenecks. Ruby-prof supports output of the dot format supported by GraphViz. The installation methods of the two are as follows:

gem install ruby-profubuntu | sudo apt-get install graphvizMac  | brew install graphviz

Execute the command as follows:

ruby-prof --mode=wall --printer=dot --file=output.dot test.rb 25

For more information about how to use this command, see ruby-prof -- help.

The execution result of the above command will output a dot file of graphviz. graphviz provides a format conversion command to convert this file into a PDF file for ease of viewing, as shown below:

dot -T pdf -o output.pdf output.dot

In this case, you can open outputpipeline to view the proportion of method calls in the program.


Perftools. rb

Repo: https://github.com/tmm1/perftools.rb

Perftools. rb is the ruby version of google-perftools, but it only supports versions earlier than ruby2.1. The stackprof version 2.1 and later needs to be used. Both tools are written by one person. In view of this, we skipped perftools. rb, and the author implemented stackprof to replace perftools. rb. For more information, see the github homepage.
Stackprof

Repo: https://github.com/tmm1/stackprof

Stackprof only supports Ruby2.1 +, But Now ruby is released very quickly. Every version can bring new things, and 2.1 should soon become a very basic version, we will do some tests in this version.

Installation:

gem install stackprof

This time we use the stackprof method directly in the Code:

require 'stackprof'def m1 5_000_000.times{ 1+2+3+4+5 }enddef m2 1_000_000.times{ 1+2+3+4+5 }endStackProf.run(mode: :cpu, out: 'tmp/stackprof.dump') do m1 m2end

We execute this ruby program, ruby test. rb will generate a file stackprof in the tmp directory of the current directory. dump, and then analyze the following file. The stackprof command can parse the file and execute the following command:

stackprof tmp/stackprof.dump --text

The following results will be generated, and the results should be clear. Obviously, the m1 method occupies most of the running time in the code.

================================== Mode: cpu(1000) Samples: 75 (0.00% miss rate) GC: 0 (0.00%)==================================   TOTAL  (pct)   SAMPLES  (pct)   FRAME    62 (82.7%)     62 (82.7%)   block in Object#m1    13 (17.3%)     13 (17.3%)   block in Object#m2    75 (100.0%)      0  (0.0%)   <main>    75 (100.0%)      0  (0.0%)   block in <main>    75 (100.0%)      0  (0.0%)   <main>    62 (82.7%)      0  (0.0%)   Object#m1    13 (17.3%)      0  (0.0%)   Object#m2

For more information about the output and analysis methods, see the github homepage of stackprof.

If you want to display relevant information on the web Front-end, please refer to the stackprof-webnav gem, which provides a comprehensive presentation and operations, and is suitable for some web applications, github address: stackprof-webnav
Rack-mini-profiler

Repo: https://github.com/MiniProfiler/rack-mini-profiler

Rack-mini-profiler is used to optimize the performance of rack-based web applications. It is used in rails as follows:

First, add the gem to gemfile:

gem 'rack-mini-profiler'

Run:

bundle install

Restart your server and access any URl. in the upper left corner of the page, you will see the response time in milliseconds. As shown in:

Click 1 SQL in the query time (MS) column to view the executed SQL statement and time consumption:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.