Skip to content

Instantly share code, notes, and snippets.

@weirdbricks
Created December 27, 2011 02:08
Show Gist options
  • Save weirdbricks/1522544 to your computer and use it in GitHub Desktop.
Save weirdbricks/1522544 to your computer and use it in GitHub Desktop.
Thread Beast
#!/usr/bin/env ruby
require "rubygems"
require "sequel"
entries=0;entries=ARGV[0].to_i
threads=0;threads=ARGV[1].to_i
if ( entries.zero? || threads.zero? )
puts "Usage: ./tester number_of_entries number of threads\nValues cannot be 0"
exit
end
perthread=entries/threads
#puts "You requested #{entries} entries"
#puts "You requested #{threads} threads"
#puts "Did #{perthread} per thread"
puts "connecting to mysql..."
DB = Sequel.connect(:adapter=>'mysql2', :host=>'localhost', :database=>'sheep', :user=>'root')
#! drop table if it exists
#? creates unless table exists
DB.create_table! :sheep do
primary_key :id
String :name
String :last_name
end
sheep = DB[:sheep]
START_TIME=Time.now
def insert(entries,title)
sheep = DB[:sheep]
for i in 1..entries
name=(0...10).map{65.+(rand(25)).chr}.join
last_name=(0...10).map{65.+(rand(25)).chr}.join
sheep.insert(:name => name, :last_name => last_name)
print "Thread:#{title} Entry:#{i} Name:#{name} Last Name:#{last_name}\n"
end
end
Thread.abort_on_exception = true
#next line will create an array of threads
threadarray=Array.new(threads) { |e| e=Thread.new{insert(perthread,e)} }
threadarray.each{ |a| a.join }
END_TIME=Time.now
puts "You requested #{entries} entries"
puts "You requested #{threads} threads"
puts "Did #{perthread} per thread"
puts "Backup started at #{START_TIME}"
puts "Backup ended at #{END_TIME}"
puts "process lasted #{END_TIME-START_TIME} seconds"
@weirdbricks
Copy link
Author

First steps into Ruby....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment