Skip to content

Instantly share code, notes, and snippets.

@Guitaronin
Created February 6, 2013 04:18
Show Gist options
  • Save Guitaronin/4720245 to your computer and use it in GitHub Desktop.
Save Guitaronin/4720245 to your computer and use it in GitHub Desktop.
Attempting to benchmark popular rails ORMs performing a snippet of a task.
require 'benchmark'
require 'pp'
require 'active_record'
require 'sequel'
require 'data_mapper'
@start = Date.parse("2012-12-01")
@end = Date.parse("2012-12-31")
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'escobar_development', username: 'root', password: nil, pool: 5, timeout: 5000)
class MCwAR < ActiveRecord::Base
self.table_name = 'marketing_costs'
end
Sequel.connect(adapter: 'mysql2', user: 'root', password: nil, host: 'localhost', database: 'escobar_development')
class MCwSq < Sequel::Model(:marketing_costs)
end
DataMapper.setup(:default, 'mysql://root@localhost/escobar_development')
class MCwDM
include DataMapper::Resource
storage_names[:default] = 'marketing_costs'
property :ad_dimension, String
property :ad_group, String
property :ad_position, String
property :ad_type, String
property :ad_url, String
property :campaign, String
property :content, String
property :cost, Decimal
property :cost_events, Integer
property :cost_model, String
property :created_at, DateTime
property :creative_filename, String
property :date, Date
property :datetime, DateTime
property :id, Serial
property :import_id, Integer
property :impressions, Integer
property :match_type, String
property :medium, String
property :placement, String
property :source_id, Integer
property :source_name, String
property :source_type, String
property :sub_id, String
property :term, String
property :time, Time
property :timestamp, DateTime
property :tv_market, String
property :updated_at, DateTime
end
Benchmark.bm do |x|
x.report("activerecord") do
100.times do
objs = MCwAR.select(['source_id', 'campaign', 'sum(cost)', 'sum(cost_events)']).where(date: (@start..@end)).group(['source_id', 'campaign'])
objs.each do |obj|
row_id = obj.source_id.to_s + obj.campaign.to_s
end
end
end
x.report("sequel") do
100.times do
objs = MCwSq.select_group(:source_id, :campaign).select_append{sum(cost)}.select_append{sum(cost_events)}.where(date: (@start..@end))
objs.each do |obj|
row_id = obj.source_id.to_s + obj.campaign.to_s
end
end
end
x.report("datamapper") do
100.times do
objs = MCwDM.aggregate(:source_id, :campaign, :cost.sum, :cost_events.count, {conditions: {timestamp: (@start..@end)} } )
objs.each do |obj|
row_id = obj[0].to_s + obj[1].to_s
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment