<?xml version="1.0"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>bitmonkey</title>
    <description>by Eric Lindvall</description>
    <link>http://bitmonkey.net/</link>
    <atom:link href="http://bitmonkey.net/rss.xml" rel="self" type="application/rss+xml" />
    <language>en-us</language>
    <pubDate>Wed, 14 Jun 2017 02:19:32 +0000</pubDate>
    <lastBuildDate>Wed, 14 Jun 2017 02:19:32 +0000</lastBuildDate>

    
    <item>
      <title>Answers About Web App Architecture For Small Teams</title>
      <link>http://bitmonkey.net/2014/09/16/answers-about-web-app-architecture/</link>
      <guid>http://bitmonkey.net/2014/09/16/answers-about-web-app-architecture</guid>
      <author>Eric Lindvall</author>
      <pubDate>Tue, 16 Sep 2014 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;Brent Simmons &lt;a href=&quot;http://inessential.com/2014/09/04/web_app_architecture_question&quot;&gt;asked some questions&lt;/a&gt; recently about Web App Architecture.&lt;/p&gt;

&lt;p&gt;At &lt;a href=&quot;https://papertrailapp.com/&quot;&gt;Papertrail&lt;/a&gt; I’ve been living a lot of this stuff on a daily basis for some years now and wanted talk about it from the perspective of being on a small team.&lt;/p&gt;

&lt;p&gt;In thinking about the questions, I realized that a lot of these architectural questions are much more to do with business decisions than people realize. Cliff Moon recently had a &lt;a href=&quot;http://boundary.com/blog/2014/08/27/microservices-conways-law/&quot;&gt;great post about microservices&lt;/a&gt; that is worth reading for a very grounded perspective on why microservices can be great.&lt;/p&gt;

&lt;p&gt;There is a sentence in particular that I think is very valuable to think about:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An important corollary of Conway’s law, in my experience, has been that teams will tend to scale about as well as the software that they create.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the big benefits of a distributed architecture is providing a structure for communication and collaboration with large numbers of people. The fundamental problem that everyone is trying to solve is reducing friction. The funny thing about friction is that it is caused by different things depending on the size and makeup of the organization.&lt;/p&gt;

&lt;p&gt;So, stepping back for a moment, one of the prerequisites of making these decisions is understanding what sort of service and what sort of company you’re trying to build.&lt;/p&gt;

&lt;p&gt;What sort of growth do you expect, in terms of users and traffic, but more importantly in terms of your engineering team?&lt;/p&gt;

&lt;p&gt;Are you on a rocket ship where you’re going to be going crazy from the very beginning and never stop with 100 developers before you know it?&lt;/p&gt;

&lt;p&gt;Are you going to be growing organically and plan on keeping your team small for a few years?&lt;/p&gt;

&lt;p&gt;These different paths will give you very different requirements.  If you are planning on staying a small team (say, less than 3-5 engineers) for a few years, the friction you feel is going to be coming from some place different than communication and coordination between engineers.&lt;/p&gt;

&lt;p&gt;When you’re starting out, and when you’re small, the speed at which you can make changes and improvements makes all the difference in the world. Having a bunch of separate services with interfaces and contracts just means that you have to make the same change in more places and have to do busywork to share code.&lt;/p&gt;

&lt;p&gt;What can you do to reduce the friction required to push out that new feature or fix that bug? How can you reduce the number of steps that it takes to get a change into the hands of your users? Having code in a single repository, using an established web framework like Rails or Django can help a lot in reducing those steps. Don’t be scared of monolithic web apps when you’re small. Being small can be an advantage. Use it.&lt;/p&gt;

&lt;p&gt;Predictions of the future are seldom accurate. Be mentally prepared for your architecture and your needs to be very different a year from now.&lt;/p&gt;

&lt;p&gt;Never stop reevaluating your past decisions. It’s likely you’ll reach a point where you need to start breaking out your codebase into smaller components. Don’t consider that a failure of your past self.&lt;/p&gt;

&lt;p&gt;A fundamental part of scaling a growing service is that you’re never done. If you are continuing to grow, there will always be more and different bottlenecks that crop up. There will never be a piece of technology or a style of development or architecture that will mitigate that.&lt;/p&gt;

&lt;p&gt;Don’t try to be too clever. Assume you’ll have to change everything at some point in ways you never expected and don’t let that scare you. It’s a lot of fun.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Introducing Metriks</title>
      <link>http://bitmonkey.net/2012/03/06/introducing-metriks/</link>
      <guid>http://bitmonkey.net/2012/03/06/introducing-metriks</guid>
      <author>Eric Lindvall</author>
      <pubDate>Tue, 06 Mar 2012 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;I was very inspired by &lt;a href=&quot;http://codahale.com/&quot;&gt;Coda Hale&lt;/a&gt;’s &lt;a href=&quot;http://codahale.com/codeconf-2011-04-09-metrics-metrics-everywhere.pdf&quot;&gt;Metrics Metrics Everywhere&lt;/a&gt; talk at CodeConf 2011 and have spent a lot of time over the past year thinking about it. After seeing &lt;a href=&quot;https://github.com/github/rack-statsd&quot;&gt;rack-statsd&lt;/a&gt; and how it kept important process stats in the proctitle, I wanted the same thing for the background tasks that run &lt;a href=&quot;https://papertrailapp.com/&quot;&gt;Papertrail&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hadn’t been able to find a metrics library for ruby that provided the calculations I was looking for, so I decided to experiment with creating one myself.&lt;/p&gt;

