Skip to content

Commit 6ed499f

Browse files
NullVoxPopulirichmolj
authored andcommitted
added active record benchmark (#1919)
* added active record benchmark * address bf4's feedbock * fix spacing
1 parent 0606b06 commit 6ed499f

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
3838

3939
group :bench do
4040
# https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
41-
gem 'benchmark-ips', require: false, group: :development
41+
gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
4242
end
4343

4444
group :test do

test/benchmark/benchmarking_support.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def ams(label = nil, time:, disable_gc: true, warmup: 3, &block)
3636
version: ::ActiveModel::Serializer::VERSION.to_s,
3737
rails_version: ::Rails.version.to_s,
3838
iterations_per_second: entry.ips,
39-
iterations_per_second_standard_deviation: entry.stddev_percentage,
39+
iterations_per_second_standard_deviation: entry.error_percentage,
4040
total_allocated_objects_per_iteration: count_total_allocated_objects(&block)
4141
}.to_json
4242

test/benchmark/bm_active_record.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
require_relative './benchmarking_support'
2+
require_relative './app'
3+
4+
time = 10
5+
disable_gc = true
6+
7+
# This is to disable any key transform effects that may impact performance
8+
ActiveModelSerializers.config.key_transform = :unaltered
9+
10+
###########################################
11+
# Setup active record models
12+
##########################################
13+
require 'active_record'
14+
require 'sqlite3'
15+
16+
# For debugging SQL output
17+
# ActiveRecord::Base.logger = Logger.new(STDERR)
18+
19+
# Change the following to reflect your database settings
20+
ActiveRecord::Base.establish_connection(
21+
adapter: 'sqlite3',
22+
database: ':memory:'
23+
)
24+
25+
# Don't show migration output when constructing fake db
26+
ActiveRecord::Migration.verbose = false
27+
28+
ActiveRecord::Schema.define do
29+
create_table :authors, force: true do |t|
30+
t.string :name
31+
end
32+
33+
create_table :posts, force: true do |t|
34+
t.text :body
35+
t.string :title
36+
t.references :author
37+
end
38+
39+
create_table :profiles, force: true do |t|
40+
t.text :project_url
41+
t.text :bio
42+
t.date :birthday
43+
t.references :author
44+
end
45+
end
46+
47+
class Author < ActiveRecord::Base
48+
has_one :profile
49+
has_many :posts
50+
end
51+
52+
class Post < ActiveRecord::Base
53+
belongs_to :author
54+
end
55+
56+
class Profile < ActiveRecord::Base
57+
belongs_to :author
58+
end
59+
60+
# Build out the data to serialize
61+
author = Author.create(name: 'Preston Sego')
62+
Profile.create(project_url: 'https://github.com/NullVoxPopuli', author: author)
63+
50.times do
64+
Post.create(
65+
body: 'something about how password restrictions are evil, and less secure, and with the math to prove it.',
66+
title: 'Your bank is does not know how to do security',
67+
author: author
68+
)
69+
end
70+
71+
Benchmark.ams('AR: attributes', time: time, disable_gc: disable_gc) do
72+
ActiveModelSerializers::SerializableResource.new(author, adapter: :attributes, include: 'profile,posts').serializable_hash
73+
end
74+
75+
Benchmark.ams('AR: json', time: time, disable_gc: disable_gc) do
76+
ActiveModelSerializers::SerializableResource.new(author, adapter: :json, include: 'profile,posts').serializable_hash
77+
end
78+
79+
Benchmark.ams('AR: JSON API', time: time, disable_gc: disable_gc) do
80+
ActiveModelSerializers::SerializableResource.new(author, adapter: :json_api, include: 'profile,posts').serializable_hash
81+
end

0 commit comments

Comments
 (0)