From d9200c43b49fa6d82f86f0b493f58d56b102dd08 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Thu, 6 May 2010 00:09:18 -0700 Subject: [PATCH] add raw driver benchmark with casting for the mysql gem --- benchmark/query_with_mysql_casting.rb | 82 +++++++++++++++++++ ...uery.rb => query_without_mysql_casting.rb} | 0 2 files changed, 82 insertions(+) create mode 100644 benchmark/query_with_mysql_casting.rb rename benchmark/{query.rb => query_without_mysql_casting.rb} (100%) diff --git a/benchmark/query_with_mysql_casting.rb b/benchmark/query_with_mysql_casting.rb new file mode 100644 index 0000000..93435d9 --- /dev/null +++ b/benchmark/query_with_mysql_casting.rb @@ -0,0 +1,82 @@ +# encoding: UTF-8 + +require 'rubygems' +require 'benchmark' +require 'mysql' +require 'mysql2_ext' +require 'do_mysql' + +number_of = 100 +database = 'test' +sql = "SELECT * FROM mysql2_test LIMIT 100" + +class Mysql + include Enumerable +end + +def mysql_cast(type, value) + case type + when Mysql::Field::TYPE_NULL + nil + when Mysql::Field::TYPE_TINY, Mysql::Field::TYPE_SHORT, Mysql::Field::TYPE_LONG, + Mysql::Field::TYPE_INT24, Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_YEAR + value.to_i + when Mysql::Field::TYPE_DECIMAL, Mysql::Field::TYPE_NEWDECIMAL + BigDecimal.new(value) + when Mysql::Field::TYPE_DOUBLE, Mysql::Field::TYPE_FLOAT + value.to_f + when Mysql::Field::TYPE_DATE + Date.parse(value) + when Mysql::Field::TYPE_TIME, Mysql::Field::TYPE_DATETIME, Mysql::Field::TYPE_TIMESTAMP + Time.parse(value) + when Mysql::Field::TYPE_BLOB, Mysql::Field::TYPE_BIT, Mysql::Field::TYPE_STRING, + Mysql::Field::TYPE_VAR_STRING, Mysql::Field::TYPE_CHAR, Mysql::Field::TYPE_SET + Mysql::Field::TYPE_ENUM + value + else + value + end +end + +Benchmark.bmbm do |x| + mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root") + mysql2.query "USE #{database}" + x.report do + puts "Mysql2" + number_of.times do + mysql2_result = mysql2.query sql + mysql2_result.each(:symbolize_keys => true) do |res| + # puts res.inspect + end + end + end + + mysql = Mysql.new("localhost", "root") + mysql.query "USE #{database}" + x.report do + puts "Mysql" + number_of.times do + mysql_result = mysql.query sql + fields = mysql_result.fetch_fields + mysql_result.each do |row| + row_hash = {} + row.each_with_index do |f, j| + row_hash[fields[j].name.to_sym] = mysql_cast(fields[j].type, row[j]) + end + # puts row_hash.inspect + end + end + end + + do_mysql = DataObjects::Connection.new("mysql://localhost/#{database}") + command = DataObjects::Mysql::Command.new do_mysql, sql + x.report do + puts "do_mysql" + number_of.times do + do_result = command.execute_reader + do_result.each do |res| + # puts res.inspect + end + end + end +end \ No newline at end of file diff --git a/benchmark/query.rb b/benchmark/query_without_mysql_casting.rb similarity index 100% rename from benchmark/query.rb rename to benchmark/query_without_mysql_casting.rb