Skip to content

Instantly share code, notes, and snippets.

@mwlang
Created April 25, 2010 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwlang/378689 to your computer and use it in GitHub Desktop.
Save mwlang/378689 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sequel'
require 'faker'
require 'sequel_plus'
require 'progressbar'
require 'benchmark'
begin
DB = Sequel.connect("jdbc:mysql://localhost/jdbc_test?user=root")
rescue
DB = Sequel.mysql("jdbc_test", :host => "localhost", :user => 'root')
end
def recreate_table(table)
puts "creating the #{table} table..."
DB.create_table!(table, :engine => "innodb") do
primary_key :billing
String :first_name
String :last_name
String :address
String :city
String :state
String :zip_code, :index => true
String :phone
String :email, :index => true
BigDecimal :charges, :size => [10, 2]
BigDecimal :payments, :size => [10, 2]
BigDecimal :balance, :size => [10, 2]
Date :date_created, :index => true
Date :date_of_invoice
end
end
def fake_date
year = rand(5) + 2005
month = rand(12) + 1
day = rand(30) + 1
begin
dt = Date.civil(year, month, day)
rescue
fake_date
end
end
def populate_sample_data(rows)
puts "populating sample_data with #{rows} rows"
values = {}
pbar = ProgressBar.new('sample data', rows)
rows.times do
values[:first_name] = Faker::Name.first_name
values[:last_name] = Faker::Name.last_name
values[:address] = Faker::Address.street_address
values[:city] = Faker::Address.city
values[:state] = Faker::Address.us_state
values[:zip_code] = Faker::Address.zip_code
values[:phone] = Faker::PhoneNumber.phone_number
values[:email] = Faker::Internet.email
values[:charges] = rand(25) + 5 + (rand(100) / 100.0)
values[:payments] = rand(100) / 100.0 * values[:charges]
values[:balance] = values[:charges] = values[:payments]
values[:date_created] = fake_date
values[:date_of_invoice] = fake_date
DB[:sample_data].insert(values)
pbar.inc
end
pbar.finish
end
def clone_data(src_table, dest_table)
src_ds = DB[src_table]
dest_ds = DB[dest_table]
# truncate the destination table
dest_ds.delete
# Set up progress bar
records = src_ds.count
puts "cloning #{records} records for #{src_table}"
pbar = ProgressBar.new(src_table.to_s, records)
# Copy the data
src_ds.each do |values|
dest_ds.insert(values)
pbar.inc
end
pbar.finish
puts "Destination now has #{dest_ds.count} records in #{dest_table}"
end
def threaded_clone_data(src_table, dest_table)
src_ds = DB[src_table]
# truncate the destination table
DB[dest_table].delete
# Set up progress bar
records = src_ds.count
puts "cloning #{records} records for #{src_table}"
pbar = ProgressBar.new(src_table.to_s, records)
# Copy the data
threads = []
src_ds.each_slice(records / 4) do |rows|
threads << Thread.new(dest_table, rows, threads.size) do |dest_table, rows, i|
dest_ds = begin
Sequel.connect("jdbc:mysql://localhost/jdbc_test?user=root")[dest_table]
rescue
Sequel.mysql("jdbc_test", :host => "localhost", :user => 'root')[dest_table]
end
rows.each do |values|
dest_ds.insert(values)
pbar.inc
end
end
end
threads.each{|t| t.join}
pbar.finish
puts "Destination now has #{DB[dest_table].count} records in #{dest_table}"
end
Benchmark.bm do |b|
b.report("populate sample_data table: ") do
recreate_table :sample_data
populate_sample_data 20_000
end
b.report("clone sample_data to cloned_data: ") do
recreate_table :cloned_data
clone_data :sample_data, :cloned_data
end
b.report("threaded clone: ") do
threaded_clone_data :sample_data, :cloned_data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment