| 1 |
ROOT = File.dirname(__FILE__) + "/.." |
|---|
| 2 |
$:.unshift "#{ROOT}/activerecord/lib" |
|---|
| 3 |
|
|---|
| 4 |
require 'active_record' |
|---|
| 5 |
require 'benchmark' |
|---|
| 6 |
|
|---|
| 7 |
ActiveRecord::Base.logger = Logger.new(nil) |
|---|
| 8 |
ActiveRecord::Base.colorize_logging = false |
|---|
| 9 |
|
|---|
| 10 |
ActiveRecord::Base.establish_connection( |
|---|
| 11 |
:adapter => "sqlite3", |
|---|
| 12 |
:dbfile => ":memory:" |
|---|
| 13 |
) |
|---|
| 14 |
|
|---|
| 15 |
DATA.read.split(/;/).each do |statement| |
|---|
| 16 |
next if statement.strip.blank? |
|---|
| 17 |
ActiveRecord::Base.connection.execute statement |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
N = 1000 |
|---|
| 21 |
|
|---|
| 22 |
class Thing < ActiveRecord::Base |
|---|
| 23 |
serialize :mixed_bag |
|---|
| 24 |
end |
|---|
| 25 |
|
|---|
| 26 |
value = [1, :two, "three", { :four => [5, "six", :seven] }, |
|---|
| 27 |
%w(eight nine ten eleven twelve thirteen)] |
|---|
| 28 |
|
|---|
| 29 |
Thing.create :name => "Joshua Jamis Amos Gordon Buck", |
|---|
| 30 |
:description => "But soft! What light through yonder window " + |
|---|
| 31 |
"breaks? It is the east, and Juliet is the sun!", |
|---|
| 32 |
:mixed_bag => value, |
|---|
| 33 |
:age => 30 |
|---|
| 34 |
|
|---|
| 35 |
puts "running each test #{N} times" |
|---|
| 36 |
Benchmark.bm(35) do |reporter| |
|---|
| 37 |
reporter.report("find") { N.times { Thing.find(:first) } } |
|---|
| 38 |
reporter.report("save_without_changes") { |
|---|
| 39 |
thing = Thing.find(:first) |
|---|
| 40 |
N.times { thing.save! } } |
|---|
| 41 |
reporter.report("save_with_changes") { |
|---|
| 42 |
thing = Thing.find(:first) |
|---|
| 43 |
N.times { thing.age += 1; thing.save! } } |
|---|
| 44 |
reporter.report("query_integer_field") { |
|---|
| 45 |
thing = Thing.find(:first) |
|---|
| 46 |
N.times { thing.age } } |
|---|
| 47 |
reporter.report("query_string_field") { |
|---|
| 48 |
thing = Thing.find(:first) |
|---|
| 49 |
N.times { thing.name } } |
|---|
| 50 |
reporter.report("query_string_fields") { |
|---|
| 51 |
thing = Thing.find(:first) |
|---|
| 52 |
N.times { thing.name; thing.description } } |
|---|
| 53 |
reporter.report("query_serializable_field") { |
|---|
| 54 |
thing = Thing.find(:first) |
|---|
| 55 |
N.times { thing.mixed_bag } } |
|---|
| 56 |
reporter.report("query_string_field_and_save") { |
|---|
| 57 |
thing = Thing.find(:first) |
|---|
| 58 |
N.times { thing.name; thing.save! } } |
|---|
| 59 |
reporter.report("query_string_fields_and_save") { |
|---|
| 60 |
thing = Thing.find(:first) |
|---|
| 61 |
N.times { thing.name; thing.description; thing.save! } } |
|---|
| 62 |
reporter.report("query_serializable_field_and_save") { |
|---|
| 63 |
thing = Thing.find(:first) |
|---|
| 64 |
N.times { thing.mixed_bag; thing.save! } } |
|---|
| 65 |
reporter.report("assign_string_field_and_save") { |
|---|
| 66 |
thing = Thing.find(:first) |
|---|
| 67 |
N.times { thing.name = "Harry Potter"; thing.save! } } |
|---|
| 68 |
reporter.report("assign_serializable_field_and_save") { |
|---|
| 69 |
thing = Thing.find(:first) |
|---|
| 70 |
N.times { thing.mixed_bag = value; thing.save! } } |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
__END__ |
|---|
| 74 |
create table things ( |
|---|
| 75 |
id integer primary key, |
|---|
| 76 |
name varchar2(40) not null, |
|---|
| 77 |
description varchar2(40) not null, |
|---|
| 78 |
mixed_bag text, |
|---|
| 79 |
age integer default 0 |
|---|
| 80 |
); |
|---|