Sha256: f833fda7874f5b2f3b4e2389147bc2066953e1f3c91f907dc3bb52c5e7fb0c94

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

#!/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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
remcached-0.4.1 examples/fill.rb
remcached-0.4.0 examples/fill.rb