Skip to content

Instantly share code, notes, and snippets.

@rbranson
rbranson / rails-log-db-times-with-timestamps.sh
Created April 7, 2011 19:44
Extracts a list of timestamped DB times from Rails logs
cat production.log | ruby -rtime -n \
-e 'ts = $_.scan(/at (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)' \
-e 'last_ts = ts if ts.size > 0' \
-e 'm = $_.scan(/DB: (\d+)/)' \
-e 'puts "#{Time.parse(last_ts.to_s).to_i} #{m[0]}" if m.size > 0'
@rbranson
rbranson / module-inheritance.rb
Created April 8, 2011 16:47
modules have inheritance too brah
#
#### output:
#
# Called in [MixinA]
# Called in [MixinC]
# Called in [MixinB]
# Called in [Klass]
# Called in [MixinD]
# Called in [SubKlass]
#
@rbranson
rbranson / show_mysql_index_effectiveness.sql
Created April 22, 2011 21:20
Lists indexes in MySQL by how effective they are (cardinality / rows)
---
--- Make sure you run mysqlcheck -Aa on your database before running this, or
--- that you are reasonably certain your statistics are up to date.
---
SELECT
stats.table_schema,
stats.table_name,
stats.index_name,
GROUP_CONCAT(stats.column_name) AS column_names,
@rbranson
rbranson / extract-rails-completion-time.sh
Created April 28, 2011 05:14
Extract Completion Time and URL (sans IDs and query string) from Rails logs
cat production.log | grep "Completed in" | sed -e 's/.* \([0-9]*\)ms .*\[http.*:\/\/[^/]*\([^\?0-9]*\).*\].*/\1 \2/'
@rbranson
rbranson / cruncher-reducer.c
Created April 28, 2011 07:34
Ghetto C to read key/duration list from STDIN and dumps simple analysis per-key
//
// cruncher-reducer.c
//
// $ cc -o cruncher-reducer cruncher-reducer.c
//
// NOTE: Run data through "sort" first or it won't work. If your keys don't have any
// spaces, it's just something like this to get a proper numeric sort:
//
// $ cat input | sort -k1,2n | cruncher-reducer
//
@rbranson
rbranson / puts-lines.rb
Created June 17, 2011 05:14
It puts lines, dawg
#
# On OS X 10.6.7:
#
# JRuby 1.6.0:
#
# $ time jruby --server puts-lines.rb 10000000 > /dev/null
#
# real 0m52.309s
# user 0m32.801s
# sys 0m16.642s
@rbranson
rbranson / ranges_of_calendar_units_in_range.rb
Created June 20, 2011 22:56
Horrifically inefficient way to extract date ranges of calendar units (year, month, quarter, week) for a certain date range
def ranges_of_calendar_units_in_range(what, range)
b_sym, e_sym = "beginning_of_#{what}".to_sym, "end_of_#{what}".to_sym
range.map { |d| d.send(b_sym) .. d.send(e_sym) }.uniq
end
@rbranson
rbranson / controller_action_helpers.rb
Created June 29, 2011 20:22
RSpec Controller Action Hack
#
# Drop this little gem in your spec/support/helpers
#
###########################################################
#
# context "things" do
# let(:item) { mock_model(Item) }
# action { put :update, :item => item }
#
@rbranson
rbranson / god-of-gods.god
Created July 8, 2011 02:55
God of Gods
#
#
# God of Gods - A god config file
# Rick Branson <rick@diodeware.com>
#
# Runs a god instance for every user on the system, provided
# they have a .godrc file in their home diretory, which is just
# a regular old god config file.
#
#
slower:elasticsearch-0.16.2 rbranson$ lsof -p 17392 | grep lib
ruby 17392 rbranson txt REG 14,2 137368 28211204 /opt/local/lib/libtcmalloc_minimal.0.dylib
ruby 17392 rbranson txt REG 14,2 8528 28284248 /Users/rbranson/.rvm/rubies/ree-1.8.7-2011.03/lib/libsystem_allocator.dylib
ruby 17392 rbranson txt REG 14,2 17176 28283848 /Users/rbranson/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/i686-darwin10.7.0/etc.bundle
ruby 17392 rbranson txt REG 14,2 30640 28283863 /Users/rbranson/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/i686-darwin10.7.0/stringio.bundle
ruby 17392 rbranson txt REG 14,2 8960 28283859 /Users/rbranson/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/i686-darwin10.7.0/rational.bundle
ruby 17392 rbranson txt REG 14,2 9592 28283841 /Users/rbranson/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/i686-darwin10.7.0/digest/sha1.bundle
ruby 17392 rbranson txt REG 14,2 19640 28283843 /Users/rbranson/.