Picky Case Study: Running it in a DRb Server

ruby / picky / case study

This is a post in the Picky series on its workings.

Intro

The picky generators, for example picky generate server <dirname> only generate web server examples, like the Sinatra server.

However, who tells you to always sing in the rain? Sometimes it is much more prudent to just use a DRb (Distributed Ruby) Server.

How can we have one run our searches? Not much different than in the Sinatra server. Or the classic server. (With the exception on how the access is defined. In the classic server, it’s route, in Sinatra it’s probably get, and here it’s starting the service)

Server

So, copy-and-paste away, into a file called app.rb:

require 'activesupport'
require 'yajl'
require 'picky'
require 'drb/drb'

# "Model".
#
class Item
  attr_reader :id, :name
  def initialize id, name
    @id, @name = id, name
  end
end

# Server.
#
class Server

  items = [
    Item.new(1, 'picky'),
    Item.new(2, 'drb'),
    Item.new(3, 'test'),
  ]

  drb_index = Picky::Index.new(:drb) do
    source   items
    category :name
  end
  drb_index.reindex

  drb_search = Picky::Search.new drb_index

  define_method :search do |*args|
    drb_search.search(*args).to_json
  end

end

DRb.start_service 'druby://localhost:8787', Server.new
DRb.thread.join

And that’s it for the server. Note that you don’t need to index right in the server. I only do that for your copy-paste convenience.

You could, for example, add a

Signal.trap('USR1') do
  drb_index.reindex
end

to have the server index on receiving the USR1 signal (kill -USR1 <pid>).

Client

The client.rb is much easier:

require 'drb/drb'

search_server = DRbObject.new_with_uri 'druby://localhost:8787'
1_000.times do
  puts search_server.search 'test'
end

And that’s it.

Running it

Start the server

$ ruby app.rb

and in another Terminal window you enter

$ ruby client.rb

to see the queries fly.

On my MacBook Pro I get 1600 “requests” per second. An that is on a single core!

… perhaps it could even be faster using http://msgpack.org/?

Next A quick note on APIs

Share


Previous

Comments?