Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
@raggi
raggi / inject_concat_vs_map.rb
Created December 3, 2009 12:17
closures aren't cheap, srsly
require "benchmark"
arr = [1,2,3,4,5,6,7,8,9,0] * 100
GC.start
TESTS = 10_000
Benchmark.bmbm do |results|
results.report("map:") { TESTS.times { arr.map { |i| i } } }
results.report("inj:") { TESTS.times { a = []; arr.inject(a, :<<) } }
end
class Binding
def set(hash)
backup = Thread.current[:binding_set]
hash.each do |var, val|
Thread.current[:binding_set] = val
eval(%(#{var} = Thread.current[:binding_set]))
end
ensure
Thread.current[:binding_set] = backup
end
@raggi
raggi / async_sequel.rb
Created January 13, 2010 00:40
Pooled async sequel stab
require 'eventmachine'
require 'sequel'
# I stole this code from em-mysql (tmm1 <3)
module Sequel
class Database
attr_accessor :_async
end
class Dataset
@raggi
raggi / config_general_parser.rb
Created January 13, 2010 11:27
filthy short + simple parser for Config::General.pm style configs
#!/usr/bin/env ruby
file = ARGV.first
abort "Usage: #{File.basename($0)} file.conf" unless file
cfg = []
current_subtree = cfg
parent = cfg
@raggi
raggi / README
Created February 26, 2010 01:43
This is example code for an ML response.
class String
def hash18
# cheeky integer truncate TODO move to bitmask, and method
trunc = lambda { |v| [v].pack('i').unpack('i').first }
key = 0
each_byte do |b|
key = trunc.call(trunc.call(key*65599) + b)
end
trunc.call(key + trunc.call(key >> 5))
end
require 'eventmachine'
class HTTP < EM::Connection
include EM::P::LineText2
RESPONSE = <<-PLAIN
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 12
Connection: keep-alive
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44000 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44088 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44088 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
require 'thin'
request = "GET / HTTP/1.0\r\nHost: 127.0.0.1:3001\r\nUser-Agent: ApacheBench/2.3\r\nAccept: */*\r\n\r\n"
def rss
system "ps -orss= #{$$}"
end
rss
while true
class A
def example; end
def leak
method(:example)
end
end
a = A.new
a.leak while true