Skip to content
This repository has been archived by the owner on Jun 25, 2018. It is now read-only.

Commit

Permalink
adding the nb pg,mysql tests
Browse files Browse the repository at this point in the history
  • Loading branch information
humanzz committed Sep 2, 2008
1 parent c35613d commit 6ca677d
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/test_mysql.rb
@@ -0,0 +1,48 @@
require 'neverblock-mysql'

class Mysql
attr_accessor :fiber
alias :old_query :query
def query(sql)
if Fiber.current[:neverblock]
send_query(sql)
@fiber = Fiber.current
Fiber.yield
else
old_query(sql)
end
end

def process_command
@fiber.resume get_result
end
end

@count = 10
@connections = {}
@fpool = NB::Pool::FiberPool.new(@count)
@cpool = NB::Pool::FiberedConnectionPool.new(size:@count, eager:true) do
c = Mysql.real_connect('localhost','root',nil)
@connections[IO.new(c.socket)] = c
c
end

@break = false
@done = 0
@t = Time.now
@count.times do
@fpool.spawn(false) do
@cpool.hold do |conn|
conn.query('select sleep(1)').each{|r| r}
@done = @done + 1
puts "done in #{Time.now - @t}" if @done == @count
end
end
end
@sockets = @connections.keys
loop do
res = select(@sockets,nil,nil,nil)
if res
res.first.each{|c|@connections[c].process_command}
end
end
102 changes: 102 additions & 0 deletions test/test_pg.rb
@@ -0,0 +1,102 @@
require 'rubygems'
require 'neverblock'
require 'neverblock-pg'

$fpool = NB::Pool::FiberPool.new(24)

$long_count = ARGV[0].to_i
$freq = ARGV[1].to_i
$done = false

$connections = {}
$sockets = []
$cpool = NB::Pool::FiberedConnectionPool.new(:size=>10, :eager=>true) {
conn = NB::DB::FPGconn.new({:host=>'localhost',:user=>'postgres',:dbname=>'evented'})
$sockets << socket = IO.new(conn.socket)
$connections[socket] = conn
}

def $cpool.exec(sql)
hold do |conn|
conn.exec(sql)
end
end

def $cpool.[](sql)
self.exec(sql)
end

def $cpool.begin_db_transaction
hold(true) do |conn|
conn.exec("begin")
end
end
def $cpool.rollback_db_transaction
hold do |conn|
conn.exec("rollback")
release(Fiber.current,conn)
end
end
def $cpool.commit_db_transaction
hold do |conn|
conn.exec("commit")
release(Fiber.current,conn)
end
end

$long_query = "select sleep(1)"
$short_query = "select 1"

def run_blocking
t = Time.now
$long_count.times do |i|
$cpool[$long_query]
$freq.times do |j|
$cpool[$short_query].each{|r|r}
end
end
Time.now - t
end
print "finished blocking queries in : "
puts $b_time = run_blocking

def run_evented
$count = 0
$count_long = 0
$finished = 0
$long_count.times do |i|
$fpool.spawn do
$cpool[$long_query].each{|r|r}
$finished = $finished + 1
if $finished == ($long_count * ($freq+1))
puts ($e_l_time = Time.now - $t)
puts "advantage = #{(100 - ( $e_l_time / $b_time ) * 100).to_i}%"
stop_loop
end
end
$freq.times do |j|
$fpool.spawn do
$cpool[$short_query].each{|r|r}
$finished = $finished + 1
if $finished == ($long_count * ($freq+1))
puts ($e_l_time = Time.now - $t)
puts "advantage = #{(100 - ( $e_l_time / $b_time ) * 100).to_i}%"
stop_loop
end
end
end
end
end

def stop_loop
$done = true
end

$t = Time.now
print "finished evented queries in : "
run_evented
loop do
res = select($sockets,nil,nil,nil)
res.first.each{ |s|$connections[s].process_command } if res
break if $done
end

0 comments on commit 6ca677d

Please sign in to comment.