Skip to content

Instantly share code, notes, and snippets.

@pacojp
Created January 24, 2013 08:29
Show Gist options
  • Save pacojp/4618708 to your computer and use it in GitHub Desktop.
Save pacojp/4618708 to your computer and use it in GitHub Desktop.
require 'mysql2'
def r_signal(sig)
@kill = true
end
SIGNALS = [ :QUIT, :INT, :TERM, :USR1, :USR2, :HUP ]
SIGNALS.each { |sig| trap(sig){r_signal(sig)} }
def access_mysql
begin
client = Mysql2::Client.new(
:host => "localhost",
:username => "root",
:database => "test",
)
results = client.query("SELECT * FROM test")
results.to_a.inspect
@total_ok += 1
rescue
@total_ng += 1
ensure
begin
client.close
rescue
end
end
end
@total_count = 0
@total_ok = 0
@total_ng = 0
loop do
break if @kill
threads = []
(1..30).each do
t = Thread.new do
access_mysql
Thread.pass
end
threads << t
@total_count += 1
end
threads.each do |th|
th.join
#break if @kill
end
end
puts "total_count:#{@total_count}"
puts "total_ok:#{@total_ok}"
puts "total_ng:#{@total_ng}"
__END__
Mysql2::Client.new(
:host,
:username,
:password,
:port,
:database,
:socket = '/path/to/mysql.sock',
:flags = REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | MULTI_STATEMENTS,
:encoding = 'utf8',
:read_timeout = seconds,
:write_timeout = seconds,
:connect_timeout = seconds,
:reconnect = true/false,
:local_infile = true/false,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment