From 88305d53c0cc91d4caaf7bf9ebd99bff075c9c6c Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Tue, 6 Apr 2010 01:16:37 -0700 Subject: [PATCH] add data generation script for benchmarking, update order benchmarks are run to sort by speed according to the test data generated by benchmark/setup_db.rb - update benchmarks on readme --- README.rdoc | 16 +++--- benchmark/query.rb | 28 +++++------ benchmark/setup_db.rb | 110 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 benchmark/setup_db.rb diff --git a/README.rdoc b/README.rdoc index 3da1878..98bf4f6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -76,18 +76,18 @@ them into proper Ruby types in Ruby-land - which is slow as balls. Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types. -Me: Yep, but it's API is considerably more complex *and* is 3x slower. +Me: Yep, but it's API is considerably more complex *and* is 2-3x slower. == Benchmarks -Performing a basic "SELECT * FROM" query on a table with ~31k rows and fields of nearly every Ruby-representable data type, +Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type, then iterating over every row using an #each like method yielding a block: - # The Mysql gem appears faster because it only gives back nil and strings. - user system total real - Mysql - 1.050000 0.090000 1.140000 ( 1.441405) + # And remember, the Mysql gem only gives back nil and strings. + user system total real Mysql2 - 3.660000 0.170000 3.830000 ( 4.082238) + 2.080000 0.790000 2.870000 ( 3.418861) + Mysql + 1.210000 0.790000 2.000000 ( 4.527824) do_mysql - 11.440000 0.260000 11.700000 ( 11.951023) \ No newline at end of file + 5.450000 0.980000 6.430000 ( 7.001563) \ No newline at end of file diff --git a/benchmark/query.rb b/benchmark/query.rb index 0dcf0e1..f00762b 100644 --- a/benchmark/query.rb +++ b/benchmark/query.rb @@ -7,22 +7,10 @@ require 'mysql2_ext' require 'do_mysql' number_of = 1 -database = 'nbb_1_production' -sql = "SELECT * FROM account_entries" +database = 'test' +sql = "SELECT * FROM mysql2_test" Benchmark.bmbm do |x| - mysql = Mysql.new("localhost", "root") - mysql.query "USE #{database}" - x.report do - puts "Mysql" - number_of.times do - mysql_result = mysql.query sql - mysql_result.each_hash do |res| - # puts res.inspect - end - end - end - mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root") mysql2.query "USE #{database}" x.report do @@ -35,6 +23,18 @@ Benchmark.bmbm do |x| 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 + mysql_result.each_hash do |res| + # puts res.inspect + end + end + end + do_mysql = DataObjects::Connection.new("mysql://localhost/#{database}") command = DataObjects::Mysql::Command.new do_mysql, sql x.report do diff --git a/benchmark/setup_db.rb b/benchmark/setup_db.rb new file mode 100644 index 0000000..278fcc5 --- /dev/null +++ b/benchmark/setup_db.rb @@ -0,0 +1,110 @@ +# encoding: UTF-8 + +# This script is for generating psudo-random data into a single table consisting of nearly every +# data type MySQL 5.1 supports. +# +# It's meant to be used with the query.rb benchmark script (or others in the future) + +require 'mysql2_ext' +require 'rubygems' +require 'faker' + +num = ENV['NUM'] && ENV['NUM'].to_i || 10_000 + +create_table_sql = %[ + CREATE TABLE IF NOT EXISTS mysql2_test ( + null_test VARCHAR(10), + bit_test BIT, + tiny_int_test TINYINT, + small_int_test SMALLINT, + medium_int_test MEDIUMINT, + int_test INT, + big_int_test BIGINT, + float_test FLOAT(10,3), + double_test DOUBLE(10,3), + decimal_test DECIMAL(10,3), + date_test DATE, + date_time_test DATETIME, + timestamp_test TIMESTAMP, + time_test TIME, + year_test YEAR(4), + char_test CHAR(10), + varchar_test VARCHAR(10), + binary_test BINARY(10), + varbinary_test VARBINARY(10), + tiny_blob_test TINYBLOB, + tiny_text_test TINYTEXT, + blob_test BLOB, + text_test TEXT, + medium_blob_test MEDIUMBLOB, + medium_text_test MEDIUMTEXT, + long_blob_test LONGBLOB, + long_text_test LONGTEXT, + enum_test ENUM('val1', 'val2'), + set_test SET('val1', 'val2') + ) DEFAULT CHARSET=utf8 +] + +# connect to localhost by default, pass options as needed +@client = Mysql2::Client.new :host => "localhost", :username => "root", :database => "test" + +@client.query create_table_sql + +def insert_record(args) + insert_sql = " + INSERT INTO mysql2_test ( + null_test, bit_test, tiny_int_test, small_int_test, medium_int_test, int_test, big_int_test, + float_test, double_test, decimal_test, date_test, date_time_test, timestamp_test, time_test, + year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test, + tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test, + long_blob_test, long_text_test, enum_test, set_test + ) + + VALUES ( + NULL, #{args[:bit_test]}, #{args[:tiny_int_test]}, #{args[:small_int_test]}, #{args[:medium_int_test]}, #{args[:int_test]}, #{args[:big_int_test]}, + #{args[:float_test]}, #{args[:double_test]}, #{args[:decimal_test]}, '#{args[:date_test]}', '#{args[:date_time_test]}', '#{args[:timestamp_test]}', '#{args[:time_test]}', + #{args[:year_test]}, '#{args[:char_test]}', '#{args[:varchar_test]}', '#{args[:binary_test]}', '#{args[:varbinary_test]}', '#{args[:tiny_blob_test]}', + '#{args[:tiny_text_test]}', '#{args[:blob_test]}', '#{args[:text_test]}', '#{args[:medium_blob_test]}', '#{args[:medium_text_test]}', + '#{args[:long_blob_test]}', '#{args[:long_text_test]}', '#{args[:enum_test]}', '#{args[:set_test]}' + ) + " + @client.query insert_sql +end + +puts "Creating #{num} records" +num.times do |n| + insert_record( + :bit_test => 1, + :tiny_int_test => rand(128), + :small_int_test => rand(32767), + :medium_int_test => rand(8388607), + :int_test => rand(2147483647), + :big_int_test => rand(9223372036854775807), + :float_test => rand(32767)/1.87, + :double_test => rand(8388607)/1.87, + :decimal_test => rand(8388607)/1.87, + :date_test => '2010-4-4', + :date_time_test => '2010-4-4 11:44:00', + :timestamp_test => '2010-4-4 11:44:00', + :time_test => '11:44:00', + :year_test => Time.now.year, + :char_test => Faker::Lorem.words(rand(5)), + :varchar_test => Faker::Lorem.words(rand(5)), + :binary_test => Faker::Lorem.words(rand(5)), + :varbinary_test => Faker::Lorem.words(rand(5)), + :tiny_blob_test => Faker::Lorem.words(rand(5)), + :tiny_text_test => Faker::Lorem.paragraph(rand(5)), + :blob_test => Faker::Lorem.paragraphs(rand(25)), + :text_test => Faker::Lorem.paragraphs(rand(25)), + :medium_blob_test => Faker::Lorem.paragraphs(rand(25)), + :medium_text_test => Faker::Lorem.paragraphs(rand(25)), + :long_blob_test => Faker::Lorem.paragraphs(rand(25)), + :long_text_test => Faker::Lorem.paragraphs(rand(25)), + :enum_test => ['val1', 'val2'].rand, + :set_test => ['val1', 'val2', 'val1,val2'].rand + ) + $stdout.putc '.' + $stdout.flush +end +puts +puts "Done" \ No newline at end of file