





1. Each step of the execution

2. Objects which are found along the way

The object which is the context of the TracePoint



Objects which are passed as arguments (if a call)



Objects which are returned (if a return)



Objects which are referenced via variables

3. Abstract syntax trees of all Ruby code files involved in the execution

neo4j_session = Neo4j::Session.open(:server_db, neo4j_url) Neolytics.record_execution(neo4j_session) do doc = Nokogiri::HTML(open('https://www.google.com').read) doc.xpath('//form/input').map(&:name) end

Querying the Data

Examining the Execution

call

MATCH (call_tp:TracePoint {event: 'call'}) WITH call_tp LIMIT 1 MATCH path=shortestPath((call_tp)-[:NEXT*]->(return_tp:TracePoint {event: 'return'})) RETURN path

#plural

active_support

call

line

NEXT

HAS_PARENT

call

c_call

STARTED_AT

return

call

Running Code Metrics

MATCH (def:ASTNode {type: 'def'}) OPTIONAL MATCH (def)

This gives us a straightforward listing of methods, where to find them, and their complexity score, all ordered with the most complex methods at the top:





method Path and line complexity initialize_pattern …/rubies/ruby-2.2.3/lib/ruby/2.2.0/uri/rfc2396_parser.rb:342 85 validate …/rubies/ruby-2.2.3/lib/ruby/2.2.0/rubygems/specification.rb:2453 65 perform_arguments …/gems/ruby-2.2.3/gems/sass-3.4.20/lib/sass/tree/visitors/perform.rb:14 57 _next_token …/gems/ruby-2.2.3/gems/nokogiri-1.6.7/lib/nokogiri/css/tokenizer.rb:55 56 parse …/gems/ruby-2.2.3/gems/tzinfo-1.2.2/lib/tzinfo/zoneinfo_timezone_info.rb:95 55 pretty_print …/rubies/ruby-2.2.3/lib/ruby/2.2.0/pp.rb:421 53 merge_final_ops …/gems/ruby-2.2.3/gems/sass-3.4.20/lib/sass/selector/sequence.rb:342 52 call …/gems/ruby-2.2.3/gems/sprockets-3.5.2/lib/sprockets/server.rb:22 48 convert_input …/gems/ruby-2.2.3/gems/erubis-2.7.0/lib/erubis/converter.rb:127 43 visit_rule …/gems/ruby-2.2.3/gems/sass-3.4.20/lib/sass/tree/visitors/to_css.rb:280 43

MATCH (tp:TracePoint) WITH sum(tp.execution_time) AS total_execution_time MATCH (node:ASTNode {type: 'def'}) OPTIONAL MATCH (node)

With this, we get a nice table of methods which take the most time, along with their cyclomatic complexity. We can sort by either metric or create a combined metric of our own in order to determine which methods might be ripe for refactoring.





method cc execs total_time % total time avg_exec …/rubies/ruby-2.2.3/lib/ruby/2.2.0/set.rb:289

Set#add 1 2343 11457 2.31 4.89 …/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:91

ActiveSupport::Inflector#underscore 6 237 5057 1.02 21.34 …/gems/ruby-2.2.3/gems/pry-0.10.3/lib/pry/command.rb:27

#<Class:Pry::Command>#match 4 810 4917 0.99 6.07 …/gems/ruby-2.2.3/gems/pry-0.10.3/lib/pry/command.rb:43

#<Class:Pry::Command>#command_options 2 641 3790 0.76 5.91 …/gems/ruby-2.2.3/gems/pry-0.10.3/lib/pry/command.rb:177

#<Class:Pry::Command>#command_regex 6 510 3722 0.75 7.30 …/gems/ruby-2.2.3/gems/pry-0.10.3/lib/pry/command.rb:185

#<Class:Pry::Command>#convert_to_regex 1 1020 3537 0.71 3.47 …/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/dependencies/autoload.rb:35

ActiveSupport::Autoload#autoload 4 233 3186 0.64 13.67 …/gems/ruby-2.2.3/gems/sprockets-3.5.2/lib/sprockets/utils.rb:33

Sprockets::Utils#hash_reassoc1 4 115 2366 0.48 20.57 …/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/core_ext/module/delegation.rb:151

Module#delegate 36 43 1164 0.23 27.07 …/gems/ruby-2.2.3/gems/pry-0.10.3/lib/pry/command.rb:37

#<Class:Pry::Command>#description 2 152 1158 0.23 7.62

More Options

ruby_code_analytics

For a long time I’ve been wanting to use Neo4j as a tool to analyze Ruby code.Using Ruby for almost a decade, I have a lot of experience with effectively finding my way around Ruby code. Still, Ruby programs can be very dynamic and thus tools are always welcome.As I’ve begun to use Neo4j, I’ve been inspired by code analysis tools such as joern and jQAssistant as well as Aaron Patterson’s heap-analyzer . In that spirit, I’d like to announce a new project called Neolytics Neolytics is a Ruby gem which records the execution of a block of Ruby code in a Neo4j database. There are three major things which it records:This is thanks to Ruby’s excellent TracePoint class. TracePoint gives a hook into each execution step of Ruby code so that we can record line executions, method calls/returns and raising of executions.For each trace point, Neolytics finds:For each object, the class and module ancestry information is also recursively imported.With thanks to the parser gem! Additionally, method definitions in the AST are linked to their corresponding TracePoint.To record the data, I used my neo4apis gem to send data to Neo4j efficiently without a lot of round trips to the database.Lastly, I’ve built a ruby_code_analytics Rails application to give a UI to examine the results of the dump. You can see an example running on Heroku which is a record of the following simple Ruby code:Let’s look at some examples of things that you can do with the data from neolytics!Let’s say that we want to know what goes on during the execution of a method. With the following query, we grab the first TracePoint which was aevent and find the entire series of TracePoints until the method is returned from:Visually we can see the query like this:In this case, the method is themethod from thegem which lets you pluralize strings. You can compare the graph database model with the actual source code here on GitHub In the screenshot above, each node is displaying a TracePoint event (i.e., etc…), the class and method in question and the line number. Starting from the node on the left, you can follow therelationships to see each point in the execution.Note that every node has arelationship back to theTracePoint, so we can see that this method didn’t make any further method calls (at least no Ruby calls; we see someevents which TracePoint doesn’t expose).Lastly, note therelationship from theevent to theevent which is there as a convenience for any query that needs to get both nodes at once (e.g., if you need to get both the arguments and the return value of a method).For more details, you can see this example tabular output of the TracePoints.Let’s run a query to evaluate the cyclomatic complexity of our methods:Now let’s extend this complexity metric by using the TracePoint data to see how long each method actually took:There are many ways to use this data model to debug and otherwise analyze our code. I’ve provided some more sample queries in the README for the neolytics gem. I’ve also started a list of ideas for new queries which could be made.These could be used directly or perhaps built into a reporting feature of theRails application. I’m excited to see where things could go!Analysis app running on Heroku: https://ruby-neo4j-code-analysis.herokuapp.com