&lt;p&gt;The end result was an API for measurement that I’m really proud of. For example, to calculate how much work a process is doing, use a meter to measure how many times an event has happened by calling the &lt;code class=&quot;highlighter-rouge&quot;&gt;mark()&lt;/code&gt; method on the meter:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def perform(job)
  Metriks.meter('tasks').mark
  # the work
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The meter provides methods to give a 1, 5 and 15 minute average rate-per-second. The process title can be updated in with the rate returned by &lt;code class=&quot;highlighter-rouge&quot;&gt;one_minute_average()&lt;/code&gt; in another thread:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;meter = Metriks.meter('tasks')
loop do
  $0 = &quot;worker: #{meter.one_minute_average.to_i} tasks/sec&quot;
  sleep 5
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;…which will give you the output from &lt;code class=&quot;highlighter-rouge&quot;&gt;ps ax&lt;/code&gt; that looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;no-highlight&quot;&gt;22665 ?        S     17:09 worker: 273 tasks/sec&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once you are tracking those metrics in-process it becomes very easy to start sending them to remote services in all sorts of ways.&lt;/p&gt;

&lt;p&gt;The library is called &lt;a href=&quot;https://github.com/eric/metriks/&quot;&gt;Metriks&lt;/a&gt;, an experiment in creating a ruby metrics library with a simple interface and the ability to send the metrics to a number of services.&lt;/p&gt;

&lt;h2 id=&quot;what-sort-of-stuff-are-we-tracking&quot;&gt;What sort of stuff are we tracking?&lt;/h2&gt;

&lt;p&gt;Error rates, database insertion times, cache hits vs misses, messages-per-second processed by workers.&lt;/p&gt;

&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;

&lt;p&gt;The main components of the library are the metrics and reporters.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Metrics are responsible for doing a specific kind of measurement&lt;/li&gt;
  &lt;li&gt;Reporters are responsible for sending metrics to a specific destination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today you can send to &lt;a href=&quot;http://graphite.wikidot.com/&quot;&gt;Graphite&lt;/a&gt;, &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt; and a log file.&lt;/p&gt;

&lt;p&gt;Here’s a quick review of what it’s like to use it and what the important pieces of the library are. It isn’t a huge amount of code, so please &lt;a href=&quot;https://github.com/eric/metriks/&quot;&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any questions or comments, feel free to &lt;a href=&quot;https://twitter.com/lindvall&quot;&gt;say hi on twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;what-does-it-look-like-to-use-it&quot;&gt;What does it look like to use it?&lt;/h2&gt;

&lt;h3 id=&quot;using-a-meter-to-track-web-requests&quot;&gt;Using a meter to track web requests&lt;/h3&gt;

&lt;p&gt;To track the number of requests per second a rack app was doing but didn’t care about timing info, use a &lt;code class=&quot;highlighter-rouge&quot;&gt;Metriks::Meter&lt;/code&gt; which can be created by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;Metriks.meter('name')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s a very simple example of what it would look like to track the number of requests per second a rack app was doing.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class MetriksMiddleware &amp;lt; Rack::Middleware
  def initialize(app)
    @app = app
  end
  def call(env)
    Metriks.meter('rack.requests').mark
    @app.call(env)
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Sending that metric to &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt; could get you a pretty graph looking like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://img.skitch.com/20120306-1fyk9p6n3f5j58rsnh4rrq1x2y.png&quot; alt=&quot;Librato Metrics Meter&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;using-a-timer-to-measure-how-long-a-method-takes-to-run&quot;&gt;Using a timer to measure how long a method takes to run&lt;/h3&gt;

&lt;p&gt;To track how long it took to run a method, use a &lt;code class=&quot;highlighter-rouge&quot;&gt;Metriks::Timer&lt;/code&gt; which can be created with &lt;code class=&quot;highlighter-rouge&quot;&gt;Metriks.timer('name')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To time how long it took to run a &lt;code class=&quot;highlighter-rouge&quot;&gt;fib()&lt;/code&gt; method, all it takes is:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def fib(n)
  n &amp;lt; 2 ? n : fib(n-1) + fib(n-2)
end

Metriks.timer('fib.time').time do
  puts fib(10)
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Sending that metric to &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt; could get you a pretty graph looking like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://img.skitch.com/20120306-kef4nbpnmsr155y7weiaa9acmb.png&quot; alt=&quot;Librato Metrics Timer&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;try-it&quot;&gt;Try it&lt;/h2&gt;

&lt;p&gt;To install the gem just add this to your &lt;code class=&quot;highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;gem 'metriks'&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The source is available &lt;a href=&quot;https://github.com/eric/metriks/&quot;&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;metrics-supported&quot;&gt;Metrics supported&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Meter&lt;/strong&gt;: used to measure the rate that something is happening (number of times per second a method is called).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Timer&lt;/strong&gt;: used to measure how long it takes to perform something. Also contains a meter to track how many times it’s happening.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Counter&lt;/strong&gt;: used to keep track of how many times something has happened since the process started. This is mostly used by the other metrics and isn’t often used directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;meter&quot;&gt;Meter&lt;/h3&gt;

&lt;p&gt;Meters are used to keep track of a rate of an action (how many times per second it happens).&lt;/p&gt;

&lt;p&gt;To mark when an action is performed:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Metriks.meter('calls').mark&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;timer&quot;&gt;Timer&lt;/h3&gt;

&lt;p&gt;Timers are used to keep track of how long it takes for an action to take. It also contains a &lt;code class=&quot;highlighter-rouge&quot;&gt;Meter&lt;/code&gt; in it to track how often it happens.&lt;/p&gt;

&lt;p&gt;To measure how long an action takes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Metriks.timer('fib.duration').time do
  fib(10)
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It’s also possible to use it without making it a block:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;timer = Metriks.timer('fib.duration').time
fib(10)
timer.stop
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;counter&quot;&gt;Counter&lt;/h3&gt;

&lt;p&gt;Counters are used to keep track of an absolute number. They can be incremented and decremented. This metric is generally used as the basis for other metrics instead of being one that would be used directly.&lt;/p&gt;

&lt;p&gt;To increment:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Metriks.counter('calls').increment&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;reporters&quot;&gt;Reporters&lt;/h2&gt;

&lt;p&gt;Reporters take a Registry and report the metrics to a remote store.&lt;/p&gt;

&lt;p&gt;For a detailed overview of the reporter API, it’s available &lt;a href=&quot;https://github.com/eric/metriks/wiki/Reporters&quot;&gt;on the wiki&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;metriksreportergraphite&quot;&gt;Metriks::Reporter::Graphite&lt;/h3&gt;

&lt;p&gt;Sends metrics to &lt;a href=&quot;http://graphite.wikidot.com/&quot;&gt;graphite&lt;/a&gt; on a set interval. It takes a &lt;code class=&quot;highlighter-rouge&quot;&gt;host&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;port&lt;/code&gt; of the carbon agent as required arguments. Example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Metriks::Reporter::Graphite.new('localhost', 3309).start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;metriksreporterlibratometrics&quot;&gt;Metriks::Reporter::LibratoMetrics&lt;/h3&gt;

&lt;p&gt;Send metrics to &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt; on a specified interval. It takes the &lt;a href=&quot;https://metrics.librato.com/account&quot;&gt;API credentials&lt;/a&gt; as two required arguments: &lt;code class=&quot;highlighter-rouge&quot;&gt;email&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;token&lt;/code&gt;. Example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Metriks::Reporter::LibratoMetrics.new('user@metriks.local', '186dbe1cf215').start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;metriksreporterlogger&quot;&gt;Metriks::Reporter::Logger&lt;/h3&gt;

&lt;p&gt;Sends metrics to a logger on a specified interval. Example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;logger = Logger.new('log/metrics.log')
Metriks::Reporter::Logger.new(:logger =&amp;gt; logger).start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The main reason behind this reporter was that I wanted to be able to aggregate the metrics collected by multiple processes on the same system before they were sent to &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt; similarly to how &lt;a href=&quot;https://github.com/etsy/statsd/&quot;&gt;StatsD&lt;/a&gt; does. To facilitate that, I created a &lt;a href=&quot;https://papertrailapp.com/&quot;&gt;Papertrail&lt;/a&gt; webhook receiver that took the logs, parsed the metrics, and submitted them to &lt;a href=&quot;https://metrics.librato.com/&quot;&gt;Librato Metrics&lt;/a&gt;. The work resulted in a Sinatra app I’ve posted on GitHub at &lt;a href=&quot;https://github.com/eric/metriks_log_webhook&quot;&gt;metriks_log_webhook&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;metriksreporterproctitle&quot;&gt;Metriks::Reporter::ProcTitle&lt;/h3&gt;

&lt;p&gt;Being inspired by &lt;a href=&quot;https://github.com/github/rack-statsd&quot;&gt;rack-statsd&lt;/a&gt; I realized there are many metrics that are deep in my processes that would be very interesting to keep track of how my workers are running.&lt;/p&gt;

&lt;p&gt;This reporter isn’t really like the others. It reports metrics by updating your proctitle so you can see select metrics when you run &lt;code class=&quot;highlighter-rouge&quot;&gt;ps aux&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because space in the process title is limited, it requires configuration to specify what metrics are reported.&lt;/p&gt;

&lt;p&gt;Example usage:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;reporter = Metriks::Reporter::ProcTitle.new
reporter.add 'reqs', 'sec' do
  Metriks.meter('rack.requests').one_minute_rate
end
reporter.start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It would allow you to see the metric when you run &lt;code class=&quot;highlighter-rouge&quot;&gt;ps ax&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;no-highlight&quot;&gt;22665 ?        S     17:09 thin reqs: 273.3/sec&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;implementation-details&quot;&gt;Implementation Details&lt;/h2&gt;

&lt;p&gt;Metriks is thread-safe. It uses a combination of mutexes and the &lt;a href=&quot;https://github.com/headius/ruby-atomic&quot;&gt;atomic&lt;/a&gt; gem. Using atomic reduces the need for mutexes without sacrificing thread safety.&lt;/p&gt;

&lt;p&gt;It uses the &lt;a href=&quot;https://github.com/copiousfreetime/hitimes&quot;&gt;hitimes&lt;/a&gt; gem to get high granularity timing data without having to call &lt;code class=&quot;highlighter-rouge&quot;&gt;Time.now.to_f&lt;/code&gt; frequently.&lt;/p&gt;

&lt;p&gt;Metriks doesn’t tie the gathering of metrics to how they are reported. This allows for swapping out where metrics are reported to without having to change any of the instrumentation.&lt;/p&gt;

&lt;p&gt;The metrics classes themselves are mostly ports of the metrics from Coda Hale’s Java &lt;a href=&quot;https://github.com/codahale/metrics/tree/master/metrics-core/src/main/java/com/yammer/metrics&quot;&gt;metrics library&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;thanks&quot;&gt;Thanks&lt;/h2&gt;

&lt;p&gt;Thanks to Troy Davis, Joe Ruscio and Mathias Meyer for their help reviewing drafts of this post.&lt;/p&gt;

</description>
    </item>
    
    <item>
      <title>Make your library a better citizen</title>
      <link>http://bitmonkey.net/2010/03/27/make-your-library-a-better-citizen/</link>
      <guid>http://bitmonkey.net/2010/03/27/make-your-library-a-better-citizen</guid>
      <author>Eric Lindvall</author>
      <pubDate>Sat, 27 Mar 2010 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;I’ve recently ran into a case where I wanted the Rails logger to be at &lt;code class=&quot;highlighter-rouge&quot;&gt;:debug&lt;/code&gt; level, but really didn’t care about the debug output from the &lt;a href=&quot;http://hoptoadapp.com/&quot;&gt;Hoptoad&lt;/a&gt; plugin. It kept giving a big XML dump every time it reported an exception which just added noise to the log file that wasn’t relevant.&lt;/p&gt;

&lt;p&gt;I’ve found many plugins including Dash (may it rest in peace) and &lt;a href=&quot;http://hoptoadapp.com/&quot;&gt;Hoptoad&lt;/a&gt; who would steal the logger from Rails and reference that internally.&lt;/p&gt;

&lt;p&gt;Here’s the logger in &lt;a href=&quot;http://github.com/thoughtbot/hoptoad_notifier/blob/0e68e717b02e504ee5f2424998f56dd087c2de9a/lib/hoptoad_notifier.rb#L60-66&quot;&gt;hoptoad&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Look for the Rails logger currently defined
def logger
  if defined?(Rails.logger)
    Rails.logger
  elsif defined?(RAILS_DEFAULT_LOGGER)
    RAILS_DEFAULT_LOGGER
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Many times it’s useful to run your staging environment (or even production) with debug logging enabled, but the downside was that now all of these plugins would be logging at that level as well.&lt;/p&gt;

&lt;p&gt;One little change that plugin authors can start to do to help the rest of us out is make sure you &lt;code class=&quot;highlighter-rouge&quot;&gt;#dup&lt;/code&gt; the logger before you grab a copy.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def logger
  @logger ||= if defined?(Rails.logger)
    Rails.logger.dup
  elsif defined?(RAILS_DEFAULT_LOGGER)
    RAILS_DEFAULT_LOGGER.dup
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This would allow us to then say:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hoptoad.logger.level = Logger::WARN
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and escape all of the noise from plugins that we don’t care about.&lt;/p&gt;

&lt;p&gt;Much thanks to &lt;a href=&quot;http://twitter.com/wbruce&quot;&gt;Bruce Williams&lt;/a&gt; for first proposing this trick.&lt;/p&gt;

</description>
    </item>
    
    <item>
      <title>PSA: Doing less is more with system and exec</title>
      <link>http://bitmonkey.net/2010/01/20/psa-doing-less-is-more-with-system-and-exec/</link>
      <guid>http://bitmonkey.net/2010/01/20/psa-doing-less-is-more-with-system-and-exec</guid>
      <author>Eric Lindvall</author>
      <pubDate>Wed, 20 Jan 2010 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;I’ve come across a lot of cases where people take an array of arguments and turn it into a string before passing them to &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;exec&lt;/code&gt;. I want to spread the news that it isn’t needed and that we can save ourselves from bugs in the future with a little &lt;em&gt;less&lt;/em&gt; code.&lt;/p&gt;

&lt;p&gt;I don’t mean to pick on &lt;a href=&quot;http://twitter.com/defunkt&quot;&gt;defunkt&lt;/a&gt; specifically — it’s a pattern I see all the time, but this example is one I had available:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def self.perform(*args)
  script = args.join(' ')
  puts &quot;$ #{script}&quot;
  Open3.popen3(args.join(' ')) do |stdin, stdout, stderr|
    puts stdout.read.inspect
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can see the code on GitHub &lt;a href=&quot;https://gist.github.com/225360/4820eef38ceb979dee2672577a15f6d4ea09e358&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What is &lt;code class=&quot;highlighter-rouge&quot;&gt;Open3.popen3&lt;/code&gt; really doing? It calls a bunch of stuff, like &lt;code class=&quot;highlighter-rouge&quot;&gt;IO::pipe&lt;/code&gt;, but in the end, it &lt;a href=&quot;http://github.com/FooBarWidget/rubyenterpriseedition187/blob/d0546e75c86da27b8f184820166ea0854608349b/lib/open3.rb#L67&quot;&gt;calls &lt;code class=&quot;highlighter-rouge&quot;&gt;exec&lt;/code&gt;&lt;/a&gt; with the same arguments that were passed to it:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;exec(*cmd)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see the whole source for &lt;code class=&quot;highlighter-rouge&quot;&gt;popen3&lt;/code&gt; &lt;a href=&quot;http://github.com/FooBarWidget/rubyenterpriseedition187/blob/d0546e75c86da27b8f184820166ea0854608349b/lib/open3.rb&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What happens if we pass a multi-word argument to &lt;code class=&quot;highlighter-rouge&quot;&gt;perform&lt;/code&gt;?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;ShellJob.perform('rm', '/Library/Keyboard Layouts/Qwerty.bundle')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The arguments are joined and turn into:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&quot;rm /Library/Keyboard Layouts/Qwerty.bundle&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When this is passed to the ruby &lt;code class=&quot;highlighter-rouge&quot;&gt;exec&lt;/code&gt; method, the results are parsed with the ruby shell argument parser and it ends up with a child process holding an &lt;code class=&quot;highlighter-rouge&quot;&gt;ARGV&lt;/code&gt; of:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;[&quot;rm&quot;, &quot;/Library/Keyboard&quot;, &quot;Layouts/Qwerty.bundle&quot;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;…but this isn’t what we wanted!&lt;/p&gt;

&lt;p&gt;To understand the basics of what is going on, we have to understand the underlying call that the ruby interpreter calls: &lt;code class=&quot;highlighter-rouge&quot;&gt;execv(3)&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;int
execv(const char *path, char *const argv[]);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;argv[]&lt;/code&gt; array that is passed into &lt;code class=&quot;highlighter-rouge&quot;&gt;execv(3)&lt;/code&gt; is copied into the child process as &lt;code class=&quot;highlighter-rouge&quot;&gt;ARGV&lt;/code&gt;. Handy isn’t it?&lt;/p&gt;

&lt;p&gt;The problem is, there is no version of the C function that takes a single &lt;code class=&quot;highlighter-rouge&quot;&gt;char *&lt;/code&gt; of all of the arguments and Does The Right Thing. Because of this, the ruby interpreter has to fake it, using an approximation of how the shell parses arguments.&lt;/p&gt;

&lt;p&gt;But there &lt;strong&gt;is&lt;/strong&gt; a better way.&lt;/p&gt;

&lt;p&gt;From the documentation for &lt;a href=&quot;http://ruby-doc.org/core/classes/Kernel.html#M005968&quot;&gt;Kernel#exec&lt;/a&gt; &lt;em&gt;(emphasis mine)&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Replaces the current process by running the given external command. If exec is given a single argument, that argument is taken as a line that is subject to shell expansion before being executed. &lt;strong&gt;If multiple arguments are given, the second and subsequent arguments are passed as parameters to command with no shell expansion.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What’s great is, in this case (and in a lot of them), we actually already have an array with all of the arguments in it.&lt;/p&gt;

&lt;p&gt;So, we can actually just change the &lt;code class=&quot;highlighter-rouge&quot;&gt;popen3&lt;/code&gt; line to say:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Open3.popen3(*args) do |stdin, stdout, stderr|
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and everything will work the way we want.&lt;/p&gt;

&lt;p&gt;You can see the full code on GitHub &lt;a href=&quot;http://gist.github.com/226703&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    
    <item>
      <title>Making it easy to run a single test</title>
      <link>http://bitmonkey.net/2010/01/15/making-it-easy-to-run-a-single-test/</link>
      <guid>http://bitmonkey.net/2010/01/15/making-it-easy-to-run-a-single-test</guid>
      <author>Eric Lindvall</author>
      <pubDate>Fri, 15 Jan 2010 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;In the course of normal development I find myself focusing on specific tests that I want to make sure work properly before moving on to testing the entire test suite.&lt;/p&gt;

&lt;p&gt;There is a really great argument to &lt;code class=&quot;highlighter-rouge&quot;&gt;test/unit&lt;/code&gt; called &lt;code class=&quot;highlighter-rouge&quot;&gt;--name&lt;/code&gt; which will allow you to specify the test to run.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;test/unit&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;rake&lt;/code&gt; tasks provide the &lt;code class=&quot;highlighter-rouge&quot;&gt;TESTOPTS&lt;/code&gt; environment variable as a means to pass options to the tests runner. To run a specific test we can add our &lt;code class=&quot;highlighter-rouge&quot;&gt;--name&lt;/code&gt; option to &lt;code class=&quot;highlighter-rouge&quot;&gt;TESTOPTS&lt;/code&gt; like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake test:units TEST=test/unit/user_test.rb TESTOPTS=&quot;--name=test_should_create_user&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;--name&lt;/code&gt; option even takes a regular expression, so we can even do this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake test:units TEST=test/unit/user_test.rb TESTOPTS=&quot;--name='/create_user/'&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Because it’s a pain in the ass to remember the option as well as doing the right amount of escaping and quoting, I created a simple rake task that you can have run before your real test tasks to set &lt;code class=&quot;highlighter-rouge&quot;&gt;TESTOPTS&lt;/code&gt; for you. It looks in the &lt;code class=&quot;highlighter-rouge&quot;&gt;TESTNAME&lt;/code&gt; environment variable and if it exists, sets &lt;code class=&quot;highlighter-rouge&quot;&gt;TESTOPTS&lt;/code&gt; with the correct value.&lt;/p&gt;

&lt;p&gt;To pull this off, you can just put the following in &lt;code class=&quot;highlighter-rouge&quot;&gt;lib/tasks/test_name.rake&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;namespace :test do
  task :populate_testopts do
    if ENV['TESTNAME'].present?
      ENV['TESTOPTS']  = ENV['TESTOPTS'] ? &quot;#{ENV['TESTOPTS']} &quot; : ''
      ENV['TESTOPTS'] += &quot;--name=#{ENV['TESTNAME'].inspect}&quot;
    end    
  end
end

%w(test:units test:functionals test:integration).each do |task_name|
  Rake::Task[task_name].prerequisites &amp;lt;&amp;lt; 'test:populate_testopts'
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can find the gist &lt;a href=&quot;http://gist.github.com/276854&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The only thing tricky about this code is that we go and stick an entry in the &lt;code class=&quot;highlighter-rouge&quot;&gt;prerequisites&lt;/code&gt; array that each &lt;code class=&quot;highlighter-rouge&quot;&gt;Rake::Task&lt;/code&gt; has to make sure that it runs our environment filter code before it runs the task itself.&lt;/p&gt;

&lt;p&gt;Once we’ve done that, we can run the tests we want with:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ rake test:units TEST=test/unit/user_test.rb TESTNAME='/create_user/'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

</description>
    </item>
    
    <item>
      <title>Quick tip for up-to-date Scout graphs</title>
      <link>http://bitmonkey.net/2010/01/07/quick-tip-for-up-to-date-scout-graphs/</link>
      <guid>http://bitmonkey.net/2010/01/07/quick-tip-for-up-to-date-scout-graphs</guid>
      <author>Eric Lindvall</author>
      <pubDate>Thu, 07 Jan 2010 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;One of the things that’s always made me sad when using &lt;a href=&quot;http://scoutapp.com/&quot;&gt;Scout&lt;/a&gt; has been having to refresh the page to reload the graphs.&lt;/p&gt;

&lt;p&gt;Fortunately &lt;a href=&quot;http://amcharts.com/&quot;&gt;amcharts&lt;/a&gt;, the charting engine used by Scout, has the ability to do refreshing.&lt;/p&gt;

&lt;p&gt;Here’s a nice little bookmarklet to make the Scout graphs refresh every 60 seconds.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Drag this link:&lt;/em&gt; &lt;strong&gt;&lt;a href=&quot;javascript:(function(){$('#amstock').get(0).setSettings('&amp;lt;settings&amp;gt;&amp;lt;data_reloading&amp;gt;&amp;lt;interval&amp;gt;60&amp;lt;/interval&amp;gt;&amp;lt;show_preloader&amp;gt;false&amp;lt;/show_preloader&amp;gt;&amp;lt;reset_period&amp;gt;true&amp;lt;/reset_period&amp;gt;&amp;lt;/data_reloading&amp;gt;&amp;lt;/settings&amp;gt;');})()&quot;&gt;Scout Reload&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To install the bookmarklet, drag the “Scout Reload” link to your bookmark bar.&lt;/p&gt;

&lt;p&gt;To use it, click on the bookmarklet when you are on a Scout graph page.&lt;/p&gt;

&lt;p&gt;For more hints on how to install bookmarklets, you can see how to install the Delicious bookmarklet &lt;a href=&quot;http://delicious.com/help/bookmarklets&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;what-is-it-doing&quot;&gt;What is it doing?&lt;/h3&gt;

&lt;p&gt;Amcharts provides all sorts of &lt;a href=&quot;http://www.amcharts.com/docs/v.1/stock/settings/settings_reference&quot;&gt;great functionality&lt;/a&gt;, including a simple refresh mechanism.&lt;/p&gt;

&lt;p&gt;What we do is update the XML configuration via javascript with this chunk:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;settings&amp;gt;
  &amp;lt;data_reloading&amp;gt;
    &amp;lt;interval&amp;gt;60&amp;lt;/interval&amp;gt;
    &amp;lt;show_preloader&amp;gt;false&amp;lt;/show_preloader&amp;gt;
    &amp;lt;reset_period&amp;gt;true&amp;lt;/reset_period&amp;gt;
  &amp;lt;/data_reloading&amp;gt;
&amp;lt;/settings&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To send that chunk of XML to the chart is very strait forward. We just get a reference to the DOM object that is the chart and call &lt;code class=&quot;highlighter-rouge&quot;&gt;setSettings&lt;/code&gt; on it, passing it the XML we want:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$(&quot;#amstock&quot;).get(0).setSettings(xml_settings_string);
&lt;/code&gt;&lt;/pre&gt;

</description>
    </item>
    
    <item>
      <title>Abusing HTTParty for a Tender client</title>
      <link>http://bitmonkey.net/2010/01/06/abusing-httparty-for-a-tender-client/</link>
      <guid>http://bitmonkey.net/2010/01/06/abusing-httparty-for-a-tender-client</guid>
      <author>Eric Lindvall</author>
      <pubDate>Wed, 06 Jan 2010 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;We all know how great &lt;a href=&quot;http://tenderapp.com/&quot;&gt;Tender&lt;/a&gt; is and had a chance to play with their great &lt;a href=&quot;https://help.tenderapp.com/faqs/api/introduction&quot;&gt;API&lt;/a&gt; while writing a &lt;a href=&quot;http://github.com/eric/tender_summary/&quot;&gt;tool to send out an email every morning&lt;/a&gt; with any pending issues.&lt;/p&gt;

&lt;p&gt;They’ve done some really great stuff with it, especially with their use of &lt;a href=&quot;http://bitworking.org/projects/URI-Templates/&quot;&gt;URI Templates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you haven’t played with URI Templates before, an example of their JSON with a template in it looks like:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;site_href&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;http://api.tenderapp.com/{site_permalink}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;which, with the &lt;a href=&quot;http://github.com/sporkmonger/addressable&quot;&gt;Addressable&lt;/a&gt; gem would allow us to say:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt; require 'addressable/template'
&amp;gt;&amp;gt; tmpl = Addressable::Template.new('http://api.tenderapp.com/{site_permalink}')
&amp;gt;&amp;gt; tmpl.expand('site_permalink' =&amp;gt; 'help').to_s
=&amp;gt; http://api.tenderapp.com/help
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To make things nice and strait forward, Tender follows the convention of using &lt;code class=&quot;highlighter-rouge&quot;&gt;_href&lt;/code&gt; as the suffix for any key that contains a template. I was able to use this along with a little magic I snuck into Hash to make my code that much prettier.&lt;/p&gt;

&lt;p&gt;All that was required to expand the templates was a little module:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;module JsonHelpers
  def href(key, opts = {})
    Addressable::Template.new(self[&quot;#{key}_href&quot;]).expand(opts).to_s
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;which I was able to mix in to any &lt;code class=&quot;highlighter-rouge&quot;&gt;Hash&lt;/code&gt; that was returned:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Make the JSON a litte bit more fun to work with
def self.add_json_helpers(data)
  case data
  when Hash
    data.send(:extend, TenderSummary::JsonHelpers)
    data.each { |_, value| add_json_helpers(value) }
  when Array
    data.each { |elem| add_json_helpers(elem) }
  end
  data
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and then tell &lt;code class=&quot;highlighter-rouge&quot;&gt;HTTParty&lt;/code&gt; to use it by specifying a custom &lt;code class=&quot;highlighter-rouge&quot;&gt;parser&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;module TenderSummary
  class TenderApi
    include HTTParty
 
    headers  'Accept' =&amp;gt; 'application/vnd.tender-v1+json'
    format   :json
    parser   Proc.new { |b| add_json_helpers(Crack::JSON.parse(b)) }

    # ...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;With all this done, it made it very simple to generate the URLs to the resources I wanted access to.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# assuming `site` holds the JSON returned from
# 'http://api.tenderapp.com/{site_permalink}'
discussion_url = site.href(:discussions, :state =&amp;gt; :pending)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can see the entire class of &lt;a href=&quot;http://github.com/eric/tender_summary/blob/master/lib/tender_summary/tender_api.rb&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;TenderApi&lt;/code&gt;&lt;/a&gt; on GitHub.&lt;/p&gt;

</description>
    </item>
    
    <item>
      <title>Tracking initial memory usage by file in Ruby</title>
      <link>http://bitmonkey.net/2009/12/30/tracking-initial-memory-usage-by-file-in-ruby/</link>
      <guid>http://bitmonkey.net/2009/12/30/tracking-initial-memory-usage-by-file-in-ruby</guid>
      <author>Eric Lindvall</author>
      <pubDate>Wed, 30 Dec 2009 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;I’ve found that as a project progresses, the initial memory usage of a Rails application seems to grow more and more.&lt;/p&gt;

&lt;p&gt;The more time I spend trying to track down memory leaks (or just pieces of code that use more memory than they should) the more I realize that it’s a fairly imprecise science. I’ve had the best luck using tools to give me a good idea of where to start poking around. From there it’s just a matter of looking at the code and finding what silly things people are doing.&lt;/p&gt;

&lt;p&gt;If I wanted to see what was contributing to the large memory footprint of an application on startup, tracking how much memory was allocated during each &lt;code class=&quot;highlighter-rouge&quot;&gt;require&lt;/code&gt; would give me a good place to start.&lt;/p&gt;

&lt;p&gt;The code to do this is amazingly strait forward:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;module RequireTracking
  def require(*args)
    start_size = GC.allocated_size
    super
  ensure
    $require_stats[args.first] += (GC.allocated_size - start_size)
  end
end
 
Object.send(:include, RequireTracking)
Kernel.send(:include, RequireTracking)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;The entire implementation is available &lt;a href=&quot;http://gist.github.com/264496&quot;&gt;as a gist&lt;/a&gt; on GitHub.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;GC.allocated_size&lt;/code&gt; method is included in the &lt;a href=&quot;http://railsbench.rubyforge.org/svn/trunk/railsbench/GCPATCH&quot;&gt;RailsBench GC patch&lt;/a&gt; which is part of the &lt;a href=&quot;http://www.rubyenterpriseedition.com/&quot;&gt;Ruby Enterprise Edition&lt;/a&gt; interpreter.&lt;/p&gt;

&lt;p&gt;One thing to understand is this is only tracking how much memory was allocated but not how much was freed. This will cause these statistics to include memory that was temporarily allocated and then no longer referenced. This can be useful because even temporarily using lots of memory can negatively impact startup time.&lt;/p&gt;

&lt;p&gt;Another aspect to understand is the numbers we are tracking are what are normally called “self + children” in profilers. This means that all memory allocated by a file as well as anything allocated by files that are required from it are included in the statistics. This results in the same memory being counted multiple times, but is useful in understanding the total memory implications of requiring a file.&lt;/p&gt;

&lt;p&gt;Running this on one of the projects I was working on found this little gem:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Webster
  DICTIONARY = File.open(File.join(File.dirname(__FILE__), 'words')) do |file|
    file.readlines.collect {|each| each.chomp}
  end
  
  def random_word
    DICTIONARY[rand(DICTIONARY.size)]
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;You can find the source on GitHub &lt;a href=&quot;http://github.com/dancroak/webster/blob/09087e5d10ee950513018794f81f36ccd875eba9/lib/webster.rb&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This would be a prime candidate for refactoring if you are worried about your memory usage.&lt;/p&gt;

</description>
    </item>
    
    <item>
      <title>Add NewRelic instrumentation for ThinkingSphinx</title>
      <link>http://bitmonkey.net/2009/12/29/add-newrelic-instrumentation-for-thinkingsphinx/</link>
      <guid>http://bitmonkey.net/2009/12/29/add-newrelic-instrumentation-for-thinkingsphinx</guid>
      <author>Eric Lindvall</author>
      <pubDate>Tue, 29 Dec 2009 00:00:00 +0000</pubDate>
      <description>&lt;p&gt;&lt;a href=&quot;http://newrelic.com/&quot;&gt;NewRelic&lt;/a&gt; provides a really great mechanism in their plugin to instrument just about anything.&lt;/p&gt;

&lt;p&gt;One of the things I found when analyzing actions in NewRelic was that all of the time that was being spent in the ThinkingSphinx methods were being attributed to the template instead of the model. As we all know, mis-attribution of time spent can make tracking down trouble spots in your code much more difficult.&lt;/p&gt;

&lt;p&gt;It ends up that all that is required to start tracking the time you are searching in ThinkingSphinx is a couple calls to &lt;code class=&quot;highlighter-rouge&quot;&gt;add_method_tracer&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;add_method_tracer :search, 'ActiveRecord/#{self.name}/search'
add_method_tracer :search, 'ActiveRecord/search', :push_scope =&amp;gt; false
add_method_tracer :search, 'ActiveRecord/all', :push_scope =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;You can see the full code &lt;a href=&quot;http://gist.github.com/219071&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once you’ve required the code, you’ll start to see the &lt;code class=&quot;highlighter-rouge&quot;&gt;#search&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;#search_count&lt;/code&gt; methods show up in your Performance Breakdowns:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://img.skitch.com/20091229-ctihwdbs71jtwq5jibxyp4yygs.png&quot; alt=&quot;Performance Breakdown&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Isn’t that sweet?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; NewRelic has some great documentation for &lt;a href=&quot;http://support.newrelic.com/faqs/docs/custom-metric-collection&quot;&gt;Custom Metric Collection&lt;/a&gt; if you want to do more.&lt;/p&gt;

</description>
    </item>
    
  </channel> 
</rss>
