overcome merge conflict

This commit is contained in:
Roger Pack 2008-09-05 20:16:08 -06:00
commit e79b6291a3
5 changed files with 178 additions and 64 deletions

View File

@ -753,13 +753,17 @@ static VALUE query(VALUE obj, VALUE sql)
static VALUE socket(VALUE obj) static VALUE socket(VALUE obj)
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
return INT2NUM(vio_fd(m->net.vio)); return INT2NUM(m->net.fd);
} }
/* send_query */ /* send_query(sql,timeout=nil) */
static VALUE send_query(VALUE obj, VALUE sql) static VALUE send_query(int argc, VALUE* argv, VALUE obj)
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout);
Check_Type(sql, T_STRING); Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse) { if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected"); rb_raise(eMysql, "query: not connected");
@ -1273,12 +1277,12 @@ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
if (with_table == Qnil) if (with_table == Qnil)
with_table = Qfalse; with_table = Qfalse;
while ((hash = fetch_hash2(obj, with_table)) != Qnil) while ((hash = fetch_hash2(obj, with_table)) != Qnil)
rb_yield(hash); rb_yield(hash);
return obj; return obj;
} }
/* all_hashes_experimental(with_table=false) -- returns an array of hashes, one hash per row */ /* all_hashes(with_table=false) -- returns an array of hashes, one hash per row */
static VALUE all_hashes_experimental(int argc, VALUE* argv, VALUE obj) static VALUE all_hashes(int argc, VALUE* argv, VALUE obj)
{ {
VALUE with_table; VALUE with_table;
VALUE field_names; VALUE field_names;
@ -2087,7 +2091,7 @@ void Init_mysql(void)
rb_define_method(cMysql, "query", query, 1); rb_define_method(cMysql, "query", query, 1);
rb_define_method(cMysql, "real_query", query, 1); rb_define_method(cMysql, "real_query", query, 1);
/*rb_define_method(cMysql, "async_query", async_query, 1);*/ /*rb_define_method(cMysql, "async_query", async_query, 1);*/
rb_define_method(cMysql, "send_query", send_query, 1); rb_define_method(cMysql, "send_query", send_query, -1);
rb_define_method(cMysql, "get_result", get_result, 0); rb_define_method(cMysql, "get_result", get_result, 0);
rb_define_method(cMysql, "socket", socket, 0); rb_define_method(cMysql, "socket", socket, 0);
rb_define_method(cMysql, "refresh", refresh, 1); rb_define_method(cMysql, "refresh", refresh, 1);
@ -2241,7 +2245,7 @@ void Init_mysql(void)
rb_define_method(cMysqlRes, "row_tell", row_tell, 0); rb_define_method(cMysqlRes, "row_tell", row_tell, 0);
rb_define_method(cMysqlRes, "each", each, 0); rb_define_method(cMysqlRes, "each", each, 0);
rb_define_method(cMysqlRes, "each_hash", each_hash, -1); rb_define_method(cMysqlRes, "each_hash", each_hash, -1);
rb_define_method(cMysqlRes, "all_hashes_experimental", all_hashes_experimental, -1); /*rb_define_method(cMysqlRes, "all_hashes", all_hashes, -1);*/
/* MysqlField object method */ /* MysqlField object method */
rb_define_method(cMysqlField, "name", field_name, 0); rb_define_method(cMysqlField, "name", field_name, 0);

View File

@ -7,3 +7,11 @@ class Mysql
get_result get_result
end end
end end
class Mysql::Result
def all_hashes
rows = []
each_hash { |row| rows << row }
rows
end
end

View File

@ -1,30 +1,6 @@
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'
@count = 10
@connections = {}
@count.times do EventedMysqlTest.new( 10 ) do |test|
c = Mysql.real_connect('localhost','root','3421260') test.setup{ Mysql.real_connect('localhost','root') }
@connections[IO.new(c.socket)] = c test.run!
end
@sockets = @connections.keys
@done = 0
@t = Time.now
@connections.each_value do |c|
c.send_query('select sleep(1)')
end
loop do
res = select(@sockets,nil,nil,nil)
if res
res.first.each do |c|
@connections[c].get_result.each{|r| p r}
@done = @done + 1
if @done == @count
puts Time.now - @t
exit
end
end
end
end end

View File

@ -1,2 +1,152 @@
require 'rubygems' require 'rubygems'
require 'mysqlplus' require 'mysqlplus'
class MysqlTest
class NotImplemented < StandardError
end
attr_accessor :queries,
:connections,
:connection_signature,
:start,
:done
def initialize( queries )
@queries = queries
@done = []
yield self if block_given?
end
def setup( &block )
@start = Time.now
@connection_signature = block
end
def run!
raise NotImplemented
end
def prepare
raise NotImplemented
end
def teardown
raise NotImplemented
end
def log( message, prefix = '' )
puts "[#{timestamp}] #{prefix} #{message}"
end
def with_logging( message )
log( message, 'Start' )
yield
log( message, 'End' )
end
def timestamp
Time.now - @start
end
end
class EventedMysqlTest < MysqlTest
attr_accessor :sockets
def initialize( queries )
@sockets = []
@connections = {}
super( queries )
end
def setup( &block )
super( &block )
with_logging 'Setup connection pool' do
@queries.times do
connection = @connection_signature.call
@connections[ IO.new(connection.socket) ] = connection
@sockets = @connections.keys
end
end
end
def run!
prepare
loop do
result = select( @sockets,nil,nil,nil )
if result
result.first.each do |conn|
@connections[conn].get_result.each{|res| log( "Result for socket #{conn.fileno} : #{res}" ) }
@done << nil
teardown if done?
end
end
end
end
def prepare
@connections.each_value do |conn|
conn.send_query( "select sleep(3)" )
end
end
def teardown
log "done"
exit
end
protected
def done?
@done.size == @queries
end
end
class ThreadedMysqlTest < MysqlTest
attr_accessor :threads
def initialize( queries )
@connections = []
@threads = []
super( queries )
end
def setup( &block )
super( &block )
with_logging "Setup connection pool" do
@queries.times do
@connections << @connection_signature.call
end
end
end
def run!
prepare
with_logging "waiting on threads" do
@threads.each{|t| t.join }
end
end
def prepare
with_logging "prepare" do
@queries.times do |conn|
@threads << Thread.new do
log "sending query on connection #{conn}"
@connections[conn].async_query( "select sleep(3)" ).each do |result|
log "connection #{conn} done"
end
end
end
end
end
end

View File

@ -1,30 +1,6 @@
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'
$count = 10 ThreadedMysqlTest.new( 10 ) do |test|
test.setup{ Mysql.real_connect('localhost','root') }
$start = Time.now test.run!
$connections = []
$count.times do
$connections << Mysql.real_connect('localhost','root','3421260')
end end
puts 'connection pool ready'
$threads = []
$count.times do |i|
$threads << Thread.new do
puts "sending query on connection #{i}"
$connections[i].async_query("select sleep(3)").each{ |r|
puts "connection #{i} done"
}
end
end
puts 'waiting on threads'
$threads.each{|t| t.join }
puts Time.now - $start