Skip to content

Ruby 2.1 TracePoint API

March 1, 2014

The Ruby TracePoint API is simply an Object-Oriented wrapper around the now obsolete Kernel#set_trace_func. There is a performance hit associated with using TracePoint (not for production), but it can provide insight into the amount of work being done inside some Ruby code. We can learn a lot by diving into some code examples using Ruby’s favorite framework Rails.

Ruby’s TracePoint on Rails 4.0.3

Here are some interesting examples of using TracePoint to see what’s going on in Rails: I provide two simple examples. First, all the event stats for a startup of a Rails 4.0.3 application, and also the stats for a single request done on a single thread using WEBrick.

If you know the Rails Initialization Process, then you’ll understand why I put this code in config/boot.rb to begin TracePoint. I stop TracePoint on the first request to the root “/” and display the output.

So the results breakdown like this on my Macbook Air (10.9.2) running MRI Ruby 2.1.1-p76 with Rails 4.0.3:

From boot to 1st request takes: 603 classes, 2,508 different methods, and 468,873 method invocations.

Between requests: 271 classes used, 916 different methods, and 4,510 method invocations.

The numbers for method calls seem very high, but maybe not exorbitantly so when we recall that everything is a method call in Ruby even addition “+”.

:a_call -> the hidden event

I found out about this little fact during a RubyConf2013 in Miami from a core committer. It’s a combination event of b_call, call, and c_call. Even though it exists in Ruby 2.1.1, I’m not exactly sure what version it was first released in, but definitely not in Ruby 2.0.0-p451 or earlier. Even though TracePoint was released with Ruby 2.0, “:a_call” was not added until Ruby 2.1 which is why I named the title of this post “Ruby 2.1 TracePoint API” because that’s what I’m using here. It’s not documented in ri TracePoint, but still Jruby 9000 does have support for it, while Jruby 1.7.11 (latest at time of this writing) does not.

The relevant tests can be found in MRI Ruby source code under test/ruby/test_settracefunc.rb

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: