Skip to content

Instantly share code, notes, and snippets.

@mtkd
mtkd / gist:516046
Created August 9, 2010 20:26
Ruby to get process memory usage
`ps -o rss= -p #{$$}`.to_i
@mtkd
mtkd / gist:586685
Created September 19, 2010 11:24
Mongoid references_one persistance quirk
User (references_one Address)
Address (referenced_in User)
user = User.make
address = Address.make
user.address = address
user.save # does not work, the change does not persist
user.address.save # works and change persists
@mtkd
mtkd / Mongoid_id
Created April 23, 2011 10:23
Mongoid ID issue with state_machine
# tl;dr if state_machine is in the model when a record is created, there is a record in Mongo, but you can't do a find() on it
# If state_machine is in the model when the record is created:
ruby-1.9.2-p0 > Upload.first.id
=> BSON::ObjectId('4db2a774c4054453d000000c')
ruby-1.9.2-p0 > BSON::ObjectId.from_string('4db2a774c4054453d000000c')
=> BSON::ObjectId('4db2a774c4054453d000000c')
@mtkd
mtkd / uuid_benchmark.rb
Created December 24, 2011 22:47
UUID Benchmarks
require 'benchmark'
require 'uuidtools'
require 'ffi-uuid'
require 'uuid'
Benchmark.bm do |x|
x.report { 100000.times do; UUIDTools::UUID.timestamp_create.to_s; end; }
x.report { 100000.times do; UUID.generate.to_s; end; }
x.report { 100000.times do; FFI::UUID.generate_time.to_s; end; }
end
@mtkd
mtkd / json-vs-yajl.rb
Created January 7, 2012 16:20
JSON vs Yajl
require 'benchmark'
require 'json'
require 'yajl'
data = ''
f = File.open("test.json", "r")
f.each_line do |line|
data += line
end
@mtkd
mtkd / ipaddr-to_i-perf.rb
Created January 7, 2012 16:22
IPaddr.new().to_i performance
require 'ipaddr'
require 'benchmark'
def iptoint1(ip)
IPAddr.new(ip, Socket::AF_INET).to_i
end
def iptoint2(ip)
if (ip.kind_of?(String) &&
ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)
@mtkd
mtkd / gist:1974819
Created March 4, 2012 21:15
Models not using attr_accessible
#To see how many models in a Rails project are not using attr_accessible:
$find app/models -type f -name \*.rb | wc -l
$grep -r -m1 "attr_accessible app/models | wc -l
@mtkd
mtkd / gist:1974822
Created March 4, 2012 21:16
JSON parsing benchmarks
require 'benchmark'
require 'json'
require 'yajl'
data = ''
f = File.open("test.json", "r")
f.each_line do |line|
data += line
end
@mtkd
mtkd / gist:2263694
Created March 31, 2012 12:44
wlll: parsing values hack
s = %Q{
Maximum connect burst length: 1
Total: connections 20000 requests 20000 replies 20000 test-duration 592.676 s
Connection rate: 33.7 conn/s (29.6 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 10.8 avg 29.6 max 11652.5 median 11.5 stddev 217.1
Connection time [ms]: connect 27.6
Connection length [replies/conn]: 1.000
@mtkd
mtkd / string_test.rb
Created April 13, 2012 23:51
String Concatenation Benchmarks
require 'benchmark'
first_name = "first"
last_name = "last"
Benchmark.bm do |x|
x.report { 100000.times do; "#{first_name} #{last_name}" ; end; }
x.report { 100000.times do; [first_name, last_name].join(' ') ; end; }
x.report { 100000.times do; '%s %s' % [first_name, last_name] ; end; }
x.report { 100000.times do; first_name + ' ' + last_name ; end; }