Created
December 27, 2011 02:08
-
-
Save weirdbricks/1522544 to your computer and use it in GitHub Desktop.
Thread Beast
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 "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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First steps into Ruby....