Skip to content

Instantly share code, notes, and snippets.

View pauldix's full-sized avatar

Paul Dix pauldix

View GitHub Profile
# little test script to verify the speed of msgpack vs. json
require 'rubygems'
require 'yajl'
require 'msgpack'
require 'benchmark'
array = (1..20).map {|i| rand(i)}
map = (1..20).inject({}) {|r, o| r[rand(o).to_s] = rand(o); r}
string = (1..512).map {|i| ((rand(i) % 94) + 32).chr.to_s}.join('')
# Feedzirra example
# adding geo extensions to feed entries
Feedzirra::Feed.add_common_feed_entry_element("geo:lat", :as => :lat)
Feedzirra::Feed.add_common_feed_entry_element("geo:long", :as => :long)
feed = Feedzirra::Feed.fetch_and_parse("url-to-some-geo-rss-feed ...")
feed.entries.first.lat # yay, the parsed out lat
feed.entries.first.long # yay, the parsed out long
/usr/local/lib/ruby/gems/1.9.1/gems/typhoeus-0.1.6/lib/typhoeus/multi.rb:20: [BUG] Segmentation fault
ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-linux]
-- control frame ----------
c:0007 p:---- s:0019 b:0019 l:000018 d:000018 CFUNC :multi_perform
c:0006 p:0019 s:0016 b:0016 l:000015 d:000015 METHOD /usr/local/lib/ruby/gems/1.9.1/gems/typhoeus-0.1.6/lib/typhoeus/multi.rb:20
c:0005 p:0023 s:0013 b:0013 l:000012 d:000012 METHOD /usr/local/lib/ruby/gems/1.9.1/gems/typhoeus-0.1.6/lib/typhoeus/hydra.rb:56
c:0004 p:0023 s:0010 b:0010 l:000009 d:000009 METHOD source_worker.rb:39
c:0003 p:0143 s:0007 b:0007 l:0020bc d:0018d8 EVAL source_worker.rb:152
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
require 'rubygems'
require 'typhoeus'
require 'yajl/json_gem'
hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new('http://localhost:8089/?id=timeline', :method => :post, :body => stuff.to_json)
request.on_complete do |response|
response.code # http status
response.body
response.time # time taken in seconds
# single json object cache miss (passenger)
ab -n 10000 -c 50 http://ec2-some-ip.compute-1.amazonaws.com/api/v1/comments/1/no_cache
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking ec2-some-ip.compute-1.amazonaws.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
class User
def initialize(attrs = {})
# do stuff here
end
def self.find_request(id)
request = Typhoeus::Request.new(:method => :get,
:host => "localhost:3000",
:path => "/users/#{id}")
request.on_complete do |response|
hydra = Typhoeus::Hydra.new(20) # number is the size of the easy object pool
user_request = User.find_request(1) # where 1 is the id of the user
user_request.when_complete do
@user = User.parse(user_request.response.body)
comment_requests = @user.comment_ids.map {|id| Comment.find_request(id)}
comment_requests.each do |comment_request|
comment_request.when_complete do
@user.comments << Comment.parse(comment_request.response.body)
end
hydra = Typhoeus::Hydra.new(3)
@responses = []
start_time = Time.now
request = Typhoeus::Request.new(:method => :get, :host => "localhost:3000", :path => "/first", :query_string => "delay=1")
request.when_complete do
@responses << request.response
YAML.load(request.response.body)[:path_info].should == "/first"
# this shows how to use the update functionality for a single feed with feedzirra
require 'rubygems'
require 'feedzirra'
# I'm using Atom here, but it could be anything. You don't need to know ahead of time.
# It will parse out to the correct format when it updates.
feed_to_update = Feedzirra::Parser::Atom.new
feed_to_udpate.feed_url = some_stored_feed_url
feed_to_update.etag = some_stored_feed_etag
feed_to_update.last_modified = some_stored_feed_last_modified
require 'rubygems'
require 'json'
require 'typhoeus'
require 'net/http'
class CouchDB
include Typhoeus
remote_defaults :on_success => lambda {|response| JSON.parse(response.body)},
:on_failure => lambda {|response| puts "error code: #{response.code}"},
:base_uri => "http://127.0.0.1:5984/couch-test-db"