added active record benchmark (#1919)

* added active record benchmark

* address bf4's feedbock

* fix spacing
This commit is contained in:
L. Preston Sego III 2016-09-24 16:39:29 -04:00 committed by Lee Richmond
parent 0606b06abd
commit 6ed499f38e
3 changed files with 83 additions and 2 deletions

View File

@ -38,7 +38,7 @@ gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
group :bench do group :bench do
# https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76 # https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
gem 'benchmark-ips', require: false, group: :development gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
end end
group :test do group :test do

View File

@ -36,7 +36,7 @@ module Benchmark
version: ::ActiveModel::Serializer::VERSION.to_s, version: ::ActiveModel::Serializer::VERSION.to_s,
rails_version: ::Rails.version.to_s, rails_version: ::Rails.version.to_s,
iterations_per_second: entry.ips, iterations_per_second: entry.ips,
iterations_per_second_standard_deviation: entry.stddev_percentage, iterations_per_second_standard_deviation: entry.error_percentage,
total_allocated_objects_per_iteration: count_total_allocated_objects(&block) total_allocated_objects_per_iteration: count_total_allocated_objects(&block)
}.to_json }.to_json

View File

@ -0,0 +1,81 @@
require_relative './benchmarking_support'
require_relative './app'
time = 10
disable_gc = true
# This is to disable any key transform effects that may impact performance
ActiveModelSerializers.config.key_transform = :unaltered
###########################################
# Setup active record models
##########################################
require 'active_record'
require 'sqlite3'
# For debugging SQL output
# ActiveRecord::Base.logger = Logger.new(STDERR)
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
# Don't show migration output when constructing fake db
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :authors, force: true do |t|
t.string :name
end
create_table :posts, force: true do |t|
t.text :body
t.string :title
t.references :author
end
create_table :profiles, force: true do |t|
t.text :project_url
t.text :bio
t.date :birthday
t.references :author
end
end
class Author < ActiveRecord::Base
has_one :profile
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
end
class Profile < ActiveRecord::Base
belongs_to :author
end
# Build out the data to serialize
author = Author.create(name: 'Preston Sego')
Profile.create(project_url: 'https://github.com/NullVoxPopuli', author: author)
50.times do
Post.create(
body: 'something about how password restrictions are evil, and less secure, and with the math to prove it.',
title: 'Your bank is does not know how to do security',
author: author
)
end
Benchmark.ams('AR: attributes', time: time, disable_gc: disable_gc) do
ActiveModelSerializers::SerializableResource.new(author, adapter: :attributes, include: 'profile,posts').serializable_hash
end
Benchmark.ams('AR: json', time: time, disable_gc: disable_gc) do
ActiveModelSerializers::SerializableResource.new(author, adapter: :json, include: 'profile,posts').serializable_hash
end
Benchmark.ams('AR: JSON API', time: time, disable_gc: disable_gc) do
ActiveModelSerializers::SerializableResource.new(author, adapter: :json_api, include: 'profile,posts').serializable_hash
end