Created
November 17, 2010 14:20
-
-
Save tomas-stefano/703422 to your computer and use it in GitHub Desktop.
Arel benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bench_dir = File.expand_path(File.dirname(__FILE__)) | |
| $LOAD_PATH.unshift(File.join( bench_dir, '..', 'lib')) | |
| require 'benchmark' | |
| require File.expand_path(File.join(File.dirname(__FILE__), 'fake_engine')) | |
| @table = Arel::Table.new(:users) | |
| @photos = Arel::Table.new(:photos) | |
| def run(number=10_000_000) | |
| number.times do | |
| yield | |
| end | |
| end | |
| def simple_query! | |
| @table.take(1).where("name = 'tomas'").project('*').to_sql | |
| end | |
| def a_little_more_complexity! | |
| @table.take(1). \ | |
| where("name = 'tomas'"). \ | |
| join(@photos). \ | |
| having("email = 'tomas_stefano@'"). \ | |
| project('name', 'email', 'count(*)'). \ | |
| order("name", "email"). \ | |
| to_sql | |
| end | |
| # More complex example using the fields methid in Arel | |
| # * Maybe I need to do a bench with raw strings too equal in this example | |
| # | |
| def a_more_complex_example! | |
| @table.take(1). \ | |
| where(@table[:name].eq('tomas')). \ | |
| where(@table[:email].eq('tomas_stefano')).\ | |
| where(@table[:age].eq(10)).\ | |
| join(@photos). \ | |
| on(@photos[:parent_id].eq(@table[:id])). \ | |
| having(@table[:age].eq(10)). \ | |
| project(@table[:name], @table[:email], @table[:age], 'count(*)'). \ | |
| order(@table[:name], @table[:email], @table[:age]). \ | |
| skip(10). \ | |
| to_sql | |
| end | |
| Benchmark.benchmark do |x| | |
| x.report("10.000 Queries ") do | |
| run(10_000) do | |
| simple_query! | |
| end | |
| end | |
| x.report("100.000 Queries ") do | |
| run(100_000) do | |
| simple_query! | |
| end | |
| end | |
| x.report("1.000.000 Queries") do | |
| run(1_000_000) do | |
| simple_query! | |
| end | |
| end | |
| puts | |
| puts "Add some little complexity =p" | |
| puts | |
| x.report("10.000 Queries ") do | |
| run(10_000) do | |
| a_little_more_complexity! | |
| end | |
| end | |
| x.report("100.000 Queries ") do | |
| run(100_000) do | |
| a_little_more_complexity! | |
| end | |
| end | |
| x.report("1.000.000 Queries ") do | |
| run(1_000_000) do | |
| a_little_more_complexity! | |
| end | |
| end | |
| puts | |
| puts "A more complex Example" | |
| puts | |
| x.report("10.000 Queries ") do | |
| run(10_000) do | |
| a_more_complex_example! | |
| end | |
| end | |
| x.report("100.000 Queries ") do | |
| run(100_000) do | |
| a_more_complex_example! | |
| end | |
| end | |
| x.report("1.000.000 Queries ") do | |
| run(1_000_000) do | |
| a_more_complex_example! | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'arel' | |
| module FakeRecord | |
| class Column < Struct.new(:name, :type) | |
| end | |
| class Connection | |
| attr_reader :tables | |
| def initialize | |
| @tables = %w{ users photos developers } | |
| @columns = { | |
| 'users' => [ | |
| Column.new('id', :integer), | |
| Column.new('name', :string), | |
| Column.new('email', :string), | |
| Column.new('age', :integer), | |
| Column.new('bool', :boolean), | |
| Column.new('created_at', :date), | |
| ], | |
| 'photos' => [ | |
| Column.new('parent_id', :integer) | |
| ] | |
| } | |
| @primary_keys = { | |
| 'users' => 'id' | |
| } | |
| end | |
| def primary_key name | |
| @primary_keys[name.to_s] | |
| end | |
| def table_exists? name | |
| @tables.include? name.to_s | |
| end | |
| def columns name, message = nil | |
| @columns[name.to_s] | |
| end | |
| def quote_table_name name | |
| "\"#{name.to_s}\"" | |
| end | |
| def quote_column_name name | |
| "\"#{name.to_s}\"" | |
| end | |
| def quote thing, column = nil | |
| if column && column.type == :integer | |
| return 'NULL' if thing.nil? | |
| return thing.to_i | |
| end | |
| case thing | |
| when true | |
| "'t'" | |
| when false | |
| "'f'" | |
| when nil | |
| 'NULL' | |
| when Numeric | |
| thing | |
| else | |
| "'#{thing}'" | |
| end | |
| end | |
| end | |
| class ConnectionPool | |
| class Spec < Struct.new(:config) | |
| end | |
| attr_reader :spec, :connection | |
| def initialize | |
| @spec = Spec.new(:adapter => 'america') | |
| @connection = Connection.new | |
| end | |
| def with_connection | |
| yield connection | |
| end | |
| end | |
| class Base | |
| attr_accessor :connection_pool | |
| def initialize | |
| @connection_pool = ConnectionPool.new | |
| end | |
| def connection | |
| connection_pool.connection | |
| end | |
| end | |
| end | |
| Arel::Table.engine = Arel::Sql::Engine.new(FakeRecord::Base.new) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Simple Query | |
| 10.000 Queries 0.320000 0.000000 0.320000 ( 0.333302) | |
| 100.000 Queries 3.090000 0.010000 3.100000 ( 3.115193) | |
| 1.000.000 Queries 30.850000 0.080000 30.930000 ( 31.036174) | |
| Add some little complexity =p | |
| 10.000 Queries 0.550000 0.000000 0.550000 ( 0.551271) | |
| 100.000 Queries 5.550000 0.020000 5.570000 ( 5.643197) | |
| 1.000.000 Queries 55.670000 0.180000 55.850000 ( 56.547011) | |
| A more complex Example | |
| 10.000 Queries 1.390000 0.000000 1.390000 ( 1.391753) | |
| 100.000 Queries 13.850000 0.030000 13.880000 ( 13.899418) | |
| 1.000.000 Queries 139.320000 0.430000 139.750000 (141.308846) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment