Skip to content

Instantly share code, notes, and snippets.

@syrnick
Created January 5, 2011 23:55
Show Gist options
  • Save syrnick/767260 to your computer and use it in GitHub Desktop.
Save syrnick/767260 to your computer and use it in GitHub Desktop.
Datamapper performance comparison and profiling. Part2.
# Trying to replicate performance issues with DM 1.0
# DM 1.02:
# Create: 3.100000 0.180000 3.280000 ( 4.758490)
# Get: 3.720000 0.180000 3.900000 ( 5.238979)
# DM 0.10:
# Create: 1.200000 0.100000 1.300000 ( 2.411933)
# Get: 2.540000 0.130000 2.670000 ( 3.465188)
###
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-migrations/migration_runner'
require 'dm-timestamps'
require 'benchmark'
require 'ruby-prof'
#DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'postgres://localhost/dm_perf_test') # createdb test
class Course
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :exams
end
class Exam
include DataMapper::Resource
property :id, Serial
property :course_id, Serial
belongs_to :course
end
DataMapper::MigrationRunner.migration( 1, :create_courses ) do
up do
create_table :courses do
column :id, Integer, :serial => true
column :name, String, :size => 50
end
create_index(:courses, :id)
create_table :exams do
column :id, Integer, :serial => true
column :course_id, Integer
end
create_index(:exams, :id)
create_index(:exams, :course_id)
end
down do
drop_table :exams
drop_table :courses
end
end
DataMapper::MigrationRunner.migrate_up!
exams = []
timing_create = Benchmark.measure do
1000.times do |i|
course = Course.create(:name => "Ruby #{i}")
exam = Exam.create(:course => course)
exams << exam.id
end
end
puts timing_create
if ARGV[0] == 'timing'
timing_get = Benchmark.measure do
exams.each do |exam_id|
Exam.all(:id => exam_id).first.course
end
end
puts timing_get
else
RubyProf.start
timing_get = Benchmark.measure do
exams.each do |exam_id|
Exam.all(:id => exam_id).first.course
end
end
profiling_result = RubyProf.stop
printer = RubyProf::GraphHtmlPrinter.new(profiling_result)
File.open('profile_get.html','w') do |out_file|
printer.print(out_file, :min_percent => 0.05)
end
puts 'profiling complete. See profile_get.html'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment