overcome merge conflict
This commit is contained in:
commit
e79b6291a3
20
ext/mysql.c
20
ext/mysql.c
@ -753,13 +753,17 @@ static VALUE query(VALUE obj, VALUE sql)
|
||||
static VALUE socket(VALUE obj)
|
||||
{
|
||||
MYSQL* m = GetHandler(obj);
|
||||
return INT2NUM(vio_fd(m->net.vio));
|
||||
return INT2NUM(m->net.fd);
|
||||
}
|
||||
|
||||
/* send_query */
|
||||
static VALUE send_query(VALUE obj, VALUE sql)
|
||||
/* send_query(sql,timeout=nil) */
|
||||
static VALUE send_query(int argc, VALUE* argv, VALUE obj)
|
||||
{
|
||||
MYSQL* m = GetHandler(obj);
|
||||
VALUE sql, timeout;
|
||||
|
||||
rb_scan_args(argc, argv, "11", &sql, &timeout);
|
||||
|
||||
Check_Type(sql, T_STRING);
|
||||
if (GetMysqlStruct(obj)->connection == Qfalse) {
|
||||
rb_raise(eMysql, "query: not connected");
|
||||
@ -1273,12 +1277,12 @@ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
|
||||
if (with_table == Qnil)
|
||||
with_table = Qfalse;
|
||||
while ((hash = fetch_hash2(obj, with_table)) != Qnil)
|
||||
rb_yield(hash);
|
||||
rb_yield(hash);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/* all_hashes_experimental(with_table=false) -- returns an array of hashes, one hash per row */
|
||||
static VALUE all_hashes_experimental(int argc, VALUE* argv, VALUE obj)
|
||||
/* all_hashes(with_table=false) -- returns an array of hashes, one hash per row */
|
||||
static VALUE all_hashes(int argc, VALUE* argv, VALUE obj)
|
||||
{
|
||||
VALUE with_table;
|
||||
VALUE field_names;
|
||||
@ -2087,7 +2091,7 @@ void Init_mysql(void)
|
||||
rb_define_method(cMysql, "query", query, 1);
|
||||
rb_define_method(cMysql, "real_query", 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, "socket", socket, 0);
|
||||
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, "each", each, 0);
|
||||
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 */
|
||||
rb_define_method(cMysqlField, "name", field_name, 0);
|
||||
|
@ -7,3 +7,11 @@ class Mysql
|
||||
get_result
|
||||
end
|
||||
end
|
||||
|
||||
class Mysql::Result
|
||||
def all_hashes
|
||||
rows = []
|
||||
each_hash { |row| rows << row }
|
||||
rows
|
||||
end
|
||||
end
|
||||
|
@ -1,30 +1,6 @@
|
||||
require File.dirname(__FILE__) + '/test_helper'
|
||||
@count = 10
|
||||
@connections = {}
|
||||
|
||||
@count.times do
|
||||
c = Mysql.real_connect('localhost','root','3421260')
|
||||
@connections[IO.new(c.socket)] = c
|
||||
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
|
||||
EventedMysqlTest.new( 10 ) do |test|
|
||||
test.setup{ Mysql.real_connect('localhost','root') }
|
||||
test.run!
|
||||
end
|
@ -1,2 +1,152 @@
|
||||
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
|
@ -1,30 +1,6 @@
|
||||
require File.dirname(__FILE__) + '/test_helper'
|
||||
|
||||
$count = 10
|
||||
|
||||
$start = Time.now
|
||||
|
||||
$connections = []
|
||||
$count.times do
|
||||
$connections << Mysql.real_connect('localhost','root','3421260')
|
||||
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
|
||||
ThreadedMysqlTest.new( 10 ) do |test|
|
||||
test.setup{ Mysql.real_connect('localhost','root') }
|
||||
test.run!
|
||||
end
|
Loading…
Reference in New Issue
Block a user