Created
September 14, 2010 03:14
-
-
Save BinaryMuse/578470 to your computer and use it in GitHub Desktop.
Quick and ugly script to generate 1,000,000 fake accounts in MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'mongo' | |
require 'faker' | |
require 'pp' | |
require 'benchmark' | |
include Mongo | |
class Exception | |
def errmsg | |
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"] | |
end | |
end | |
host = 'localhost' | |
port = Connection::DEFAULT_PORT | |
db = Connection.new(host, port).db('test') | |
puts ">> Getting collection: mongo_test_index" | |
begin | |
coll = db.collection('mongo_test_index') | |
puts "created: #{coll.inspect}" | |
rescue => e | |
puts "Error: #{e.errmsg}" | |
end | |
OBJS_EACH = 500 | |
OBJS_TIMES = 2000 | |
TOTAL = OBJS_EACH * OBJS_TIMES | |
$TOTAL_TIME = 0 | |
$AVERAGE_TIME = 0 | |
arr = nil | |
rndm = ("a".."f").to_a | |
rndm << ("0".."9").to_a | |
rndm.flatten! | |
puts ">> Generating test data (#{OBJS_EACH*OBJS_TIMES} records, #{OBJS_EACH} per)..." | |
OBJS_TIMES.times do |x| | |
individual_time = Benchmark.realtime do | |
percent = ((x/OBJS_TIMES.to_f)*100).to_i | |
time_left = ($AVERAGE_TIME)*(OBJS_TIMES - (x+1)) | |
mins_left = (time_left / 60).to_i | |
secs_left = (time_left % 60).to_i | |
puts "#{x}/#{OBJS_TIMES} (#{percent}%) #{mins_left} m #{secs_left} s left" | |
arr = (0...OBJS_EACH).collect do | |
first = Faker::Name.first_name | |
last = Faker::Name.last_name | |
user1 = Faker::Internet.user_name | |
user2 = Faker::Internet.user_name | |
key1 = "" | |
key2 = "" | |
16.times do | |
key1 += rndm[rand(16)] | |
key2 += rndm[rand(16)] | |
end | |
{ | |
:first => first, | |
:last => last, | |
:email => Faker::Internet.email, | |
:accounts => [ | |
{ | |
:type => "facebook", | |
:username => user1, | |
:key => key1 | |
}, | |
{ | |
:type => "twitter", | |
:username => user2, | |
:key => key2 | |
} | |
] | |
} | |
end | |
coll.insert(arr) | |
end | |
$TOTAL_TIME += individual_time | |
$AVERAGE_TIME = $TOTAL_TIME / (x+1) | |
iters_left = OBJS_TIMES - (x+1) | |
end | |
last_record = arr.last | |
pp last_record | |
puts " Done in #{$TOTAL_TIME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not pretty at all (and even a couple errors now that I look) but it got the job done. :)