Skip to content

Instantly share code, notes, and snippets.

@eric
eric / newrelic_thinking_sphinx.rb
Created October 26, 2009 21:41
NewRelic instrumentation for ThinkingSphinx
# NewRelic instrumentation for ThinkingSphinx
if defined? ActiveRecord
ActiveRecord::Base.class_eval do
class << self
add_method_tracer :search, 'ActiveRecord/#{self.name}/search'
add_method_tracer :search, 'ActiveRecord/search', :push_scope => false
add_method_tracer :search, 'ActiveRecord/all', :push_scope => false
add_method_tracer :search_count, 'ActiveRecord/#{self.name}/search_count'
add_method_tracer :search_count, 'ActiveRecord/search_count', :push_scope => false
Thread ID: 9261400
Total: 250423 Kb
self calls name
55060 Kb 215738 God::Logger#log
via Object#applog
54357 Kb 431476 String#%
via God::Logger#log
28095 Kb 359580 <Class::Time>#allocate
via <Class::Time>#now
@eric
eric / memory_printer.rb
Created October 26, 2009 17:31
A ruby-prof printer to use with RubyProf::MEMORY
class MemoryPrinter < RubyProf::AbstractPrinter
# Print a flat profile report to the provided output.
#
# output - Any IO oject, including STDOUT or a file.
# The default value is STDOUT.
#
# options - Hash of print options. See #setup_options
# for more information.
#
def print(output = STDOUT, options = {})
> ps auxww | grep timeout-test ; sleep 1 ; ps auxww | grep timeout-test
eric 29741 92.1 5.2 66660 27444 pts/4 R+ 20:50 0:38 ruby timeout-test.rb
eric 29741 92.2 5.2 66656 27708 pts/4 R+ 20:50 0:39 ruby timeout-test.rb
> ps auxww | grep timeout-test ; sleep 5 ; ps auxww | grep timeout-test
eric 29741 92.9 5.5 68704 29220 pts/4 R+ 20:50 0:45 ruby timeout-test.rb
eric 29741 93.1 5.8 69728 30456 pts/4 R+ 20:50 0:50 ruby timeout-test.rb
> ps auxww | grep timeout-test ; sleep 5 ; ps auxww | grep timeout-test
eric 29741 92.3 5.9 70756 31340 pts/4 R+ 20:50 0:53 ruby timeout-test.rb
eric 29741 92.4 6.2 71776 32528 pts/4 R+ 20:50 0:58 ruby timeout-test.rb
> ruby -v
Thread ID: 16553080
Total: 33 Kb
self calls name
32 Kb 1 <Module::Timeout>#timeout
0 Kb 3 Kernel#sleep
Thread ID: 9213520
Total: 114473 Kb
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'ostruct'
require 'god'
God::Logger.syslog = false
templogio = StringIO.new
templogger = God::SimpleLogger.new(templogio)
@eric
eric / _restart.god
Created October 22, 2009 22:44
Simple mechanism to make sure god doesn't use more than 40mb
# This relies on god 0.7.22 or later.
#
# Restart god when it reaches 40MB
if defined?(ORIGINAL_ARGV)
Thread.new do
loop do
memory_usage = God::System::Process.new(Process.pid).memory # in kilobytes
if memory_usage > 40.megabytes
applog(nil, :info, "Stopping god server. Memory usage limit reached: #{memory_usage}kb")
# Want to use your own storage-conf.xml instead of the one included with
# the cassandra gem?
#
# Copy files from <cassandra-gem-root>/conf to a directory
# in your project ("cassandra/conf" is used in this example).
#
# Then run "rake cassandra"
#
CASSANDRA_HOME = "#{ENV['HOME']}/cassandra/server"
#!/usr/bin/env ruby
# Riddle at http://bt.tumblr.com/post/212634688
# Moved to http://bt.tumblr.com/post/212669579
translation = Hash[*('a'..'z').zip(('a'..'z').to_a.reverse).flatten]
translation.each { |k,v| translation[k.upcase] = v.upcase }
puts DATA.read.gsub(/./) { |c| translation[c] || c }
#!/usr/bin/env ruby
# Riddle at http://bt.tumblr.com/post/202253034
# Lower case letters
letters = ('a'..'z').to_a
translated_letters = letters.dup
12.times { translated_letters.push translated_letters.shift }
# Upper case letters