Skip to content

Instantly share code, notes, and snippets.

@copernicus
Forked from kazjote/populate.rb
Created January 8, 2012 18:44
Show Gist options
  • Save copernicus/1579279 to your computer and use it in GitHub Desktop.
Save copernicus/1579279 to your computer and use it in GitHub Desktop.
FnordMetric data generator
# Copy it to fnordmetric directory and run with
# bundle exec ruby -Ilib populate.rb live
require "securerandom"
require "socket"
require "json"
class User < Struct.new :username, :avatar
def session_id
"session_#{username}"
end
end
users = [
User.new("Alice", "http://www.avatarsdb.com/avatars/draco_malfoy.jpg"),
User.new("Jack", "http://www.avatarsdb.com/avatars/draco_malfoy_eyes.gif"),
User.new("Bob", "http://www.avatarsdb.com/avatars/german_shepherd.jpg"),
User.new("Tom", "http://www.avatarsdb.com/avatars/funny_elite_machine_gun.gif"),
User.new("James", "http://www.avatarsdb.com/avatars/ugly_face.gif"),
User.new("Kate", nil), User.new("Julia", nil) ]
urls = [[0.1, "/family"], [0.3, "/gadgets"], [0.6, "/cars"], [1.0, "/games"]]
@socket = TCPSocket.new 'localhost', 1337
trap "TERM", proc { @socket.close }
trap "INT", proc { @socket.close }
def send_event event
@socket.puts event.to_json
end
users.each do |user|
send_event(_type: :_set_name, name: user.username, _session: user.session_id)
if user.avatar
send_event(_type: :_set_picture, url: user.avatar, _session: user.session_id)
end
end
while true
user = users.sample
random_number = rand
url = "#{urls.detect {|(n, _)| rand <= n }[1]}/#{rand(10)}"
send_event(_type: :_pageview, url: url, _session: user.session_id)
sleep rand
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment