From 6ed499f38eeebdf3ac722f838dd6b80029f19c61 Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Sat, 24 Sep 2016 16:39:29 -0400 Subject: [PATCH] added active record benchmark (#1919) * added active record benchmark * address bf4's feedbock * fix spacing --- Gemfile | 2 +- test/benchmark/benchmarking_support.rb | 2 +- test/benchmark/bm_active_record.rb | 81 ++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 test/benchmark/bm_active_record.rb diff --git a/Gemfile b/Gemfile index 4505ef89..e854a204 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,7 @@ gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby]) group :bench do # 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 group :test do diff --git a/test/benchmark/benchmarking_support.rb b/test/benchmark/benchmarking_support.rb index 5894850d..dd27f6c5 100644 --- a/test/benchmark/benchmarking_support.rb +++ b/test/benchmark/benchmarking_support.rb @@ -36,7 +36,7 @@ module Benchmark version: ::ActiveModel::Serializer::VERSION.to_s, rails_version: ::Rails.version.to_s, 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) }.to_json diff --git a/test/benchmark/bm_active_record.rb b/test/benchmark/bm_active_record.rb new file mode 100644 index 00000000..0837e266 --- /dev/null +++ b/test/benchmark/bm_active_record.rb @@ -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