Created
July 8, 2011 12:59
-
-
Save tomafro/1071764 to your computer and use it in GitHub Desktop.
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
require 'faker' | |
require 'factory_girl' | |
require 'test/factories' | |
require 'message' | |
Message.collection.drop | |
Message.create_indexes | |
Contact.collection.drop | |
Contact.create_indexes | |
def show_time_taken(message = "", &block) | |
start_time = Time.zone.now | |
yield | |
duration_in_ms = 1000.0 * (Time.zone.now - start_time) | |
time = Time.zone.now.strftime('%H:%M:%S %Y-%m-%d') | |
puts ("%.1fms #{message}" % duration_in_ms) | |
end | |
class Array | |
def rand_power_law(n = 1) | |
min = 0 | |
max = size | |
pl = ((max**(n+1) - min**(n+1))*Kernel.rand() + min**(n+1))**(1.0/(n+1)) | |
self[(max-1-pl.to_i)+min] | |
end | |
end | |
batch_size = 1000 | |
batches = 250 | |
ten_thousands = (batch_size * batches) / 10_000 | |
number_of_accounts = 50 | |
contacts_per_account = 10 | |
accounts = number_of_accounts.times.collect {Factory(:activated_account)} | |
account_msisdns = accounts.inject({}) do |memo, account| | |
memo[account] = 100.times.collect { Faker::O2.msisdn } | |
memo | |
end | |
puts "Starting benchmark to import #{batch_size * batches} messages" | |
show_time_taken "to process #{batches * batch_size}" do | |
ten_thousands.times do |tens| | |
puts "----------------------------------" | |
show_time_taken "last 10k messages" do | |
(batches / ten_thousands).times do |n| | |
total = (batch_size * (n + 1)) + (tens * 10_000) | |
show_time_taken "to process #{batch_size} messages (#{total} in total)" do | |
batch_size.times do | |
Rails.logger.info("--------") | |
account = accounts.rand_power_law(2) | |
contact_msisdn = account_msisdns[account].rand_power_law(2) | |
account.import_message!(Factory.attributes_for(:message).merge(:contact_msisdn => contact_msisdn)) | |
end | |
end | |
end | |
end | |
end | |
end |
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
task :benchmark => 'db:reset' do | |
raise "This task can only run in development" unless Rails.env.development? | |
benchmark_file = File.join(Rails.root, 'db', 'benchmark.rb') | |
load(benchmark_file) if File.exist?(benchmark_file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment