Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 150 lines (118 sloc) 4.949 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

require 'rest-core'
RC.eagerload

def def_use_case name, &block
  singleton_class.send(:define_method, "#{name}_", &block)
  singleton_class.send(:define_method, name) do
    @count ||= 0
    printf "Use case #%02d: %s\n", @count+=1, name
    puts '-' * 70
    start = Time.now
    send("#{name}_")
    puts "Spent #{Time.now - start} seconds for this use case."
    puts
  end
end

def q str, m=nil
  p = lambda{ puts "\e[33m=> #{str.inspect}\e[0m" }
  if m
    m.synchronize(&p)
  else
    p.call
  end
end

# ----------------------------------------------------------------------

def_use_case 'pure_ruby_single_request' do
  q RC::Universal.new(:json_response => true).
    get('https://api.github.com/users/godfat')['name']
end

def_use_case 'pure_ruby_concurrent_requests' do
  client = RC::Universal.new(:json_response => true,
                             :site => 'https://api.github.com/users/')
  q [client.get('godfat'), client.get('cardinalblue')].map{ |u| u['name'] }
end

def_use_case 'pure_ruby_cache_requests' do
  client = RC::Universal.new(:json_response => true, :cache => {})
  3.times{ q client.get('https://api.github.com/users/godfat')['name'] }
end

def_use_case 'pure_ruby_callback_requests' do
  m = Mutex.new
  RC::Universal.new(:json_response => true ,
                    :site => 'https://api.github.com/users/' ,
                    :log_method => lambda{|str| m.synchronize{puts(str)}}).
    get('godfat'){ |res|
      q res['name'], m
    }.
    get('cardinalblue'){ |res|
      q res['name'], m
    }.wait
end

def_use_case 'pure_ruby_nested_concurrent_requests' do
  m = Mutex.new
  c = RC::Universal.new(:json_response => true ,
                        :site => 'https://api.github.com' ,
                        :log_method => lambda{|str| m.synchronize{puts(str)}})

  %w[rubytaiwan godfat].each{ |user|
    c.get("/users/#{user}/repos", :per_page => 100){ |repos|
      rs = repos.reject{ |r| r['fork'] }
      most_watched = rs.max_by{ |r| r['watchers'] }['name']
      most_size = rs.max_by{ |r| r['size'] }['name']

      watch_contri = c.get("/repos/#{user}/#{most_watched}/contributors")
       size_contri = c.get("/repos/#{user}/#{most_size}/contributors")

      most_watched_most_contri = watch_contri.max_by{ |c| c['contributions'] }
      most_size_most_contri = size_contri.max_by{ |c| c['contributions'] }

      q "Most contributed user for most watched: #{user}/#{most_watched}:", m
      q most_watched_most_contri['login'], m

      q "Most contributed user for most size : #{user}/#{most_size}:", m
      q most_size_most_contri['login'], m
    }
  }

  c.wait
end

# ----------------------------------------------------------------------

def_use_case 'eventmachine_fiber_single_request' do
  EM.run{ Fiber.new{ pure_ruby_single_request_ ; EM.stop }.resume }
end

def_use_case 'eventmachine_fiber_concurrent_requests' do
  EM.run{ Fiber.new{ pure_ruby_concurrent_requests_ ; EM.stop }.resume }
end

def_use_case 'eventmachine_fiber_cache_requests' do
  EM.run{ Fiber.new{ pure_ruby_cache_requests_ ; EM.stop }.resume }
end

def_use_case 'eventmachine_fiber_callback_requests' do
  EM.run{ Fiber.new{ pure_ruby_callback_requests_ ; EM.stop }.resume }
end

def_use_case 'eventmachine_fiber_nested_concurrent_requests' do
  EM.run{ Fiber.new{ pure_ruby_nested_concurrent_requests_; EM.stop }.resume }
end

# ----------------------------------------------------------------------

def_use_case 'eventmachine_thread_single_request' do
  EM.run{ Thread.new{ pure_ruby_single_request_ ; EM.stop } }
end

def_use_case 'eventmachine_thread_concurrent_requests' do
  EM.run{ Thread.new{ pure_ruby_concurrent_requests_ ; EM.stop } }
end

def_use_case 'eventmachine_thread_cache_requests' do
  EM.run{ Thread.new{ pure_ruby_cache_requests_ ; EM.stop } }
end

def_use_case 'eventmachine_thread_callback_requests' do
  EM.run{ Thread.new{ pure_ruby_callback_requests_ ; EM.stop } }
end

def_use_case 'eventmachine_thread_nested_concurrent_requests' do
  EM.run{ Thread.new{ pure_ruby_nested_concurrent_requests_; EM.stop } }
end

# ----------------------------------------------------------------------

pure_ruby_single_request
pure_ruby_concurrent_requests
pure_ruby_cache_requests
pure_ruby_callback_requests
pure_ruby_nested_concurrent_requests

eventmachine_fiber_single_request
eventmachine_fiber_concurrent_requests
eventmachine_fiber_cache_requests
eventmachine_fiber_callback_requests
# eventmachine_fiber_nested_concurrent_requests

eventmachine_thread_single_request
eventmachine_thread_concurrent_requests
eventmachine_thread_cache_requests
eventmachine_thread_callback_requests
# eventmachine_thread_nested_concurrent_requests
Something went wrong with that request. Please try again.