require "rubygems"
require "ruby-prof"
require 'mysql'
# ----------------------------------
# Setup
def print_profile(result)
if result
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, 0)
else
puts "There are no result to print."
end
end
# ------------------------------------------
# Setup functions
def create_db_statement
return "CREATE DATABASE mysql_profile"
end
def create_table_statement
return "CREATE TABLE target (id INT, data TEXT) TYPE=innodb;"
end
def insert_statement(table_name, column_name, value)
return "INSERT INTO #{table_name} (#{column_name}) VALUES ('#{value}')"
end
# ----------------------------------------
# Profile test functions
def write_many_profile(connection, num_times, value=nil)
puts "Write #{num_times} rows with string-length: #{value.length}"
result = RubyProf.profile do
num_times.times do |i|
connection.query(insert_statement('target', 'data', value))
end
end
print_profile(result)
end
# ----------------------------------
# Start Testing
my = Mysql::new("localhost", "root", "", "mysql_profile")
num_times = 10000
string_length = 16
write_many_profile(my, num_times, 'a'*string_length)