Skip to content

Instantly share code, notes, and snippets.

@flazz
Created July 7, 2011 15:38
Show Gist options
  • Save flazz/1069784 to your computer and use it in GitHub Desktop.
Save flazz/1069784 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'riak'
require 'json'
require 'pp'
class Time
def ms_from_epoc
k = 1000
(to_i * k) + (usec / k).to_i
end
end
class Event < Hash
def initialize time, record_type, record_id, record_attrs={}
self['created_at'] = time.ms_from_epoc
self['type'] = self.class.name
self['nonce'] = rand(1024**3).to_s(16)
self['record'] = {
'type' => record_type,
'id' => record_id,
'attributes' => record_attrs
}
end
def key
vals = values_at('nonce', 'created_at', 'type')
record_vals = self['record'].values_at('type', 'id')
(vals + record_vals).join('-').downcase
end
def to_s
e.key + "\n" + e.to_json
end
end
class Imported < Event; end
class Created < Event; end
class Updated < Event; end
class Deleted < Event; end
DAY = 1440
MONTH = DAY * 30
YEAR = MONTH * 12
DEVICES = %w(Ups Pdu Switch Firewall)
unless DEVICES.respond_to? :sample
def DEVICES.sample
self[rand size]
end
end
def random_device_events
type = DEVICES.sample
id = rand 10_000
time = Time.now - (YEAR * rand)
[Created, Updated, Deleted].map do |k|
time += MONTH * rand
k.new time, type, id, :volt_rating => 400.0, :amp_rating => 630.0
end
end
client = Riak::Client.new
events = client.bucket 'events'
n = ARGV.shift.to_i
puts Benchmark.measure {
n.times do
random_device_events.each do |e|
ro = Riak::RObject.new events, e.key
ro.data = e
ro.content_type = 'application/json'
ro.store
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment