File: fill.rb

package info (click to toggle)
ruby-remcached 0.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 200 kB
  • sloc: ruby: 1,000; sh: 9; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env ruby

# Experimentally determine how many items fit in your memcached
# instance. Adjust parameters below for your scenario.

BATCH_SIZE = 10000
KEY_SIZE = 26
VALUE_SIZE = 0


$: << File.dirname(__FILE__) + "/../lib"
require 'remcached'

EM.run do
  @total = 0

  # Action
  def fill
    old_total = @total

    reqs = (1..BATCH_SIZE).map {
      @total += 1
      { :key => sprintf("%0#{KEY_SIZE}X", @total),
        :value => sprintf("%0#{VALUE_SIZE}X", @total)
      }
    }
    Memcached.multi_add(reqs) do |resps|
      resps.each do |key,resp|
        case resp[:status]
        when Memcached::Errors::NO_ERROR
          :ok
        when Memcached::Errors::KEY_EXISTS
          @total -= 1
        else
          puts "Cannot set #{key}: status=#{resp[:status].inspect}"
          @total -= 1
        end
      end

      puts "Added #{@total - old_total}, now: #{@total}"
      if Memcached.usable?
        stats = {}
        Memcached.usable_clients[0].stats do |resp|
          if resp[:key] != ''
            stats[resp[:key]] = resp[:value]
          else
            puts "Stats: #{stats['bytes']} bytes in #{stats['curr_items']} of #{stats['total_items']} items"
          end
        end

        # Next round:
        fill
      else
        EM.stop
      end
    end
  end

  # Initialization & start
  Memcached.servers = %w(localhost)
  @t = EM::PeriodicTimer.new(0.01) do
    if Memcached.usable?
      puts "Connected to server"
      @t.cancel
      fill
    else
      puts "Waiting for server connection..."
    end
  end
end