Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/43d8b50cc899a5be5c8e to your computer and use it in GitHub Desktop.
Save lvidarte/43d8b50cc899a5be5c8e to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'mysql'
if ! ARGV[0] or ! ARGV[1]
puts "Usage: ./bench THREADS QUERIES"
abort
end
threads = ARGV[0].to_i
queries = ARGV[1].to_i
print "Bench: #{threads} threads, #{queries} queries each\n"
def worker(queries)
worker_id = Thread.current.object_id
db = Mysql.new 'host', 'user', 'pass', 'db_name'
print "c"
i = 0
queries.times {
session_id = rand 1000000
time = Time.now.to_i
print "." if i % 10 == 0
db.query %Q{
INSERT INTO ci_sessions (
session_id, ip_address,
user_agent, last_activity,
user_data)
VALUES (
'#{session_id}', '127.0.0.1',
'Ruby', #{time},
'Worker #{worker_id}, insert')
ON DUPLICATE KEY UPDATE
user_agent='Ruby',
last_activity=#{time},
user_data='Worker #{worker_id}, update'
}
i += 1
}
end
workers = []
threads.times {
print "t"
workers << Thread.new {
worker queries
}
}
workers.each { |w| w.join }
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment