CSC/ECE 517 Fall 2013/ch1 1w02 pp: Difference between revisions
Line 58: | Line 58: | ||
===Ruby-prof Profiler=== | ===Ruby-prof Profiler=== | ||
''' | [http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which methods it called. RubyProf generate both text and html and can output it to standard out or to a file. | ||
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''<br><br> | '''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''<br><br> | ||
Line 114: | Line 109: | ||
0.00 1.874 0.000 0.000 1.874 1 Object#slow_method | 0.00 1.874 0.000 0.000 1.874 1 Object#slow_method | ||
0.00 0.000 0.000 0.000 0.000 1 Module#method_added | 0.00 0.000 0.000 0.000 0.000 1 Module#method_added | ||
The above code displays the all the functions that were called and time it took to execute sorted in descending order, so that we can see the most time consuming method at the top. | |||
==Conclusion== | ==Conclusion== | ||
For large scale applications, where there are thousands of user, and many transactions try using Ruby optimizing Techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it's important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results. | For large scale applications, where there are thousands of user, and many transactions try using Ruby optimizing Techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it's important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results. |
Revision as of 23:56, 16 September 2013
Introduction to Profiling
Profiling in general, is an important feature in computer science. It is technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the programs time complexity or space complexity, and some other related performance parameters. For Example, the time complexity includes total running time, CPU time, memory used, time taken by each module or function, function calls, response time and many similar important aspects of our program/application. Profiling takes up the significant steps or constructs (like loop statements, statements involving operations and functions like aggregate functions, individual blocks and modules) in the program source code for performance study.
Ruby profiling is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution give values for a set of parameters that define the performance of the input ruby program. There are many types of profiling method like event-based and statistical methods. Ruby mostly uses event-based profilers. Here in case of Ruby profiler, we get the analysis results in various formats like table, graphs etc.
As we can see the flow we give the ruby program as input to the Ruby profiling tools and that's it we will get a profiler output, which will help us in optimizing out code.
Significance of Profiling
The main purpose of profiling is:
- To analyze the performance of the code, their CPU utilization, memory operations and also to find out bottlenecks in our program.
- Based on the results presented by the profiler, one can improve upon the code with better constructs and functions and hence we can easily optimize the code in accord with the system requirements.
- We can accurately measure how a program functions or performs in one given environment and with different input data.
- To identify the bottlenecks in a program, the portions that actually cause program overhead or can slow down the system o need special testing or can raise exception.
Ruby Profiling tools
There are many Profiling tools available for analyzing the ruby programs. Ruby itself has inbuilt profilers in the form of modules. Two such modules are Profiler__ and Benchmark module. We will look how to use these tools to profile our ruby code.
Default Profiler
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. This profile.rb source file has the program that measures the performance of the system by recording the function calls. Here, the input is a collection of all the function calls made in the code. Specifically, profile.rb uses the method ‘ kernel#set_trace_func’ to keep a track of all function calls.
How to use profile.rb to profile our code:
1) Create a ruby file profiler_example.rb
#profile_example.rb code begins require 'profile' def slow_method 10000.times do 9999999999999999*999999999 end end slow_method #profile_example.rb code ends
As we can see the ruby code, we have declared a function slow_method to just perform multiplication of two values 10000 times using "times" iterator.
2. Run the deafult profiler on profiler_example.rb using command
ruby -rprofile profiler_example.rb
3. Output of profiler
% cumulative self self total time seconds seconds calls ms/call ms/call name 65.96 0.06 0.06 1 62.00 94.00 Integer#times 34.04 0.09 0.03 10000 0.00 0.00 Bignum#* 0.00 0.09 0.00 1 0.00 0.00 Array#each 0.00 0.09 0.00 1 0.00 94.00 Object#slow_method 0.00 0.09 0.00 1 0.00 94.00 #toplevel
Note: This output is truncated to show basic functions in ruby. When you will run this code you will see a lot of other functions.
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code.
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes. Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.
Now the output of analysis is displayed in the form of table. Each row represent each method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as=time taken by operation* number of times operation/method execution is performed.
Ruby-prof Profiler
ruby-prof is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which methods it called. RubyProf generate both text and html and can output it to standard out or to a file.
To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :
1. Install ruby-prof gem
gem install ruby-prof
2. Create a ruby code file to profile
#Start of ruby code file to profile require 'ruby-prof' RubyProf.start #code to profile start def slow_method for num in 1..10_000 do is_prime = 1 for x in 2..(num - 1) do if (num % x == 0) is_prime = x break end end if is_prime == 1 puts "#{num} is a prime number" else puts "#{num} equals #{is_prime} * #{num/is_prime}" end end end slow_method #code to profile end result = RubyProf.stop printer = RubyProf::FlatPrinter.new(result) printer.print(STDOUT) #End of ruby code file to profile
3. Output of ruby-prof profiler
Thread ID: 17223036 Fiber ID: 20361900 Total: 1.873907 Sort by: self_time
%self total self wait child calls name 2.70 0.051 0.051 0.000 0.000 20000 IO#write 1.59 1.874 0.030 0.000 1.844 10001 *Range#each 0.47 0.009 0.009 0.000 0.000 27540 Fixnum#to_s 0.39 0.058 0.007 0.000 0.051 10000 IO#puts 0.21 0.004 0.004 0.000 0.000 10000 <Class::Range>#allocate 0.20 0.062 0.004 0.000 0.058 10000 Kernel#puts 0.00 1.874 0.000 0.000 1.874 1 Global#[No method] 0.00 1.874 0.000 0.000 1.874 1 Object#slow_method 0.00 0.000 0.000 0.000 0.000 1 Module#method_added
The above code displays the all the functions that were called and time it took to execute sorted in descending order, so that we can see the most time consuming method at the top.
Conclusion
For large scale applications, where there are thousands of user, and many transactions try using Ruby optimizing Techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it's important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.