File: threaded.rb

package info (click to toggle)
ruby-mysql2 0.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 748 kB
  • ctags: 511
  • sloc: ansic: 2,985; ruby: 2,699; sh: 61; makefile: 3
file content (17 lines) | stat: -rw-r--r-- 551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# encoding: utf-8

require 'mysql2'
require 'timeout'

# Should never exceed worst case 3.5 secs across all 20 threads
Timeout.timeout(3.5) do
  20.times.map do
    Thread.new do
      overhead = rand(3)
      puts ">> thread #{Thread.current.object_id} query, #{overhead} sec overhead"
      # 3 second overhead per query
      Mysql2::Client.new(:host => "localhost", :username => "root").query("SELECT sleep(#{overhead}) as result")
      puts "<< thread #{Thread.current.object_id} result, #{overhead} sec overhead"
    end
  end.each(&:join)
end