Skip to content

Instantly share code, notes, and snippets.

@hassox
hassox / benchmark.rb
Created August 30, 2012 23:28 — forked from jnicklas/benchmark.rb
Hash access speed with strings/vs symbols
require "benchmark"
hash = {'key' => 1, :key => 2}
n = 5_000_000
Benchmark.bm do |x|
x.report("strings") { n.times { hash['key'] } }
x.report("symbols") { n.times { hash[:key] } }
x.report("strings, set") { n.times { hash['key'] = 1 } }
@mattetti
mattetti / gist:3458669
Created August 25, 2012 01:47
Instrument ActiveRecord and push the results to Statsd
SQL_PARSER_REGEXP = /^(\w+)\s(\w+)\s\W*(\w+)/
ActiveSupport::Notifications.subscribe "sql.active_record" do |name, start, finish, id, payload|
if payload[:name] == "SQL"
if Thread.current[:stats_context] # where I store the name of the request context
payload[:sql] =~ SQL_PARSER_REGEXP # $1 will be the query type, $3 the table
Statsd.timing("#{Thread.current[:stats_context]}.sql.#{$3}.#{$1}.query_time",
(finish - start) * 1000, 1)
end
end