“Instrument” is a little utility library to get useful information out of methods with a minimum of fuss, and without having to dig into code to add debugging output. We’ve released it as a gem, and it’s being developed on GitHub.
Here’s a simple example. Let’s say you have a [contrived] class like this:
class Car
# ...
def drive(miles)
# ...
end
end
.. and you’d like to get some basic statistics whenever the drive method is called on an instance. With instrument, there’s no need to modify the actual Car class directly; you can just wrap the method with a handler that’s called after every invocation with the instance, time taken to invoke, and any arguments:
require 'instrument'
instrument 'Car#drive' do |car, time, miles|
puts "#{car} was driven #{miles}mi (#{time})"
end
# Drive a lot of cars
If you’d also like to know track the number of cars instantiated, just increment a counter:
cars = 0
instrument 'Car.new' do |klass, *args|
cars += 1
end
# Make a lot of cars
puts "Made #{cars} car(s)!"
You can use the library to get basic performance numbers, to output tracing information, or just to do a bit of exploratory poking around.
Play with it, fork it, and send pull requests!















Continued Discussion
No comments have been added yet.