Extract thread scheduler

This commit is contained in:
Lourens Naude 2008-09-10 01:26:01 +01:00
parent 6884d454bc
commit 3c5dd38037
4 changed files with 40 additions and 32 deletions

View File

@ -764,7 +764,6 @@ static VALUE socket(VALUE obj)
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
return INT2NUM(m->net.fd); return INT2NUM(m->net.fd);
} }
/* socket_type */ /* socket_type */
static VALUE socket_type(VALUE obj) static VALUE socket_type(VALUE obj)
{ {
@ -824,23 +823,14 @@ static VALUE get_result(VALUE obj)
return store_result(obj); return store_result(obj);
} }
/* async_query(sql,timeout=nil) */ static VALUE schedule(VALUE obj, VALUE timeout)
static VALUE async_query(int argc, VALUE* argv, VALUE obj)
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
VALUE sql, timeout;
fd_set read; fd_set read;
int ret; int ret;
rb_scan_args(argc, argv, "11", &sql, &timeout);
send_query(obj,sql);
timeout = ( NIL_P(timeout) ? m->net.read_timeout : INT2NUM(timeout) ); timeout = ( NIL_P(timeout) ? m->net.read_timeout : INT2NUM(timeout) );
VALUE args[1];
args[0] = timeout;
struct timeval tv = { tv_sec: timeout, tv_usec: 0 }; struct timeval tv = { tv_sec: timeout, tv_usec: 0 };
FD_ZERO(&read); FD_ZERO(&read);
@ -861,6 +851,20 @@ static VALUE async_query(int argc, VALUE* argv, VALUE obj)
} }
} }
}
/* async_query(sql,timeout=nil) */
static VALUE async_query(int argc, VALUE* argv, VALUE obj)
{
MYSQL* m = GetHandler(obj);
VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout);
send_query(obj,sql);
schedule(obj, timeout);
return get_result(obj); return get_result(obj);
} }

View File

@ -3,34 +3,34 @@ require File.dirname(__FILE__) + '/test_helper'
ThreadedMysqlTest.new( 10, "Threaded, C, very small overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, C, very small overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 0.005 test.per_query_overhead = 0.005
test.c_async_query = true test.query_with = :c_async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, C, small overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, C, small overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 0.1 test.per_query_overhead = 0.1
test.c_async_query = true test.query_with = :c_async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, C, medium overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, C, medium overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 1 test.per_query_overhead = 1
test.c_async_query = true test.query_with = :c_async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, C, large overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, C, large overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 3 test.per_query_overhead = 3
test.c_async_query = true test.query_with = :c_async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, C, random overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, C, random overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = :random test.per_query_overhead = :random
test.c_async_query = true test.query_with = :c_async_query
test.run! test.run!
end end

View File

@ -3,29 +3,34 @@ require File.dirname(__FILE__) + '/test_helper'
ThreadedMysqlTest.new( 10, "Threaded, native Ruby, very small overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, native Ruby, very small overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 0.005 test.per_query_overhead = 0.005
test.query_with = :async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, native Ruby, small overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, native Ruby, small overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 0.1 test.per_query_overhead = 0.1
test.query_with = :async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, native Ruby, medium overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, native Ruby, medium overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 1 test.per_query_overhead = 1
test.query_with = :async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, native Ruby, large overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, native Ruby, large overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = 3 test.per_query_overhead = 3
test.query_with = :async_query
test.run! test.run!
end end
ThreadedMysqlTest.new( 10, "Threaded, native Ruby, random overhead" ) do |test| ThreadedMysqlTest.new( 10, "Threaded, native Ruby, random overhead" ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root') }
test.per_query_overhead = :random test.per_query_overhead = :random
test.query_with = :async_query
test.run! test.run!
end end

View File

@ -12,7 +12,7 @@ class MysqlTest
:connection_signature, :connection_signature,
:start, :start,
:done, :done,
:c_async_query, :query_with,
:per_query_overhead, :per_query_overhead,
:timeout :timeout
@ -20,7 +20,7 @@ class MysqlTest
@queries = queries @queries = queries
@context = context @context = context
@done = [] @done = []
@c_async_query = false @query_with = :async_query
@per_query_overhead = 3 @per_query_overhead = 3
@timeout = 20 @timeout = 20
yield self if block_given? yield self if block_given?
@ -78,7 +78,7 @@ class MysqlTest
end end
def c_or_native_ruby_async_query def c_or_native_ruby_async_query
if @c_async_query if @query_with == :c_async_query
log "** using C based async_query" log "** using C based async_query"
else else
log "** using native Ruby async_query" log "** using native Ruby async_query"
@ -86,9 +86,8 @@ class MysqlTest
yield yield
end end
def c_or_native_async_query( connection, sql, timeout = nil ) def dispatch_query( connection, sql, timeout = nil )
method = @c_async_query ? :c_async_query : :async_query connection.send( @query_with, sql, timeout )
connection.send( method, sql, timeout )
end end
end end
@ -188,7 +187,7 @@ class ThreadedMysqlTest < MysqlTest
log "sending query on connection #{conn}" log "sending query on connection #{conn}"
c_or_native_async_query( @connections[conn], "select sleep(#{@per_query_overhead})", @timeout ).each do |result| dispatch_query( @connections[conn], "select sleep(#{@per_query_overhead})", @timeout ).each do |result|
log "connection #{conn} done" log "connection #{conn} done"
end end